Does Delphi own a garbage collector?

Asked

Viewed 2,001 times

14

Does Delphi have an automatic garbage collector? How does it work. If not, what tools can do this?

  • 1

    in the program code I always put ReportMemoryLeaksOnShutdown:= True;to have a sense of and always keep the code clean.

  • @Artur_indio, I just read about the command and it’s great. Thanks a lot :D

2 answers

19


Doesn’t exist in the most accepted sense of Garbage Collector, although I can question this concept. Delphi is a manual memory management language with some library facilities, similar to what is used in modern C++ for those who know this language.

Several existing types in the standard library have their own control of memory allocation, in general through a reference counter which is a garbage collection technique, but which some do not consider exactly a GC. Strings and arrays dynamics are examples of types that use this technique.

Another way is through the TComponent. All of his descendants "self-manage", so when an object is destroyed he is responsible for releasing his children. The sum of the two techniques cited and the use of interfaces can greatly facilitate the work when complex structures of objects are assembled. In addition it is possible to make explicit control with try-finally very similar to what happens in Java and C# when you want to clarify that a resource must be released, with the difference that Delphi can release the resource and the memory at the same time.

The ease of Counting is available in the library - implementing with interfaces - to be used with other types you want to create and it is necessary. Most types do not need thanks to the concept of property of the object (there is only one owner).

Particularly if the garbage collection is done automatically without the intervention of the programmer I consider that in a certain way the Delphi has a GC. It is necessary to make clear that its form is by reference count and not by reference tracking (tracing GC). It is officially said that Delphi is not a language with GC, but the library has similar functionality. Of course, the moment you create types, you need to remember to take care of the management using what the library already offers or otherwise, but the use of the objects happens to be managed if you use what is available.

Of course it is possible to use a GC tracker if it is absolutely necessary but it is not part of the language - so there are not many facilities - or the standard library, you have to provide all the infrastructure of it. And it is not advisable in almost any case.

There is an experience on the Embarcadero website (or whatever the name the company has now) to implement the GC Boehm which is one of the simplest Gcs that exists, almost always so inefficient that it is not worth the effort when there is another way to manage the memory automatically.

The language organizes memory so that it lacks metadata for a more modern GC since it was designed not to use a GC.

Contrary to popular belief, a modern GC can manage memory faster than other forms. In refcount it is necessary to increment the counter, then decrement and check if it has reached zero, all this can be necessary to do synchronized (atomic) what it is very expensive to accomplish. And it may be necessary to make a collection to avoid leakage by cyclical references or use a technique that can generate failures. In GC, memory allocation is usually just an increment in a pointer similar to what occurs in stack and there is no cost to dislocate when the object is no longer needed. The problem with modern GC is that it slows down the displacement and this brings some disadvantages:

  • The moment of resource release is not deterministic and auxiliary tools in language are needed to ensure this release at the right time.
  • It is also indeterminate how long it will take to make all liberation. It is true that other forms of memory release also can not have the given time but just as in the GC, there is accumulation, the indetermination gets bigger.
  • Since GC needs to determine what is useful and what is junk and in some implementations need to copy data, the total processing for the release is higher. There are exceptions.

To learn more about GC implementations the Wikipedia article shown above is already a good start. There have good books recommendation on the subject.

In the case of Delphi the "lack of a GC" makes little difference to most users, mainly because it is usually used in a more specific set of problems. For those who do libraries the extra management work is usually a bigger problem.

Another issue is that implementing a decent GC is quite complex while implementing a reference counting mechanism is trivial.

-2

No, Delphi doesn’t have GC. GC in my design is the automatic release of objects allocated by the program by code generated by the compiler in the executable. This code is at the margin of the main execution, freeing the programmer from this task (from de-locating the previously allocated memory)

Example : a thread could run GC. But only if the compiler generated code to record, in global memory, everything that is allocated at runtime, but this is not done. the record of what is allocated is only made for the types : Long strings, strings, Dynamic arrays, variants and interfaces. And the reason this is not done for the other types or objects is the performance drop of the executable, because if to each memory allocation (and are many in an executable) in a function call if code is generated to record everything that is allocated and its misalignment at an inappropriate time by the GC thread, the execution performance would have a drop and a cost in the execution speed.

The refCount that is used for strings, arrays, variants and interfaces cannot be considered a GC, if it were its name it would not be refcount, but GC.

Delphi has always been an intermediate language in the sense of being neither high-level as JAVA or VB (which produces bytecode rather than native code) and neither low-level as the Assembler which produces machine language of very high execution speed.

Delphi is in the middle, comparable to C, c++ (which also has no GC)

The search for the executable performance along with the language characteristics and standards adopted at the beginning of Pascal, Turbo Pascal and Object Pascal make Delphi a language without GC, but high performance and well superior to their peers like VB and Java with respect to performance.


Sirs :

see this study in the link below where the person makes a detailed study on comparative performance between C#, Delphi Prism, Delphi 2009 and C++ Builder 2009.

And the conclusion is that Delphi is superior in performance to these 3 languages.

Unfortunately I don’t have the same study compared to Java just to satisfy you because for me there is no doubt about what I posted, I just can’t confirm by published studies.

And what I said about Delphi being superior would be in Performance only, I could even say about other aspects but I’ll wait for other topics, because the question here was the GC.

Follow the link of the comparative performance study :

http://www.ijcaonline.org/volume26/number1/pxc3874199.pdf

Conclusion of the Study :

4.CONCLUSION

In Terms of Response time, the Fastest Programming language is the Delphi 2009 and the slowest Programming language is the C++ Builder 2009. Although the Managed language C# and the Delphi Prism are Powerful in Terms of code Density, they are Weak in Terms of memory Consumption and Response time.

The Delphi 2009 is the Most Powerful Programming language Both in Terms of memory Consumption and Response time


The reference counter (refCount) implemented by Embarcadero in Delphi has the objective of optimizing memory consumption and access performance (allocation and misallocation) since the cost of allocating memory in HEAP per windows API is high in terms of CPU cycles used. In this way the compiler allocates a large amount of memory on the executable startup and this memory is consumed in distributed pieces for the various allocations that are made during the execution of the executable module.

In this reference counting model, a piece of memory allocated for example by a Widestring or dynamic array is not obtained directly from a Windows API but a request is made to the Delphi Fastmm memory manager who through the Refcount algorithm takes care of delivering a piece of memory to the string by a low cost in terms of CPU cycles used.

Refcount is an excellent memory usage optimizer, not using it would result in unnecessary memory duplication. Already with its utilization the memory is optimized,

consider the example below :

Var

a, b : String;

Begin

a := 'asdqwe';

b := a;

End;

With Refcount there is no duplication. The allocation is made first for A which counts 1 in Refcount. In the statement below there is no new memory allocation, no copies, B simply points to the same address as A and Refcount of A is incremented by 1 resulting 2.

In the displacement the decrease of the reference meters is made. When the Counter reaches zero the memory goes back to FASTMM to be used again by new allocations.

Conclusion : Refcount is an excellent optimizer of memory features in addition to providing speed in access to it, but it cannot be considered a Garbage Collector as some academicists or "slate programmers" want to make believe.

References :

http://docwiki.embarcadero.com/RADStudio/XE6/en/Memory_Management

http://docwiki.embarcadero.com/RADStudio/XE6/en/Internal_Data_Formats


Garbage Collection (or Garbage Collection)

It is a strategy for memory management automation where the compiler generates code for misalignment automatically freeing the programmer from the misalignment task (or finalizing an object).

Perks

Removes the programmer’s concern about having to release the allocated memory, always providing a memory where there are no leaks.

The problem is that the displacement in many cases is not done at the time when the program disposes of the variable or object in memory and this brings disadvantages as performance drop in the application execution, as can be seen below;

Destanvangens

Although he has the advantage mentioned in the first paragraph, he has disadvantages. They are processes that consume computational resources to decide which parts of memory can be released, while in manual management this consumption is minimal. Another negative point is that the moment when the object is actually misaligned is not deterministic, which can result in the variation of the algorithm execution time in random parts.

The drop in performance of an application may be such that those requiring superior performance would have to temporarily suspend GC in order not to interfere with processing. No. NET 4.6 there are already directives or methods (Trystartnogcregion and Endnogcregion) to block the GC from operating on a particular code snippet.

References : http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)

https://msdn.microsoft.com/en-us/library/ee787088%28v=vs.110%29.aspx

https://msdn.microsoft.com/en-us/library/system.gc%28v=vs.110%29.aspx

  • What you say would slow you down is exactly what Delphi does. The problem with GC is something else. Aside from that you are giving an opinion about one being superior to the other language. If you will state this, prove.

  • 1

    Dear I do not need to prove what I say about performance of one language in comparison to another, who has programmed in these 4 languages as I have already programmed, know that what I speak has foundation. Do I really need to prove that the Java compiler produces bytecode and Delphi produces native code ? But if Voce wants I bring articles here.

  • 5

    Here we put objective answers. If you want to put a subjective answer, okay, but you have to substantiate what you’re saying, otherwise the answer is wrong. I have programmed in these languages and I know that it has no foundation. You have to prove why one is superior to the other, you said that. And I was clear that the problem is this. The objective part, ok, the subjective part needs support, not just your opinion.

  • 3

    Hmmm I programmed many years in Delphi and I program several years ago in Java and, contrary to what you said, I think that what you say is unfounded. I don’t think Delphi is superior to Java or other GC languages in which I’ve programmed many years as well. The performance, in turn, may be better in one or the other considering specific examples that in turn may not interest in most scenarios (or the examples are not valid in the real world or the difference in performance is not relevant in the real world).

  • Moderator’s note: You can always edit your posts (questions or answers), there is a link to it below it, before the comments area. Whenever you need to supplement or correct a response, use this feature. Use comments to discuss other comments. And only post a new answer if it is even a (different) answer to the question at the top. I hope that it was more clear that part of the confusion that happened here was because you didn’t understand the system, which is normal for new users. I recommend reading the [tour] and [help], and I’m available if you need me.

  • The second part actually falls outside the scope of the question. It exists on the basis of a statement in the original answer that cannot be proven. Even the text presented proves nothing. It does not show how tests are done and reaches staggering conclusions, mainly because it is not possible to compare languages, not even implementations of languages. And you can barely compare algorithms implemented in different languages. Experienced programmers know this. But if anyone has relevant information that’s not this, just present it.

  • Friend Oce disqualifies serious texts, when I put my answer, you disqualified me asking for proofs, I provided them, Oce disqualifies the proofs again, perhaps by your ignorance and ignorance of what is managed code and native code, or even prejudice with Delphi, but I stop here, you can say what you want, fool others there with your wisdom, I do not answer anymore. Bye

  • 2

    Sorry Claudio, I don’t want to criticize your answer, but "C++ Builder 2009" is not language (or I misunderstood your answer). I don’t understand much of this, but to my little knowledge factors that make a difference in performance are compilers and Apis used, correct me if I’m wrong. Now I think the language itself will affect little performance (maybe I’m talking a lot of nonsense), at least it’s what seems to really affect.

  • You are covered with reason William, maybe I have expressed myself imperfectly, the compiler is that produces a performative executable and not the language itself. And in the case of the Delphi compiler that we were dealing with, it’s the one that produces the most performative executable code among the ones we’re dealing with, because it produces machine-native code while others don’t. As for C++ Builder not being a language I disagree but do not understand very well its reasons why Voce believes it is not a language.

  • Good afternoon @Claudioferreira , my knowledge is limited, but as far as I know C++ Builder refers to the IDE and perhaps the additional Apis used, similar to QT. I believe that the language is "normal C++ (I can’t tell if they use the c++11 format), the only thing that changes maybe is the "compiler" and the libraries, but the language itself is the same. That is to say C++ Bulder may be an IDE and a library, but I believe it is not a "Fork" language. See more :)

  • If you can get the code done on it to be compiled and run on Unix for example in the GCC compiler, you will be programming in C++ Pure, if that is not possible Voce is programming in the C++ Builder language. It all depends on how you encode considering the various platforms that C++ connection supports.

  • 1

    @Claudioferreira Good night, if see by these points of view then C++ Builder is not a language but an SDK, as is QT. Maybe they sell this image of "language" just to say that it is an innovation, I can not say, but still everything leads to believe that it is an SDK.

  • In no way is it considered an SDK. C++ Builder is much more than that : it incorporates into its IDE the concept of RAD (Rapid Application Development) where components are reused extensively, a robust compiler and optimized for Windows, Fastmm memory manager and a huge set of accessory components that a commercial application does not run without, such as database access components, Multitier programming, Webservices, anyway, I will not put everything here because I do not know everything, .....

  • C++ Builder is a complete product and can never be called SDK because it would reduce a wonderful product with numerous features to a simple Windows library for example. Is not.

  • Good evening @Claudioferreira , So if so then QT also can not be called SDK, because it is also complete, possessing own libraries, being cross-Platform, cross-Builder, has IDE and interacts with several Apis from different operating systems. So because of this QT is now a programming language?

  • Performance is only superior in 32 bits. In 64 the Delphi gen code is not optimized

  • True @Eprogrammernotfound

  • Your example with widestring is wrong. Widestring always generates new copy with the assignment operator. See the Disassembly panel. The cases that do not generate are passed by reference or const. Your example would be correct if you had used string, ansistrig or unicodestring

  • Fact ! is the only string type that is not Referenced. I didn’t notice but the Embarcadero page had the following comment: "Widestring is composed of Widechars like Unicodestring, but is not Reference counted. " I fixed the example. Thanks for the remark.

Show 15 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.