In Java, the size of an empty object is 8 bytes, which only saves the size of an object without any attribute in the heap. See the following statement:
- Object Ob =NewObject ();
This completes the life of a Java object in the program, but it occupies 4 bytes + 8 bytes. 4 byte is the space required to save the reference in the java stack mentioned above. The 8byte is the object information in the Java heap. Because all non-basic Java objects must inherit object objects by default, No matter what Java objects are, their size must be greater than 8 bytes.
With the object size, we can calculate the size of other objects.
- Class newobject {
- IntCount;
- BooleanFlag;
- Object ob;
- }
Its size is: null object size (8 bytes) + int size (4 bytes) + Boolean size (1 byte) + Null Object Reference size (4 bytes) = 17 bytes. However, because Java divides the object memory allocation by an integer multiple of 8, the size of the object is 24 because the nearest integer multiple of 8 is greater than 17 bytes.
In this article, we will discuss a question: how to calculate (or estimate) the amount of memory occupied by a Java object?
Generally, the premise of heap memory usage we are talking about is "General situation. It does not include the following two situations:
- In some cases, the JVM does not put the object into the heap at all. For example, in principle, a small thread-local object exists in the stack, rather than in the heap.
- The memory occupied by the object depends on the current state of the object. For example, whether the synchronization lock of an object takes effect or whether the object is being recycled.
Let's take a look at what a single object looks like in the heap.
In the heap, each object is composed of four fields (A, B, C, and D). Next we will explain one by one:
- A: The object header occupies a small number of bytes to indicate the current state of the object.
- B: space occupied by basic type fields (native fields refer to int, Boolean, short, etc)
- C: space occupied by the reference type domain (reference type domain refers to the reference of other objects, each reference occupies 4 bytes)
- D: space occupied by the filling material (what is the filling material)
Next we will explain A, B, C, and D one by one.
A: Object Header
In memory, the total space occupied by each object includes not only the space required by the variables declared in the object, but also some additional information, such as the object header and padding. The "Object Header" is used to record the Instance name, ID, and instance status of an object (for example, whether the current instance is "reachable" or the status of the current lock, etc ).
In the current JVM version (Hotspot), the number of bytes occupied by the "Object Header" is as follows:
- A common object that occupies 8 bytes
- Array, which occupies 12 bytes, including 8 bytes + 4 bytes of common objects (array length)
B: Basic Type
Boolean and byte occupy 1 byte, char and short occupy 2 bytes, int and float occupy 4 bytes, and long and double occupy 8 bytes
C: Reference Type
Each reference type occupies 4 bytes.
D: Filler
In hot spot, the total space occupied by each object is calculated in multiples of 8. When the total space occupied by the object (Object Header + declaration variable) is less than a multiple of 8, it is automatically completed. However, these filled spaces can be called "FILLING ". Let's take a look at the specific instance:
- An empty object (without any variables declared) occupies 8 bytes --> the object header occupies 8 bytes
- Only a class that declares a Boolean variable, occupying 16 bytes --> Object Header (8 bytes) + Boolean (1 bytes) + padding (7 bytes)
- Class that Declares 8 Boolean variables, occupying 16 bytes --> Object Header (8 bytes) + Boolean (1 bytes) * 8
The above instances help us better understand