Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many objects will be eligible for garbage collection after executing "m1=null; m2=null;"?

People also ask

How many objects God created how many objects eligible for garbage collection?

An isolated island is created where all the three objects are stuck and there is no communication with any active part of the application. All the three objects internally references each other but there is no external reference which makes the three objects eligible for garbage collection.

How many objects are eligible for garbage collection just before the completion of execution of main function?

as per the answer given for this question, there is no object eligible for GC at line 11; but according to me at least one object, t2, which is set to point null at line 6, should be eligible for garbage collection.

How many objects eligible or GC is reached how many objects are eligible for garbage collection?

How many objects are eligible for garbage collections? According to Kathy Sierra's answer, it is C . That means two objects are eligible for garbage collection.

IS NULL garbage collection?

An object becomes eligible for garbage collection when there are no live threads anymore that hold a reference to the object. Explicit nulling is simply the practice of setting reference objects to null when you are finished with them.


Zero.

Object reference diagram looks like this:

Circular

You can see that the reference is circular. A reference from main to m3 keeps the m3 object alive. In turn, m3 keeps alive m1, which keeps m2 from GC.

Note that if you set m3 to null, all three objects would become eligible for GC at once, despite circular references existing for each one of them. GC is smart enough to figure out that all references are coming from GC-eligible objects, and collect all three.


Potentially all 3 of them. No variables are referenced after the // marker, so the optimizer is within its rights to drop them off the frame at this point.


Voila! GC will collect nothing here! Let's see what actually is going on here. When you created three objects of m1, m2 and m3 of MyTest, the object was created like below (say the object reference id starts from 410):

m1    MyTest  (id=410)
    m    null
m2    MyTest  (id=412)
    m    null
m3    MyTest  (id=414)
    m    null

When you initialize

m1.m = m2;
m2.m = m3;
m3.m = m1;

The objects are now looks like:

m1    MyTest  (id=410)
    m    MyTest  (id=412)
m2    MyTest  (id=412)
    m    MyTest  (id=414)
m3    MyTest  (id=414)
    m    MyTest  (id=410)
        m    MyTest  (id=412)
            m    MyTest  (id=414)
                m    MyTest  (id=410)
                    .
                    .
                    . (This is circular)

But after you reinitialized m1 and m2 to null, the objects look like:

m1    null
m2    null
m3    MyTest  (id=414)
    m    MyTest  (id=410)
        m    MyTest  (id=412)
            m    MyTest  (id=414)
                m    MyTest  (id=410)
                .
                .
                .

Look, m1 and m2 are null now, but their references are still alive in m3!


None as they are still all reachable through the circular reference you build there through m3