Do all finalizers call during garbage collection?

Let's say I'm trying to allocate 100 bytes, but since I don't have 100 bytes in my GC heap, garbage collection starts. In addition, there are 100 MB of unreachable objects in my GC heap. As far as I understand, after the GC released 100bytes, he could decide to stop the collection and continue the program. So, let's say GC did not release objects of 50 MB in size, which are equal to 100 different objects.

My question is, does the GC challenge all finalizers? even if he does not delete them? (in this case, 100 unreachable objects that the GC decided not to delete).

+4
source share
5 answers

The problem is that everything here can be an implementation detail, and it can be different between x86 / x64 / ia64, the server and the workstation (a very different GC profile), Mono vs MS.NET, OS version, .NET / CLI main version , patch version .NET / CLI, Compact Framework, Micro Framework, etc.

I do not think that you should accept any specific behavior, except for "objects with finalizers that are not used, will probably be completed at some point, but even this is not guaranteed."

+14
source

"as soon as the GC releases 100 bytes, it can decide to stop the collection" - no, the GC does not stop the collection. It will always collect generation 0. If this is not enough, continue with Gen1 and Gen2 + LOB.

+1
source

Garbage collection in .net has two aspects: termination and destruction. When an object that has a finalizer is created, it is added to a special list, which I will call the list "finalizable". Each time the garbage collector starts, it identifies objects that are not reachable by anything, those that are reachable only from the "finalizable" list or objects in it, and those that are reachable by something else without going through the "finalizable" list. Objects that are not reachable at all are destroyed. Those that are available only from the "finalized" list or objects in it are removed from this list and placed in the "finalize-now" list. Objects that are reachable by something other than a "finalized" list survive in the GC. Although the GC sorts objects into these three categories, all code execution stops. Once the GC is complete, code execution resumes, and a special thread starts the Finalize method for all objects in the "Finish Now" list.

Please note that the objects in the "finish now" list, as well as any objects to which they refer, will not collect garbage if they are in this list; they may be eligible for garbage collection after the finalizer starts. Once they are no longer on the finalize now list, they may again be eligible to collect.

+1
source

The answer is no. The only time finalizers start is when an object is destroyed.

0
source

GC performs several passes. He searches for unreachable objects and discovers that they are checking to see if they have a finalizer. If there is a finalizer, it places them in a separate finalizer queue, and if not, clears them. So, at the first stage, he does not actually name any finalizers, he simply organizes the objects based on whether they have finalizers.

0
source

All Articles