[fpc-pascal] question about FPC heap and threading

Jonas Maebe jonas.maebe at elis.ugent.be
Wed Aug 11 23:42:05 CEST 2010

On 11 Aug 2010, at 23:35, Seth Grover wrote:

> 1. The default FPC heap manager uses one heap per thread, right?


> 2. When a thread is started via, for example, BeginThread
> (http://www.freepascal.org/docs-html/rtl/system/beginthread.html) this
> heap, as well as all other threadvars in my code and the rtl code, is
> setup and initialized automatically, correct?


> 3. When this thread exits (in the example of using BeginThread, when
> the TThreadFunc function passed in to BeginThread terminates) the heap
> for that thread, in addition to the other threadvars, are
> automatically disposed, correct?

Only the already freed blocks that are still around are automatically disposed, because allocated blocks may still be referenced by other threads. The memory reserved for holding threadvars is indeed freed.

> The reason I ask is that I have a program (which executes a lot of
> worker threads via BeginThread) which is experiencing increasing
> memory usage. heaptrc does not indicate that there are any leaks, but
> it's a fairly complex program (we have some units where we're getting
> memory from libc directly, don't ask) so I'm not even supposing at
> this point that the increasing memory belongs to the FPC heaps at all,
> but I'm just trying to make sure I understand everything that's going
> on before I go making assumptions about where the memory allocation is
> coming from.

You can add the "cmem" unit to your uses clause and see whether the memory ballooning still happens. If that solves the problem, then the problem is related to the FPC heap manager, otherwise it isn't. If it does not solve the problem, compile everything with -gv and run the program under Valgrind (in case you are under Linux or Mac OS X).

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20100811/31b2cec3/attachment.html>

More information about the fpc-pascal mailing list