If everything implemented an interface, would it be garbage collection?

I am still a beginner, and I know that my thinking is wrong; I just don't know where ...

Almost everything in Delphi comes from TObject. What if everything instead came from TInterfaceObject, which implemented some kind of trivial interface (for example, "INamable", with a single method that returned a class name string)? Since TObject already has a property that returns a name string, you will not need to add anything to additional classes.

In other words, the TInterfacedObject object inherits TObject (or something high in the hierarchy), and everything that is currently descending from TObject will now descend from this new class. Does this not mean that everything has been counted now?

If you notice where my knowledge is lacking, I would like to study. Thanks, as always - Al C.

+7
interface delphi
source share
5 answers

It is not clear whether you are asking:

  • Why didn't Borland do this when they originally developed Delphi?
  • Why doesn't Embarcadero do this in a future version of Delphi?
  • Why don't I do this with my own custom data types?

Does this not mean that now everyone has calculated the link?

Yes, that would be.

However, you don’t necessarily want everything to be counted: every small integer, every line, every logical element, every element in the array ... if not for some other reason that the ref-counting implementation adds some overhead costs, such as a small additional memory per object, may be insignificant for large objects, but proportionally more significant if applied to each tiny object.

Also see also the Garbage Collector for objects and components of Delphi , which says (quote),

Delphi provides three ways to manage objects:

  • Create / destroy objects using try..finally.
  • Use TComponent descendants - create a component and allow its owner to free it.
  • Interfaces - when the reference counter for an interface becomes 0, the object that implements it is destroyed.

Delphi help says you shouldn't mix TComponent owner approach with memory management interface, but ...

Will it be garbage collection?

Not really; simple reference counting is not as robust as garbage collection:

  • With reference counting, if you have two instances with reference counting, each of which contains a link to the other, then they are not automatically released. To free them, you need to break this "circular link" (i.e. Explicitly point one of them to release the link to the other).

  • With true garbage collection, the garbage collector would notice that these two values ​​are not mentioned anywhere and free them both.

Update
If you comment on your potentially circular links as [weak] links, they will be destroyed in order. But before Delphi 10.1 Berlin, this only works on NexGen compilers (i.e. those who use LLVM under the hood). Starting with 10.1 in Berlin, these links [weak] work everywhere.

+8
source share

It will not work with garbage collection, because interfaces use a very simple reference counting system, and circular links, which are very common in Delphi code, break simple link counting.

+5
source share

No, because of two things:

  • Even if a class implements an interface, it does not automatically count the link. Only if you really use it to implement this interface will link counting have any effect.
  • As others have already said: reference counting in interfaces will cause the class instance to be immediately released when the reference count reaches 0. This is an implicit call to the Free method at this point in the code. This will fail, for example. if two objects refer to each other. A true garbage collection will not free objects when they go out of scope, but when memory is needed, so the performance impact will not affect every time the reference count reaches 0 because the object will continue to exist. In addition, a good garbage collector will detect isolated circular links (for example, links B refer to C-links of A, but nothing to any of these objects), and also free these objects.
+3
source share

Garbage collection is different from simple recounting. You can have automatic deletion when the number of links reaches 0, but this is also not garbage collection. Garbage collection means giving up your ability to control when things are deleted from memory, and also allows you to implement a basic language implementation to optimize behavior. You stop paying attention to reference counting and trust the dynamic behavior of a particular garbage collection implementation.

Naturally, garbage collection uses a reference counting system to know when something is no longer referenced, but this is a small part of the puzzle.

+2
source share

Link counting is a form of garbage collection, but not very good. It is used by some languages ​​(I think python), although often with loop detection.

Even if you descended from TInterfaceObject, the object is not counted and, therefore, collects garbage, unless you use a link to the interface, and not a link to the object.

those. you will need to use

 Var nameable: IMyInterface; begin nameable:= IMyInterface.Create(); nameable.x(y); etc end; 

This means that your interface should already support the methods and properties that you need, which quickly becomes tedious, since you need to create an interface for each class.

This can be done quite easily in D2009 or later. See Barry Kelly Smart Pointers Example. Usually used ordinary counters.

+1
source share

All Articles