Results 1 to 7 of 7
  1. #1
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default ArrayList pointer size java

    Where can I find out how large an arraylist entry is in bytes? Not the object it is pointing to, but the pointer itself. I've seen some computations for HashMap entries, but I'm not sure how much I trust them as they don't have sources. Else I might have to resort to creating small programs and heap dumping them. Ideas?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,768
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by senorbum View Post
    Where can I find out how large an arraylist entry is in bytes? Not the object it is pointing to, but the pointer itself. I've seen some computations for HashMap entries, but I'm not sure how much I trust them as they don't have sources. Else I might have to resort to creating small programs and heap dumping them. Ideas?
    Why do you care? You have gigabytes at your disposal. For what it's worth: most variables in your class or object take up four bytes except longs and doubles, they take eight bytes. When booleans, bytes, shorts or chars are stored in an array they take 1, 1, 2 and 2 bytes respectively. On 64 bit systems object refererences take eight bytes instead of four. Per object of any class there's about eight bytes of overhead. Compared to those gigabytes there's nothing to worry about.

    kind regards,

    Jos

  3. #3
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Why do you care? You have gigabytes at your disposal. For what it's worth: most variables in your class or object take up four bytes except longs and doubles, they take eight bytes. When booleans, bytes, shorts or chars are stored in an array they take 1, 1, 2 and 2 bytes respectively. On 64 bit systems object refererences take eight bytes instead of four. Per object of any class there's about eight bytes of overhead. Compared to those gigabytes there's nothing to worry about.

    kind regards,

    Jos
    Your assumption that there are 'gigabytes' to work with and awful. I'm clearly asking for a reason. We have a program that would be ideally stored in memory. Each of ~9 million unique strings contains at this point a set of 40-50 strings. So not only do we have to store 9 million strings (if they are interned), but we have 9x45 million references that need to be stored in memory. See how that adds up quickly? So the references/array list entries add up to be a lot of memory very quickly. Figuring out which data type to use could have a gigantic impact on the memory footprint.

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Here's how you can check (look up the actual APIs):

    1. Run garbage collection. Note that System.gc() doesn't guarantee it actually runs. you may want to do something like

    Java Code:
    while (true) {
      long memory = System.getAvailableMemory();
      System.gc();
      Thread.yield();
      Thread.sleep(10);
      long newMemory = System.getAvailableMemory();
      if(Math.abs(memory-newMemory) < 10000) {
        break;
      }
    }
    With this code, you keep trying to run garbage collection and watching the memory. Once the amount of available memory stops changing, we can assume things were fully collected.


    2. Create 1,000 arrays of 1,000,000 length of ArrayLists pointers (or whatever the numbers).

    3. Run garbage collection again using the algorithm above, and see the memory difference.

  5. #5
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    But note that the size of the pointer might be platform-dependent.

    you should expect at least 4 bytes, more likely 8

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,768
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by senorbum View Post
    Your assumption that there are 'gigabytes' to work with and awful. I'm clearly asking for a reason. We have a program that would be ideally stored in memory. Each of ~9 million unique strings contains at this point a set of 40-50 strings. So not only do we have to store 9 million strings (if they are interned), but we have 9x45 million references that need to be stored in memory. See how that adds up quickly? So the references/array list entries add up to be a lot of memory very quickly. Figuring out which data type to use could have a gigantic impact on the memory footprint.
    Why not use JavaDB/Derby? It comes bundles with your JDK 1.6 and it's completely free. It laughs about 9x45 million Strings.

    kind regards,

    Jos

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

    Default

    As said, the references themselves will be 4 or 8 bytes, depending on whether it's a 32 or 64 bit system.

    An ArrayList entry (presumably you mean the pointer to the object) willl therefore be 4 or 8 bytes. So an ArrayList will be approximately (on a 32 bit system) 4 bytes for the AL reference, 4 * size for the contents, plus some overhead. For large ALs the overhead will drop off to negligible pretty rapidly, so is largely irrelevant. I don't expect there to be an overhead for each entry.

    ETA: But, as Jos says, why not use a db of some description?

Similar Threads

  1. Null Pointer Exception when adding items to ArrayList
    By ShadowCopy in forum New To Java
    Replies: 3
    Last Post: 08-27-2009, 10:23 AM
  2. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 05:37 PM
  3. Java heap size
    By jmdrocks in forum Eclipse
    Replies: 2
    Last Post: 12-12-2008, 07:10 AM
  4. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  5. Limiting size of ArrayList
    By ravian in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 07:37 AM

Posting Permissions

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