[fpc-pascal]Memory Usage

Marco van de Voort marcov at stack.nl
Mon May 12 09:47:07 CEST 2003

> I'd like to know why one of my programs pircsrv written in FPC uses up so
> much memory. 107Mb of virtual memory (so it seems) and 7Mb of data. Here
> is the top stats...

Two possibilities:
- memory leaks.  Proceed as Peter already explained.
- heap fragmentation. Use unit cmem, or if you use a lot of small same
	size reallocations, pool them and reuse (often even faster)

The second needs some explaining. The default FPC heapmanager never releases
memory back to the system. Of course it tries to reuse the memory if
possible, but sometimes with weird allocation schemes this can lead to a
race in long running programs. (like daemons)

All this is because of performance reasons. Memory allocation is close to
being the slowest thing a program can do itself (calling the OS is of course
The cmem unit links in libc, and uses the C primary memory allocations
routines. Note that if you use libc anyway for some reason, it will also avoid
having two heaps. (a libc and a fpc-rtl one)

The libc memory manager is much slower, but has less problems with strange
memory allocation patterns. It is also probably a bit more secure.

In general keep using the FPC memory manager, unless this kind of behaviour
happens (usually only in daemon programs that allocate/deallocate memory all
the time), and then either switch to libc, or try to fix the odd memory
patterns. (e.g. by pooling)

More information about the fpc-pascal mailing list