Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > fastest way to make a copy of an object (with no references tooriginal)

Reply
Thread Tools

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

 
 
caultonpos
Guest
Posts: n/a
 
      06-18-2008
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
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      06-19-2008
caultonpos wrote:
> 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
 
Reply With Quote
 
 
 
 
mike.mainguy@gmail.com
Guest
Posts: n/a
 
      06-19-2008
On Jun 18, 7:15*pm, Arne Vajh°j <(E-Mail Removed)> wrote:
> caultonpos wrote:
> > 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


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.


 
Reply With Quote
 
ownowl
Guest
Posts: n/a
 
      06-19-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) a Úcrit :
> On Jun 18, 7:15 pm, Arne Vajh°j <(E-Mail Removed)> wrote:
>> caultonpos wrote:
>>> 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

>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fastest way to copy <SELECT> g_meney Javascript 0 02-24-2006 07:29 AM
What the fastest way to copy strings from file to a vector? SIgnOff C++ 7 03-21-2005 07:01 AM
fastest way to copy dvds George &The Dragon DVD Video 4 12-16-2004 11:51 PM
Fastest 5 mp Digital Camera ? Fastest 4 mp Digital Camera? photoguysept102004@yahoo.com Digital Photography 6 10-28-2004 11:33 AM
Fastest way to copy multiple small files TEX NZ Computing 4 06-14-2004 08:35 AM



Advertisments