In Java, programmers deal with objects. The memory required for an object is always allocated on the heap. The memory is allocated implicitly using the new operator. For example you have a class called
Computer comp = new Computer();
Depending on the definition of the
How it works?
Every Java object in memory has two areas:
- Header Area: It stores bookkeeping information to be used by the Java runtime, for example, the pointer to the object class, information about the garbage collection status of the object, locking information about the object, length of an array if the object is an array, etc.
- Data Area: It is used to store the values for all instance variables of the object. The header area layout is fixed for a particular JVM implementation whereas the data area layout is dependent on the object type.
Let’s see layout of an object in Java
The Java Hotspot virtual machine uses two machine-words (in 32-bit architecture one word is 4 bytes) for the object header.
If the object is an array, it uses three machine-words for its header. One extra word in the header is used to store the array length. However, most JVMs use three-machine words for an object header.
The Java Hotspot VM uses a variable length object header to save memory on the heap. Since most Java objects are small, one machine-word savings per object for non-array objects is a significant heap space savings. The Java Hotspot VM’s object header contains the following information:
- classptr: This is the first machine-word in the object layout. It contains a pointer to the class information of the object. The class information includes the object’s method table, the object’s size, and a pointer to a class structure, which contains information about the class of the object, etc.
- hash + age + lock: This is the second machine-word in the object header. It contains the object’s hash code, age information, and lock fields. Age information is used in the process of reclaiming the object’s memory by the generational garbage collector. The generational garbage collector is a special type of garbage collector that uses the object’s age in its algorithm to reclaim an object’s memory.
- arraylength: This is the third machine-word in the object header. It is included only if the object is an array. It contains the length of the array. In this case, the object’s data area contains the array elements.
In Java, all objects are created on heap. Java uses the new operator to allocate memory for an object on heap. An array’s length is not a part of its class definition. It is defined at runtime. It is stored in the object header. You will not find the length instance variable in the array’s class definition when you perform introspection on an array’s class.
Java does not provide any direct means to compute the size of an object. You should not write a Java program that depends on the size of the objects anyway. The size of primitive types, for example, int, long, double, etc. is fixed for all JVM implementations. The layout and size of an object depends on the JVM implementation. Therefore, any code that depends on the size of objects may work on one platform and not on others