• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

clone an object locally

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author
Posts: 23892
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your responses ...
 
What's a year in metric? Do you know this metric stuff tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic