Memory usage Calculation for Java object classes

Source: Internet
Author: User

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:

  1. 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.

  1. Class newobject {
  2. IntCount;
  3. BooleanFlag;
  4. Object ob;
  5. }

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:

 

  1. 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.
  2. 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:

 

  1. A: The object header occupies a small number of bytes to indicate the current state of the object.
  2. B: space occupied by basic type fields (native fields refer to int, Boolean, short, etc)
  3. C: space occupied by the reference type domain (reference type domain refers to the reference of other objects, each reference occupies 4 bytes)
  4. 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:

 

  1. A common object that occupies 8 bytes
  2. 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:

 

  1. An empty object (without any variables declared) occupies 8 bytes --> the object header occupies 8 bytes
  2. Only a class that declares a Boolean variable, occupying 16 bytes --> Object Header (8 bytes) + Boolean (1 bytes) + padding (7 bytes)
  3. Class that Declares 8 Boolean variables, occupying 16 bytes --> Object Header (8 bytes) + Boolean (1 bytes) * 8

The above instances help us better understand

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.