Delphi: How to find and fix an EOutOfMemory Error?

EOutOfMemory occurs when the memory manager cannot find a contigious block of memory for a given allocation request. So you are either 1) allocating more memory than you are expecting, 2) leaking memory you have successfully allocated, or 3) fragmenting (not necessarily leaking) memory so the memory manager has to keep allocating more and more memory over time.

When the exception occurs, look at the call stack. That will lead you to the code that is failing to allocate memory. To get the call stack, run your app in the debugger, or use an exception logging framework like MadExcept, EurekaLog, JCLExcept, etc.


I doubt that the code you are showing, is the source of the problem. It is probably a place where a symptom occurs.

If you suspect that you have in fact, some basic low level corruption, you might want to try turning on FastMM's Full Debug Mode. For example, problems like the one you're encountering could be caused by general memory heap corruption instead of actually running out of memory.

If you do not have heap corruption, but instead have a true out of memory problem, then finding and solving it usually requires a proper tool, called a profiler, like AQTime. Perhaps your allocation code is wrong in a way you can understand if you simply debug your code, and find that somewhere you are trying to grab an unreasonable amount of memory, either in one, or a series of calls to some memory-allocation function.

However, without a profiler, such as nexus quality suite or AQTime, or another similar tool, you will be mostly blind. Your application is simply failing and the heap management code is reporting it is out of memory. It is possible that you are doing something somewhere that is corrupting your heap. It is possible you are really allocating too much memory for your 32 bit process. It is possible your computer does not have enough real or virtual memory, or that you are allocating a huge data structure that you have not realized is not practical, within your application.


Have you installed the full version of the FastMem memory manager? It can help you track down errors in memory handling. See if you're leaking something.

If you don't have a leak you have a pretty extreme fragmentation problem, you'll have to deal with it by maintaining a pool of objects rather than keeping allocating/deallocating them.

Tags:

Delphi

Vcl

Pascal