开发者

Third party Solution for Garbage Collection in Delphi

开发者 https://www.devze.com 2023-01-23 19:13 出处:网络
Is there any third party solution , VCL ,Plugins etc to do automatic garbage co开发者_StackOverflow社区llection in Delphi for win32 You can use interfaces for doing such garbage collection.

Is there any third party solution , VCL ,Plugins etc to do automatic garbage co开发者_StackOverflow社区llection in Delphi for win32


You can use interfaces for doing such garbage collection.

If you use interfaces and not classes, you don't have to put an explicit try...finally block, with a call to the free method in the finally section. The compiler will generate it for you, just like with regular string methods.

You can extend this trick to every object, with code like the one published in http://edn.embarcadero.com/article/28217

But this won't be a full garbage collector, like in java or DotNet. It's only reference counting.

IMHO full garbage collector is evil. Managing memory is not difficult in Delphi. You'll type a little more, but in all cases, the performance and global memory consummation will be better with no garbage collector.


There is API for Boehm Garbage Collector for Delphi. But it will work only on Delphi <=7 and without FastMM.


Here's a 'first party solution' that I used to use all the time, until I opted for interfaces:

When you create a class (or sometimes a whole application), first thing you do is create a TObjectList - then every time you call a constructor, you immediately add a reference to your new instance to your TobjectList. In your destructor just make sure to free your TObjectList instance and you're good: by default TOBjectList owns every object it contains - so when you free your TObjectList, everything else will get cleaned up along with it.


you can use FastMM or MemCheck to see where you forgot to free the memory, but at my knowledge a garbage collector full integrated in delphi for win32 does not exists.

best regards, Radu

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号