• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ExamLab Question on Garbage Collection.

 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I believe the answer is zero i couldn't determine how when all the objects are set to null.
 
Balraj Momi
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats Correct, Answer is 0.

Here even though setNull() method is marking references to null, that is no way affecting reference inside caller method. Remember called method can only change values inside the passed reference type, It can not re-assign It to any new object or make it null.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thiyagarajan Kamala wrote:
I believe the answer is zero i couldn't determine how when all the objects are set to null.


You should know that objects are eligible for Garbage collection if know live thread can access them......

an object should atleast have one reference from stack to it(on heap)....

even if an objcet has many reference inside the heap but NOT EVEN A SINGLE REFERENCE FROM STACK, it is eligible for garbage collection only...
 
Jagat Sastry
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the cost of drawing flak for opening an ancient thread, I have one question regarding this.

Just before calling the setNull varargs method, isn't an array of Studio created before passing the object to the setNull method?
In that case, shouldn't the answer be "one"? (The array created just before setNull is called is eligible for garbage collection) Or does that come as an implementation detail?
 
ayush raj
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balraj Momi
Remember called method can only change values inside the passed reference type, It can not re-assign It to any new object or make it null.


Please site some more example to get the concept clearer . I couldn't figure out why it happened to be zero as the output . As far as i knew , every reference variable being assigned null in setNull method , the two objects created are eligible for garbage collection .

Thanks

Ayush
 
Henry Wong
author
Marshal
Pie
Posts: 21492
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jagat Sastry wrote:At the cost of drawing flak for opening an ancient thread, I have one question regarding this.

Just before calling the setNull varargs method, isn't an array of Studio created before passing the object to the setNull method?
In that case, shouldn't the answer be "one"? (The array created just before setNull is called is eligible for garbage collection) Or does that come as an implementation detail?



That's actually a good point -- the implementation detail of varargs does create an array that will be garbage collected. Not sure which way I stand on whether this is a bug in the question, but good catch nonetheless.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21492
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ayush raj wrote:
Balraj Momi
Remember called method can only change values inside the passed reference type, It can not re-assign It to any new object or make it null.


Please site some more example to get the concept clearer . I couldn't figure out why it happened to be zero as the output . As far as i knew , every reference variable being assigned null in setNull method , the two objects created are eligible for garbage collection .


It may be a good idea to review how Java passes parameters.

http://www.coderanch.com/how-to/java/CallByReferenceVsCallByValue

The parameters reference variables are local to the method -- and setting thoses references to null has no effect on the references in the main() method.

Henry
 
Alex Theedom
Author
Ranch Hand
Posts: 77
5
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interesting point Jagat. I would imagine that an array is created by the vararg itself. I have no idea where I can go to check this other than to search the net. Which I did and I found a similar discussion on the stackoverflow site: http://stackoverflow.com/questions/7452513/java-varargs-allocates-array-objects. I would be interested in a definitive answer.
 
foxy milano
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that it zero because: when the setNull method is called, it creates an array in the heap which has reference s on the stack. and all the element in the array (s[0] = s1 , s[1] = s2 , s[2] = s3 , s[3] = s4) are set to null. To conclude i think that when the method setNull terminate its execution, all the element in the array s are set to null, and all our assignment created in the main method are still valid. and the array create by setNull still has a reference to an array with null value element.
this is my point of view ...please check
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic