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

clone an object locally  RSS feed

 
Suma Rangaraj
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi all -

This is my object -



What happens once I am out of the method public void setArr (A a)? I think the localCopy will still be aroudn in memory becasue a part of it being referenced by B right?

alternatively, you can just clone a.arr2.clone() to avoid teh above problem.

is my understanding correct?
 
Mohamed Sanaulla
Bartender
Posts: 3174
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot just call the clone method. You need to implement java.lang.Cloneable interface for the class whose object you are going to clone. There are lot of discussions before regarding using clone method. You can search for the same.

Clone method gives you an entirely new object with the values of the instance variables being taken from the instance from which it was cloned.

And what ever be the way the object has been created- Clone or new- If its local object- It will be eligible for GC soon after the method completes its execution provided the object reference has not been returned to the calling method.
 
Suma Rangaraj
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.

Actually my question has nothing to do with clone - just realized.

Say you create an object locally in a method but you only hold a reference to one of it's member variable's even after you are out of the method,

will the local object be GC'd ?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suma Rangaraj wrote:
Say you create an object locally in a method but you only hold a reference to one of it's member variable's even after you are out of the method,

will the local object be GC'd ?


Based on your example, yes, the A object will be eligible for GC once the method goes out of scope. The string array object, that was originally referenced by the A object, is still reachable, and will not be eligible for GC though.

Henry
 
Mohamed Sanaulla
Bartender
Posts: 3174
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suma Rangaraj wrote:Thanks for your response.

Actually my question has nothing to do with clone - just realized.

Say you create an object locally in a method but you only hold a reference to one of it's member variable's even after you are out of the method,

will the local object be GC'd ?


I did mention about this as well in last two lines of my first post above
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the local object becomes eligible for garbage collection. I'm going to restate the problem, so you can check that I understand what you mean.

Any object of class A contains a reference to an object B, held in an instance variable.

In object C, you create a new object A, referred to in a local variable, and from A obtain a reference to its object B. It holds this reference in an instance variable of C.

At this point, object C has a (local var) reference to A and a(n instance var) reference to B. The fact that one of them is also referred to by the other doesn't make a lot of difference to C in terms of memory management.

Once the method returns, the local variable reference to A, and therefore that instance of A itself, is eligible for garbage collection. The reference that that instance of A has to B will be freed up along with the rest of the object. However, there will still be a different reference to B, namely the one held in C, and so B is not eligible for collection, because B still has (at least) one reference while that instance of C is referenced somewhere.

rc
 
Suma Rangaraj
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your responses ...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!