Garbage collection

This is an old revision of Garbage collection, edited by Marcov at . The current revision is 59397.

This page is a placeholder for experiences with trying to shoehorn the commonly used libgc (Boehm-Demers-Weiser garbage collector) for C-like languages into FPC. This can be specially useful for hybrids systems where a scripting language interacts in complex ways with a FPC core.

Such GC works by examining the datasegment and stack of the system for possible pointer values. Since FPC has the ability to redefine the memory manager, in theory one could slap the GC under the system.

There have been one or two attempts in the past, but no code was saved from these events as far as known. Some of these were on windows, which makes it harder because the versioning situation of open source libraries on Windows is often more complicated (multiple C runtimes in broad use; several msvcrt, borland, mingw,cygwin).

One of the possible problems would be that for automated type FPC often keeps references around that do not point to the beginning of the block (e.g. ansistrings), and no reference to the beginning is kept outside the heapmgr. I asked this question to sb more knowledgable in GC matters, and got this answer (verbatim):

If the GC_all_interior_pointers flag is turned off then the GC will probably prematurely free blocks for 
which there's no pointer to the start, which will cause all manner of problems.  
This flag is set by  default on my system (Debian GNU/Linux) but you should probably set it  explicitly.

So this issue can be worked around, but adds a requirement to the way libgc is initialized or compiled. (depending if the flag is compile or runtime). Since more checking is required, it could be that this option is less optimal.