aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
This is the question from whizlab :
How many objects are eligible for garbage collection when the System.gc() method invoked?



options are
A. 3
B. 4
C. 5
D.None of above.


Answer is A

How??
I think when we assign null reference to array object all objects are eligible for gc, since all MyClass object in array will be in Island of isolation(gc).

I think answer should be 4???


Sage of The Monstrous Toad of Mount Myoboku
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Reference passing to a method doesn't work like that in java. Try this


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

You will find that the last array position still holds the MyClass object. Also, the reference to array is not changed from the caller's perspective.

array[0] = X; is an assignment operation that will work to change the contents of this array. However array=null will not assign the reference to null outside this method's body. The caller still holds the reference to the array


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Then, what array=null means in above example?
And how many objects are eligible for garbage collection
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

rohan yadav wrote:Then, what array=null means in above example?
And how many objects are eligible for garbage collection

array=null only modifies the local array in method f. It doesn't effect the original array in main method. Thus 3 objects are eligible for GC i.e. array[0], array[1] and array[2]...
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
So original array which is passed to method f() remains unchanged, And local 3 objects are eligible for gc.

Got it.
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Can anyone tell me the documents for reference passing as an arguments to method
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Rohan, please go through this. If even after going through it you are not clear than please ask...
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
dude see.....when you passed the array to the method you create a new variable that contains the position of the array....so this refers to the original array.....but mind you this is a new variable that refers to the same array.... when you null this reference the object is still being referred by the original variable....so it not available for gc....

but using the new ref variable in the method you change the contents of the array....that is you make array[0], [1],[2] null....so objects referred by them become available for gc...
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Thanks Ankit and Ankur, understood. Now i am ready for any question regarding passing object reference to methods.
Special thanks to Ankit, the link you provided was awesome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection