Results 1 to 4 of 4
  1. #1
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Question The mos memory and disk efficient way to serialize a part of an array of primitives?

    Hi,

    With ObjectOutputStream, I know that I can serialize a whole array of primitives by calling ObjectOutputStream::writeObject(Object obj). I assume that the writeObject method for arrays of primitives does not simply call the write method of ObjectOutputStream for the appropriate primitive type in a loop because we know that the elements of these arrays all have the same type and we don't want their class ID to be written for every single element of the array. So I would like to know, is there a way to save only a part of such an array of primitives without having to copy it in a smaller array first and while maintaining the serialization "efficiency" of the default primitive array serialization method?

    Thanks!
    Last edited by kreyszig; 10-14-2010 at 10:53 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Not too sure you can.
    There doesn't appear to be a subArray() method that would simply point to a section of the array rather than copy it. Strings substring() does that, using the parent String backing array, but there doesn't seem to be the same sort of thing.

    You might be able to copy that functionality? Except I suspect some of it might be native.

    Do you really need to avoid making a copy? It's a pretty quick operation I think, though if the array is big then you might get a temporary hefty footprint in the JVM. The thing is it doesn't sound like this is going to be a longlived copy, so thatll be eligible for gc pretty quickly.

  3. #3
    kreyszig is offline Member
    Join Date
    Oct 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Not too sure you can.
    There doesn't appear to be a subArray() method that would simply point to a section of the array rather than copy it. Strings substring() does that, using the parent String backing array, but there doesn't seem to be the same sort of thing.
    More specifically I only want to serialize less elements (without any offset for the first element)

    Quote Originally Posted by Tolls View Post
    You might be able to copy that functionality? Except I suspect some of it might be native.
    Yes I think that some of it is either native, or uses functionalities that are not available to the users of the API...

    Quote Originally Posted by Tolls View Post
    Do you really need to avoid making a copy? It's a pretty quick operation I think, though if the array is big then you might get a temporary hefty footprint in the JVM. The thing is it doesn't sound like this is going to be a longlived copy, so thatll be eligible for gc pretty quickly.
    So there is no way to cast an int[] to a byte[] either in Java right (obviously without allocating more memory)? You are right that this array would be only temporary, but it would be quite big and I would prefer not having to copy it. There is nothing equivalent to the C realloc function either in Java right (i.e. to reduce the size of for an array without copying it for example)? In some situations I find realloc very helpful in C++. I even use it combined with the placement new operator sometimes for more efficiency...

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    int[] is not the same class as a byte[], so no you can't cast between them.
    I can't think of anything, without you essentially writing the outputstream yourself...which I would argue is worse than simply copying the array.

    I'd do the copy and then revisit if that proves to be a problem.

Similar Threads

  1. How to find out if part of an array is empty
    By counterfox in forum New To Java
    Replies: 2
    Last Post: 10-13-2010, 06:09 PM
  2. Memory representation of an array?
    By hellolleh in forum Forum Lobby
    Replies: 2
    Last Post: 04-29-2010, 03:05 AM
  3. Replies: 1
    Last Post: 04-15-2010, 05:40 PM
  4. Drawing graphics primitives
    By diegofsza in forum Java 2D
    Replies: 7
    Last Post: 09-24-2009, 11:10 PM
  5. Replies: 0
    Last Post: 08-19-2008, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •