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?

  • 2
    Check out: stackoverflow.com/a/258150/1029272 – Deco Aug 28 '12 at 3:52
  • 1
    @Gnijuohz: Do you mean to ask: what data is this overhead composed of? – FrustratedWithFormsDesigner Aug 28 '12 at 4:28
  • @YannisRizos: I think the OP wants to know what is actually in those 24 bytes, for arrays. – FrustratedWithFormsDesigner Aug 28 '12 at 4:29
  • @FrustratedWithFormsDesigner Ah, that seems to be a better interpretation of the question than mine. – yannis Aug 28 '12 at 4:34
  • @YannisRizos sorry about my bad attitude. But when you post that link I can't help but think that's some kind of sarcasm. Too defensive, I guess. – Gnijuohz Aug 28 '12 at 4:36

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.

  • There is work happening in OpenJDK at the moment to reduce the size of object headers, small but important steps :-) – Martijn Verburg Aug 28 '12 at 8:54
  • In C++, only polymorphic classes need vtables. std::pair<int, float> is a simple class which doesn't need a vtable at all. As a result, it may very well fit in 8 bytes. Also, smart pointers do not actually need to add housekeeping. A clear counter-example is std::unique_ptr<T>, which typically is just as big as the raw T* (unique_ptr of course doesn't do GC). – MSalters Aug 28 '12 at 16:04
  • 3
    C also has an overhead, each malloc-allocated block of memory needs a header which free then uses. – herby Aug 28 '12 at 16:31
  • At least one malloc library that I know of uses an 8-byte header on 32-bit systems (which is a 4-byte length bracketed by two sets of 2-byte sentinel values, IIRC). – Donal Fellows Aug 28 '12 at 21:12

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.