Garbage Collection in Delphi

Yes, it does.

Delphi Win32 does not include a garbage collector out of the box so the other answers to this question are technically correct. However, this doesn't imply that it isn't possible or that one doesn't already exist. Thanks to Delphi's replaceable memory manager Barry Kelly implemented a fully functional wrapper for the Boehm garbage collector back in 2004.

It includes sample code demonstrating its use (basically creating unassigned objects and watching the GC chew them up). There are more advanced GCs than the Boehm GC but this clearly demonstrates its possible and it can be used almost transparently. You just add the gc unit to the beginning of your project's uses clause.

And while I've not heard of any projects attempting it there is nothing preventing someone from wrapping or porting a more advanced gc.


Simple answer No.

Delphi is not a complete garbage collection language, user-defined types should be manually allocated and deallocated. It only provide automatic collection, for a few built-in types, such as strings, dynamic arrays and interfaces for ease of use.

But you can use interfaces which uses reference counting for garbage collection for some extent.