Java Programming - Garbage Collections - Discussion

3. 

class Bar { } 
class Test 
{  
    Bar doBar() 
    {
        Bar b = new Bar(); /* Line 6 */
        return b; /* Line 7 */
    } 
    public static void main (String args[]) 
    { 
        Test t = new Test();  /* Line 11 */
        Bar newBar = t.doBar();  /* Line 12 */
        System.out.println("newBar"); 
        newBar = new Bar(); /* Line 14 */
        System.out.println("finishing"); /* Line 15 */
    } 
}
At what point is the Bar object, created on line 6, eligible for garbage collection?

[A]. after line 12
[B]. after line 14
[C]. after line 7, when doBar() completes
[D]. after line 15, when main() completes

Answer: Option B

Explanation:

Option B is correct. All references to the Bar object created on line 6 are destroyed when a new reference to a new Bar object is assigned to the variable newBar on line 14. Therefore the Bar object, created on line 6, is eligible for garbage collection after line 14.

Option A is wrong. This actually protects the object from garbage collection.

Option C is wrong. Because the reference in the doBar() method is returned on line 7 and is stored in newBar on line 12. This preserver the object created on line 6.

Option D is wrong. Not applicable because the object is eligible for garbage collection after line 14.


Shrikant Firodiya said: (Jul 6, 2011)  
Tell about how garbage collecter internally works?

Chandrabhushan said: (Apr 25, 2012)  
The "garbage collection" that I'm talking about is the work that the drive's internal firmware does in order to consolidate the "marked as free" logical sectors into flash memory pages that can be erased so that they're ready to accept writes from the host. That happens inside the drive and is independent of what the rest of the system is doing.

Firdosh said: (Nov 20, 2015)  
How garbage collection works?

Dipak Mehta said: (Jan 8, 2016)  
I didn't understand can some one explain please?

Chaitu said: (Dec 26, 2016)  
I didn't understand the meaning of 4, 12 &14 line on the coding.

Please explain it.

Sathiesh said: (Feb 9, 2017)  
As per my thought,

First, the main method called in a class.

doBar() method called by the Test class object t.
The default modifier or the public modifier class can be accessed inside its package so that the class Bar coded inside the class Test as a return type for the method doBar(). So it enters into the doBar() method from line 4, a new object created for the class Bar in doBar() method of Test class.

The object can be created by using the new keyword at line 6. Whenever the object created for a class, the constructor of the class has been called and execute the functions. It returns the object of class Bar at line 7 because the return type of doBar() is Bar. Then it returns back to line 12 and stores the returned object of class Bar from doBar() in a newBar object which is an object of class Bar.

At line 14, the value of object newBar that is the object which is created at the line 6 that has been replaced by creating a fresh (new) object. So that object which is created at the line 6 is not use after line 14.

Hence the garbage collection destroyed that object which is created at line 6 as after line 14.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.