Saturday, February 16, 2013

Oops! Where did my memory go?

Most Java programmers know the typical amount of memory used by primitives (ints take 4 bytes, doubles 8 etc) but what about objects?

Well, primitive arrays take up 24 bytes plus N times the size of the primitive (where N is the number of elements) [1].

Objects typically have an overhead of 16 bytes and are padded to the nearest 8 bytes. Their references to other objects typically take 8 bytes each. Furthermore, each inner class needs 8 bytes for its pointer to the enclosing class [1].

Pointers can be compressed. The reason we might want to do this is that "memory is pretty cheap, but these days bandwidth and cache is in short supply" [2].

[1] Prof Robert Sedgewick, Coursera.
[2] CompressedOops.

No comments:

Post a Comment