fastest way to make a copy of an object (with no references tooriginal)

C

caultonpos

Hi,

I keep a local cache of objects for performance reasons and pull
objects from that cache instead of the server. The Swing client has a
configuration to how much memory is allocated for that cache.
If those objects are modified the cache is not updated (for a number
of reasons).

Currently I get the object from the server and use a byte stream to
make a copy of the object which is placed in the cache. I can use the
byte array size to determine the memory allocated to the object.

ByteArrayOutputStream byteArray = new
java.io.ByteArrayOutputStream(defaultByteArraySize);
out = new ObjectOutputStream(byteArray);
out.writeObject(o);
return byteArray.toByteArray();

When I need the object from the cache I deserialize from the bytes

bis = new ByteArrayInputStream(bytes);
in = new ObjectInputStream(bis);
Object o = in.readObject();

Is this the fastest way to make a (true) copy and then retrieve the
copy (while staying within a fix size of memory)?

thanks!

Greg
 
A

Arne Vajhøj

caultonpos said:
I keep a local cache of objects for performance reasons and pull
objects from that cache instead of the server. The Swing client has a
configuration to how much memory is allocated for that cache.
If those objects are modified the cache is not updated (for a number
of reasons).

Currently I get the object from the server and use a byte stream to
make a copy of the object which is placed in the cache. I can use the
byte array size to determine the memory allocated to the object.

ByteArrayOutputStream byteArray = new
java.io.ByteArrayOutputStream(defaultByteArraySize);
out = new ObjectOutputStream(byteArray);
out.writeObject(o);
return byteArray.toByteArray();

When I need the object from the cache I deserialize from the bytes

bis = new ByteArrayInputStream(bytes);
in = new ObjectInputStream(bis);
Object o = in.readObject();

Is this the fastest way to make a (true) copy and then retrieve the
copy (while staying within a fix size of memory)?

I would expect clone to be faster than serialization and deserialization
and object to use much less memory than serialized objects.

Arne
 
M

mike.mainguy

I would expect clone to be faster than serialization and deserialization
and object to use much less memory than serialized objects.

Arne

Clone only does a shallow copy and I think only work if the classes
implement Cloneable. I guess we'd need to know your requirements a
little better before we can give any good answer.

I'd take a look at the real differences between those two methods. A
better way might be injecting a proxy that has some custom super-duper
class representation that is easy to copy. This would work if the
domain of possible classes that need to be copied is fairly small and
well defined, but should certainly only be reserved for extreme
cases.
 
O

ownowl

(e-mail address removed) a écrit :
Clone only does a shallow copy and I think only work if the classes
implement Cloneable. I guess we'd need to know your requirements a
little better before we can give any good answer.

I'd take a look at the real differences between those two methods. A
better way might be injecting a proxy that has some custom super-duper
class representation that is easy to copy. This would work if the
domain of possible classes that need to be copied is fairly small and
well defined, but should certainly only be reserved for extreme
cases.

clone method can make a deep copy, but for this, you must write a clone
method of all of your classes that contains some objects like List, Map,
or anothers complex objects . But clone is really faster, by making a
copy of block memory to another.

Olivier
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top