• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Garbage collection

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reference passing to a method doesn't work like that in java. Try this
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then, what array=null means in above example?
And how many objects are eligible for garbage collection
 
Ankit Garg
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me the documents for reference passing as an arguments to method
 
Ankit Garg
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rohan, please go through this. If even after going through it you are not clear than please ask...
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic