Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't the garbage collector figure out when objects referencing eachother are really orphaned

I understand that in a managed language like Java or C# there is this thing called a garbage collector that every once in a while checks to see if there are any object instances that are no longer referenced, and thus are completely orphaned, and clears then out of memory. But if two objects are not referenced by any variable in a program, but reference each other (like an event subscription), the garbage collector will see this reference and not clear the objects from memory.

Why is this? Why can't the garbage collector figure out that neither of the objects can be reference by any active part of the running program and dispose them.

like image 377
Eric Anastas Avatar asked Aug 03 '09 19:08

Eric Anastas


People also ask

How garbage collector knows that the object is not in use and needs to be removed?

When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots.

How does the garbage collector know if an object can be collected?

An object is eligible to be garbage collected if its reference variable is lost from the program during execution. Sometimes they are also called unreachable objects.

What is the problem with garbage collection?

When the garbage collector runs, it can introduce delays into your application. This is because of the way GC is implemented. G1GC will pause your app while it frees unused memory objects and compacts memory regions to reduce wasted space. These GC pauses can introduce visible delays while your app is running.

What is reference counting explain how they are used in garbage collection?

Reference counting. Reference counting garbage collection is where each object has a count of the number of references to it. Garbage is identified by having a reference count of zero. An object's reference count is incremented when a reference to it is created, and decremented when a reference is destroyed.


2 Answers

Your presumption is incorrect. If the GC 'sees' (see below) a cyclic reference of 2 or more objects (during the 'Mark' phase) but are not referenced by any other objects or permanent GC handles (strong references), those objects will be collected (during the 'Sweep' phase).

An in-depth look at the CLR garbage collector can be found in this MSDN article and in this blog post.

Note: In fact, the GC doesn't even 'see' these types of objects during the mark phase since they are unreachable, and hence get collected during a sweep.

like image 163
Jason Avatar answered Oct 16 '22 01:10

Jason


Most GCs don't work with reference counting anymore. They usually (and this is the case both in Java and .NET) work with reach-ability from the root set of objects. The root set of objects are globals and stack referenced instances. Anything reachable from that set directly or indirectly is alive. The rest of the memory is unreachable and thus prone to be collected.

like image 37
David Rodríguez - dribeas Avatar answered Oct 15 '22 23:10

David Rodríguez - dribeas