Garbage Collection

It came up in the Perth meeting last night that Delphi engineer Barry Kelly had experimented to some degree adding GC to Delphi.

It comes up in this episode of The Podcast at Delphi in 2008.

It links to the code publish on the (CodeGear?) website … now Embarcadero archives …

“A simple API unit for the Boehm GC, easy to use with classes needing finalization. Demo application, GC DLL, full source.”

http://cc.codegear.com/Item/21646


and separately, in 2009 :

There is “Native Precise Tracking Garbage Collector”

by Henrick Hellström Email: Anonymous

http://cc.embarcadero.com/Item/26716

Native precise tracking garbage collector for Delphi, based on a sweep-and-mark algorithm and lock-less linked lists, that can be used in either incremental mode or stop-the-world mode. It does not replace the built in memory manager, but supports memory block caching for faster allocation when used for classes that have roughly the same instance size.

The purpose of this garbage collector is to facilitate the implementation of complex structures where the ownership relations are unclear, where circular references make traditional reference counting unreliable, and where the structures are used and manipulated by multiple concurrent threads.

Supports Delphi 6 and up. (Delphi 4 and Delphi 5 are not supported, although the code could technically be modified to work with these compilers.)

(2009) ID: 26716,
Native Precise Tracking Garbage Collector
by Henrick Hellström

Text here → 26716 Native Precise Tracking Garbage Collector
No download.


(2004) ID: 21646,
API for Boehm Garbage Collector DLL
by Barry Kelly Email: Anonymous
A simple API unit for the Boehm GC, easy to use with classes needing finalization, demo application, GC DLL, full source.
Text here → 21646 API for Boehm Garbage Collector DLL
No download.

Barry Kelly , 19 April 2004

This archive contains a simple API unit for the Boehm Garbage Collector DLL, along with another unit which makes it easier to use with classes, and a demonstration application. Also included is the Boehm GC DLL binary, along with source code in the gc_dll_src directory.

The files:

BoehmGc.pas

This unit exports a dozen or so routines from the Boehm GC dll. Since the GC integrates with and replaces the Delphi default memory manager, you probably don’t need to use this unit unless you want to fine-tune the behaviour of the DLL. The DLL exports more routines than are in this unit; the C prototypes are in the gc_dll_src/gc.h header file, and can be imported as needed. If you allocate large chunks of memory (>100K) which don’t contain references to other chunks (and thus don’t need to be scanned for pointers), there are routines in this unit which you can use to increase performance. General advice: don’t tweak until you need to tweak.

Gc.pas

This is the main unit. Put this unit first in the uses clause of you project and the project will automatically use garbage collection. If you want to use objects which require finalization and you don’t want to have to call TObject.Free / TObject.Destroy on them manually, you can use the MarkForFinalization(TObject) function. The basic pattern is to register the object for finalization in its constructor and unregister it with UnmarkForFinalization in its destructor. This handles the two most common use cases for finalization: GC-invoked finalization and manual finalization. Note that it’s always safe to behave as if GC doesn’t exist, and use GetMem/FreeMem, New/Dispose, Create/Free etc. The use of these units simply allows you to also program with garbage collection.

GcTest.dpr & GcTest.exe

This program contains simple sample code demonstrating the garbage collector in action.

BoehmGC.dll

This contains the implementation of the garbage collector itself. The DLL can be recompiled from the source in gc_dll_src with various options, including multithreaded support, different pointer alignment granularities, etc.

**** The original Boehm GC source comes from: http://www.hpl.hp.com/personal/Hans_Boehm/gc/

I’m Barry Kelly: barry_j_kelly@hotmail.com You can do anything you like with my source code (*.pas, *.dpr). See the file gc_dll_src/LICENSEa for permissions for the GC itself.

That reference at the end … “The original Boehm GC source” …

:face_without_mouth: :exploding_head: :saluting_face:

image