Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage Collection in Delphi

Does Delphi have Garbage Collection?

like image 881
Thomas Jungblut Avatar asked Dec 14 '10 15:12

Thomas Jungblut


People also ask

When and how garbage collector is invoked?

When the JVM doesn't have necessary memory space to run, the garbage collector will run and delete unnecessary objects to free up memory. Unnecessary objects are the objects which have no other references (address) pointing to them.

What languages support garbage collection?

Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java.


Video Answer


2 Answers

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.

like image 168
Bharat Avatar answered Sep 25 '22 06:09

Bharat


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.

like image 30
Kenneth Cochran Avatar answered Sep 24 '22 06:09

Kenneth Cochran