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

Discussion in 'Java' started by caultonpos, Jun 18, 2008.

  1. caultonpos

    caultonpos Guest

    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
     
    caultonpos, Jun 18, 2008
    #1
    1. Advertising

  2. caultonpos

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Jun 19, 2008
    #2
    1. Advertising

  3. caultonpos

    Guest

    On Jun 18, 7:15 pm, Arne Vajhøj <> 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.
     
    , Jun 19, 2008
    #3
  4. caultonpos

    ownowl Guest

    a écrit :
    > On Jun 18, 7:15 pm, Arne Vajhøj <> 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
     
    ownowl, Jun 19, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SIgnOff
    Replies:
    7
    Views:
    608
    SIgnOff
    Mar 21, 2005
  2. Alex
    Replies:
    2
    Views:
    1,237
  3. Jack Bates
    Replies:
    3
    Views:
    309
    Steven D'Aprano
    Aug 6, 2011
  4. Magicloud Magiclouds

    What is the fastest way to convert a object?

    Magicloud Magiclouds, Jul 17, 2007, in forum: Ruby
    Replies:
    13
    Views:
    169
    Magicloud Magiclouds
    Jul 17, 2007
  5. g_meney

    Fastest way to copy <SELECT>

    g_meney, Feb 24, 2006, in forum: Javascript
    Replies:
    0
    Views:
    91
    g_meney
    Feb 24, 2006
Loading...

Share This Page