Page 1 of 1

High memory usage with madExcept

Posted: Mon Nov 21, 2016 3:33 pm
by csamartin
In a large Delphi 7 project (consists of > 20 DLLs + Exe; uses COM/ActiveX) I observe a big difference (> 500MB) in memory usage if i compile the binaries with or without madExcept.

See Image below.

This causes OutOfMemory Exceptions quite often, since the DLLs are sometimes linked into a .NET environment via COM and the host application is quite memory consuming itself.I also use FastMM. Without madExcept no problem occurrs.

Does anybody have observed similar problems and/or has a solution?

Re: High memory usage with madExcept

Posted: Mon Nov 21, 2016 3:57 pm
by madshi
Did you accidently activate the "leak reporting" or "crash on buffer overrun/underrun" features? These consume a lot more RAM.

Re: High memory usage with madExcept

Posted: Tue Nov 22, 2016 1:20 pm
by csamartin
No they are both off.

But the options 'RangeChecks', 'IOChecks' and 'OverFlowChecks' are activated.
Could that possibly have an effect?

Re: High memory usage with madExcept

Posted: Tue Nov 22, 2016 1:47 pm
by madshi
No, that shouldn't make any difference.

Wait, are all those > 20 DLLs compiled with madExcept? madExcept allocates a "dummy" block of about 18MB in its initialization, which is always freed whenever an exception is raised, and then allocated again after exception handling has completed. The purpose of this behaviour is that this memory block is supposed to ensure that even if the process is running out of RAM, madExcept still has some headroom to do its job. Normally, 18MB is not a problem at all. But if you have 20+ DLLs, and if they're all compiled with the full blown madExcept logic, each of them will allocate 18MB which of course adds up to a lot of RAM overall. Would that explain the problem?

Re: High memory usage with madExcept

Posted: Tue Nov 22, 2016 1:48 pm
by madshi
P.S: Most of those 18MB are only *reserved*, not allocated, but they still block a part of the addressable memory range, which in 32bit is limited to 4GB.

Re: High memory usage with madExcept

Posted: Tue Nov 22, 2016 3:24 pm
by csamartin
Thank's a lot - that's probably the explanation.
I've counted almost thirty Modules (Exe+DLLs) loaded simultanously at run time, which add up to 30 * 18MB = 540MB.

Is there a constant in the madExcept source code (which I use for compiling the binaries) defining the "18MB".
In other words, is it possible to reduce this size? Or would you recommend against it?

Re: High memory usage with madExcept

Posted: Tue Nov 22, 2016 3:32 pm
by madshi
It's just a safety net against problems for "out of RAM" situations. For any other situations these 18MB are useless. So I see no big problem reducing that at all.

Please look at madExcept.AllocOutOfMemoryInsurance(). Feel free to change that to whatever values you're comfortable with. There are 2 VirtualAlloc calls there: One that reserves (but doesn't allocate) a big chunk of memory (4MB), the other one truely allocates 500KB. This is done 4 times, which sumes up to 18MB.