Java Memory – Why the Overhead When Allocating Objects/Arrays?

javamemory

How many bytes an array occupies in Java? Assume It's a 64bit machine and also assume there are N elements in an array, so all these elements would take up 2*N, 4*N or 8*N bytes for different types of array.

And a lecture in Coursera says that it would occupy 2*N+24, 4*N+24 or 8*N+24 bytes for a N element array and the 24 bytes is called overhead, but didn't explain why the overhead is needed.

Also objects have overheads, which is 16 bytes.

What exactly are these overheads? What are these 24/16 bytes composed of?

Also, do these overheads only exist in Java? How about C, C++ and Python?

Best Answer

Each Java object has a header that contains information important for the JVM. The most important is a reference to the object's class (one machine word), and there are some flags used by the garbage collector and to manage synchronization (since every object can be synchronized on) which takes up another machine word (using partial words would be bad for performance). So that's 2 words, which is 8 bytes on 32 bit systems, and 16 bytes on 64 bit. Arrays additionally need an int field for the array length, which is another 4 bytes, possibly 8 on 64 bit systems.

As for other languages:

  • C doesn't have objects, so of course it doesn't have object headers - but may have a header on each separately allocated piece of memory.

  • In C++, you don't have garbage collection and cannot use arbitrary objects for synchronization, but if you have classes with overridden methods, each object has a pointer to its vtable, just like the Java object's reference to its class. If you use smart pointers that do garbage collection, they need housekeeping data.

  • I don't know about Python, but I'm pretty sure it also needs a reference to the class, and housekeeping information for the garbage collector.