aspose file tools*
The moose likes Java in General and the fly likes Cloning Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cloning Question" Watch "Cloning Question" New topic
Author

Cloning Question

Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
I have a list of Objects of a class. This class has several instance variables, most of them are String and couple of others are BitSet type.

At runtime this list of objects is huge. It takes a lot of memory. I need to clone this list at one stage. But cloning this huge list is affecting application performance badly. Here's a sample of clone method I have written:



How can I clone class objects so that only bitsets are cloned instead of all other instance variables as well?


-Varun -
(My Blog) - Online Certifications - Webner Solutions
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You said that the object is made up of Strings and BitSets. The Strings can be assigned to the clone via normal reference assignment since they are immutable. This is the default behavior in Object.clone(), so you should be ready to go with the code you have. The Strings won't be cloned (they will have their references copied) and the BitSets will be deeply copied. Unless some superclass of yours overrode clone() to make deep copied of the Strings (which is completely unnecessary).


Steve
Rory Marquis
Ranch Hand

Joined: Nov 03, 2008
Posts: 48
If you do not want the Strings any more then you can just override clone in your object and clone the object and setting the strings to null.

However, as Steve said the clone will simply copy the reference to the String anyway so this wouldn't make any difference until you disposed of the original.

HTH
Ror
[ November 06, 2008: Message edited by: Rory Marquis ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Originally posted by Rory Marquis:
However, as Steve said the clone will simply copy the reference to the String anyway so this wouldn't make any difference until you disposed of the original.


And it wont make any difference when you dispose of the original either. The reference in your clone will still point at the String object.


Joanne
Rory Marquis
Ranch Hand

Joined: Nov 03, 2008
Posts: 48
Originally posted by Joanne Neal:


And it wont make any difference when you dispose of the original either. The reference in your clone will still point at the String object.


hi Joanne

Not if the String isn't cloned by the overriding clone() method, which is what I was suggesting Then the String would be GCed when the original List was, just leaved the cloned objects sans String.
[ November 06, 2008: Message edited by: Rory Marquis ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Rory Marquis:


hi Joanne

Not if the String isn't cloned by the overriding clone() method, which is what I was suggesting Then the String would be GCed when the original List was, just leaved the cloned objects sans String.

[ November 06, 2008: Message edited by: Rory Marquis ]


Incorrect. No Object would get GCed if there is at least one reference to the object. When you copy the reference into the new clone the Strings have 2 references to them (at least), the one in the original object and the second in the new object. When the original object gets GCed the Strings still have references to them and so can't be collected.
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
Thanks for your reply my friends. Yes that slipped out of my mind that immutables are not to be worried about.
[ November 07, 2008: Message edited by: Varun Chopra ]
Rory Marquis
Ranch Hand

Joined: Nov 03, 2008
Posts: 48
Originally posted by Steve Luke:


Incorrect. No Object would get GCed if there is at least one reference to the object. When you copy the reference into the new clone the Strings have 2 references to them (at least), the one in the original object and the second in the new object. When the original object gets GCed the Strings still have references to them and so can't be collected.


Hi Steve
What you said is correct.... However, If you would all READ my post I have said that if you override the clone method to do the clone and then SET THE STRING VALUE TO NULL, then the String would be GCed WHEN the original was GCed.

just override clone [method] in your object and clone the object and setting the strings to null.


I am well aware if how GC works.

[ November 07, 2008: Message edited by: Rory Marquis ]
[ November 07, 2008: Message edited by: Rory Marquis ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Yes, but Steve and I are referring to your second sentence
However, as Steve said the clone will simply copy the reference to the String anyway so this wouldn't make any difference until you disposed of the original.
where the clone does have a reference to the string, so disposing of the original will make no difference.
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211


Output:

s = null
cloned = Hello
Rory Marquis
Ranch Hand

Joined: Nov 03, 2008
Posts: 48
Originally posted by Joanne Neal:
Yes, but Steve and I are referring to your second sentence where the clone does have a reference to the string, so disposing of the original will make no difference.


Hi peeps
I didn't say it would have a reference to it, I said you set the String to null, so as to remove the reference! So if you read the whole thread the first thing I say is remove the reference by setting it to null, everything else I say is based on that answer. You write the clone method yourself, and inside that you set the String to null in the clone. Therefore only the original reference still remains.

In my second sentence, I am saying that if you used my way, that it wouldn't save you any memory until disposed of the original! 'this wouldn't make a difference' - "this" being my solution of making your own clone without the String:
However, as Steve said the clone will simply copy the reference to the String anyway so this wouldn't make any difference until you disposed of the original.


and in context:
However, as Steve said the [basic/original/non overidden] clone will simply copy the reference to the String anyway so this [setting the clone string to null] wouldn't make any difference [in comparison to the original way] until you disposed of the original [where upon the original string would be GCed and you would no longer have a reference to it thus freeing up memory].


Hope that clears everything up.

[ November 07, 2008: Message edited by: Rory Marquis ]
[ November 07, 2008: Message edited by: Rory Marquis ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Varun Chopra:


Output:

s = null
cloned = Hello


Hi Varun, what you showed is the right concept we are talking about. You shouldn't consider
String cloned = s;
a form of cloning. It isn't, it is just a reference copy and can only be substituted for cloning when the object in question is immutable.

A clone would create a new Object whose internals are the same as the original, where as a reference copy would keep just one single object but keep multiple references to it.

I just wanted to make sure I was clear and didn't make you think that a reference copy was a form of cloning.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Rory Marquis:


Hope that clears everything up.



It does now, but I want you to be fair - this was not at all clear until your last post here. Up until now it very much seemed like the 'this' you references in:
However, as Steve said the clone will simply copy the reference to the String anyway so this wouldn't make any difference until you disposed of the original.

referred only to the first part of the paragraph. Then in your second post when you write:
Not if the String isn't cloned by the overriding clone() method, which is what I was suggesting Then the String would be GCed when the original List was, just leaved the cloned objects sans String.

It sounds like you were saying that we would have to clone the String in order to keep them alive in the cloned object. Since none of the previous posts actually suggested cloning the String, mine only suggested letting the String references get copied, AND you never refer back to re-nulling the copies of the String, this again makes it easy to mis-interpret (or rather hard to interpret the way you meant it to be).

Why do I bother writing this? I don't want to dwell, and would normally have stopped at 'It does now', but it irks me when you repeatedly said 'read the thread' as if we hadn't and what you were saying was obvious if only we would read it. We did read the thread. What you said sounded wrong. Only now after you fill in the context of what was going on in your head (but not written in your posts) does it clear things up.

Sorry, this wasn't meant as a rant, I just wanted you to understand that you weren't really being fair when you repeatedly said 'if you would read the thread...'.
[ November 07, 2008: Message edited by: Steve Luke ]
Rory Marquis
Ranch Hand

Joined: Nov 03, 2008
Posts: 48
Originally posted by Steve Luke:

It sounds like you were saying that we would have to clone the String in order to keep them alive in the cloned object.

Yes, I can see that I wasn't as clear as I should have been there.


Since none of the previous posts actually suggested cloning the String, mine only suggested letting the String references get copied, AND you never refer back to re-nulling the copies of the String, this again makes it easy to mis-interpret (or rather hard to interpret the way you meant it to be).

You are correct, I should have referred back to myself. I didn't mean to cause confusion on something that should be simple


Why do I bother writing this? I don't want to dwell, and would normally have stopped at 'It does now', but it irks me when you repeatedly said 'read the thread' as if we hadn't and what you were saying was obvious if only we would read it.
We did read the thread. What you said sounded wrong. Only now after you fill in the context of what was going on in your head (but not written in your posts) does it clear things up.

Sorry, this wasn't meant as a rant, I just wanted you to understand that you weren't really being fair when you repeatedly said 'if you would read the thread...'.



I apologise for that, I was posting quickly. Although I did only ask to read my post once and to read the whole thread (as there was more than just my one post by then)once which I don't feel was "repeatedly", however, I can understand this irked you, which was not my intention.

I was also Irked because I do not like being being told "Incorrect" (which I thought was a bit blunt). That made me feel the thread can't have been read properly, if that makes sense.

However, I realise that my original post wasn't as clear as I had thought I had been. I shall try and be more verbose in order to avoid confusion, thank you for making that observation. Constructive criticism is always welcome.

The last thing I want to do on help forums to argue, so I am glad we were able to resolve this miscommunication.
[ November 07, 2008: Message edited by: Rory Marquis ]
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
code:





Output:

s = null
cloned = Hello



Hi Varun, what you showed is the right concept we are talking about. You shouldn't consider
String cloned = s;
a form of cloning. It isn't, it is just a reference copy and can only be substituted for cloning when the object in question is immutable.


Yes I understand that, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cloning Question