Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learn about object overhead in JVM

I am studying java, and I remember reading somewhere that java objects, had some overhead inside the JVM, which was used for administration reasons by the virtual machine. So my question is, can someone tell me if and how I can get an object's total size in the HotSpot JVM, along with any overhead it may come with?

like image 755
NlightNFotis Avatar asked Jun 13 '12 09:06

NlightNFotis


People also ask

What is overhead in Java?

In computer science, overhead is any combination of excess or indirect computation time, memory, bandwidth, or other resources that are required to perform a specific task.

How does JVM store objects?

To store an object, JVM reserves enough space for its fields and service information. Service info includes reference to the class object and bits for synchronization. All accesses to objects, including class objects, is done via memory addresses. Hashcode and equals are not used by JVM during object lifecycle.

Where are the objects created in JVM?

All objects in Java programs are created on heap memory.

What is object header in Java?

The object header contains the following data in the order presented: Object ID (class ID) A 4-byte field containing a hexadecimal value known as the class ID of the object. The class ID stored in this field for a given object represents its most detailed classification.


1 Answers

You can't get the overhead directly. The amount of overhead is implementation dependent, and can vary based on a number of factors (e.g. the precise JVM version, and whether you are on a 32 or 64bit JVM).

However it is reasonably safe to assume that in typical modern JVM implementations like HotSpot, the overhead per object is between 8 and 16 bytes. Arrays typically have an overhead that is 4 bytes larger than other objects (to contain the integer array length).

See also:

  • In Java, what is the best way to determine the size of an object?
  • Memory usage of Java objects: general guide
like image 81
mikera Avatar answered Oct 18 '22 18:10

mikera