Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

The question below is from Dan Chisholm,


Now, After method m1 returns, the object created on which line is not eligible for garbage collection? Answer is Line 1

I can understand the above from the point that array references are passed by reference value and any modifications that we do to it's elements will refelect in the calling method as well.

Now considering the question below also from Dan Chisholm,



After method m1 returns, the object created on which line is eligible for garbage collection? The answer was, None of the above.

I want to know is that what is the difference between the above 2 cases. Why not the statement a1 = null in the m1(I[] a1) method not affect it in the main method?? Anyone please help me with this.

Thanks in advance.
 
Pinkal Patel
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the Program 1)



Assigned null to Actual value of the Array
but in the Program 2)

Will Null the Second Refference to Array Object

e.g Lets Change line

with


Meance Now
a1 -> OBJECT of Array
and
a2 -> OBJECT of Array
Both Reffer the Same object if you mack a2=null;
meance OBJECT of Array is not aloved for GC
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok that is understandable that it will null the second reference. I want to know, won't it affect the reference from the main method?? Why not the reference from the main method be nulled???

Any thoughts on that?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:
Ok that is understandable that it will null the second reference. I want to know, won't it affect the reference from the main method?? Why not the reference from the main method be nulled???

Any thoughts on that?


Remember that Java always passes a copy of the reference to the method. So it is a local reference variable that is set to null. The confusion is caused by the fact that the name of the local reference variable (in the parameter list) is the same as the one in the main method, namely a1.
 
Joe Harry
Ranch Hand
Posts: 10045
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I got the concept. Thanks ranchers for the help offered.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic