Live data from Hacker News

Building Memory-Efficient Java Applications (2009) [pdf]

cs.virginia.edu

11–14 of 14 posts

Re: Building Memory-Efficient Java Applications (2009) [pdf]

#11
Please correct me if I'm wrong...

Q: What is the size ratio of Integer to int?

An int is 4 bytes, an Integer additionally has a 2 word object header (8 bytes) and a 4 byte reference pointing to it. This adds up to 12 bytes for the object, which is rounded up to 16 bytes, because Hotspot aligns objects to 8 byte boundaries. Including the reference, the Integer needs 20 bytes, or 5 times as much as an int.

Q: How many bytes in an 8-character String?

  * object header: 8 bytes
  * reference to char[] storing the value: 4 bytes
  * int field to cache hashcode: 4 bytes
  -> 16 bytes for "wrapper"
  * char[] storing the value:
      8 characters in UTF16: 16 bytes
      object header: 8 bytes
      length field: 4 bytes length
      -> 28 bytes, rounded up to 32 bytes
  total: 48 bytes

Q: Which of the following is true about HashSet relative to HashMap

d. does less, bigger: HashSet is a wrapper around HashMap

Q: Put the following 2-element collections in size order: ArrayList, HashSet, LinkedList, HashMap

Excluding the size of the actual elements:

  * ArrayList, created with initial capacity of 2 (vs. default of 10):
      * object header: 8 bytes
      * size int: 4 bytes
      * reference to Object[] holding element data: 4 bytes
      -> 16 bytes for wrapper
      * element data with 2 pointers: 2*4 bytes + 12 bytes, rounded
        up to 24 bytes
      -> 40 bytes total

  * LinkedList:
      * object header: 8 bytes
      * size int: 4 bytes
      * references to first + last node: 8 bytes
      -> 20 bytes, rounded up to 24 bytes
      * nodes (2x):
          * object header: 8 bytes
          * references to previous and next item: 8 bytes
          * reference to element data: 4 bytes
          -> 20 bytes, rounded up to 24
      -> 72 bytes total

  * HashMap, created with initial capacity of 2 (vs. 16) and default
    load factor of 0.75
      * object header: 8 bytes
      * modification count int: 4 bytes
      * size int: 4 bytes
      * load factor float: 4 bytes
      * entry set: ?
      * resizing threshold int: 4 bytes
      * pointer to hash table: 4 bytes
      -> 28 bytes for wrapper, rounded up to 32 bytes
      * hash table (Node[]):
          * object header: 8 bytes
          * length field: 4 bytes
          * pointers: 4 * 4 bytes
          -> 28 bytes, rounded up to 32 bytes
      * Node objects (2x):
          * object header: 8 bytes
          * hashcode int: 4 bytes
          * pointer to next node in chain: 4 bytes
          * pointer to element data: 4 bytes
          -> 20 bytes, rounded up to 24 bytes
      -> total of 120 bytes

  * HashSet, created with initial capacity of 2 (vs. 16) and default
    load factor of 0.75
      * object header: 8 bytes
      * pointer to HashMap: 4 bytes
      -> 12 bytes for wrapper, rounded up to 16
      * HashMap: 120 bytes
      -> 132 bytes total

Re: Building Memory-Efficient Java Applications (2009) [pdf]

#12
post #3

is the talk available?

Not from the same talk, but the same conference so probably not?

"Comment from Dave Mandelin Time: January 25, 2010, 2:15 pm

I asked the conference organizers and unfortunately they didn’t videorecord the talks."[1]

[1] https://blog.mozilla.org/dmandelin/2009/06/02/tracemonkeypld...

Post reply on HN