Memory layout of java objects (1): Calculate the memory space occupied by java objects and use of the java object layout tool

Source: Internet
Author: User

Memory layout of java objects (1): Calculate the memory space occupied by java objects and use of the java object layout tool

I recently learned some knowledge about the memory layout of java objects. I want to know how much memory space a java object occupies and what the java object looks like in the memory. The sizeof operator in c/c ++ can easily tell us the memory space occupied by a variable, but this mechanism is not directly provided in java. To obtain the memory size occupied by java objects, you can use the Instrumentation mechanism of java. Java. lang. instrument. Instrumentation this interface provides getObjectSize (Object objectToSize), which can tell us the size of an Object. There is a lot of information on the Internet about how to obtain Instrumentation objects.

 

Public static void main (String [] args) {// obtain the Instrumentation object Instrumentation instr = AgentGetter through the pre-agent. getInstrumentation (); System. out. println (instr. getObjectSize (new Object ()));}

Memory layout of Java objects: Object headers, Instance Data, and Padding ). Both 32-bit and 64-bit hot spot use 8-byte alignment. That is to say, each java object occupies an integer multiple of 8 bytes. (Object Header + instance data + padding) % 8 is equal to 0 and 0 <= padding <8. I have seen various articles on How to manually calculate the object size on the Internet, which summarize the following points:

 

1. The number of bytes occupied by the basic data type. The JVM specification clearly specifies that the memory size is the same for 32-bit or 64-bit virtual machines.
2. the reference type occupies 4 bytes in 32-bit JVM, but may occupy 4 or 8 bytes in 64-bit JVM, this depends on whether the 64-bit JVM pointer compression parameter UseCompressedOops is enabled.
3. The new Object () takes 8 bytes on the 32-bit JVM and 16 bytes on the 64-bit JVM.
4. Enable (-XX: + UseCompressedOops) pointer compression. The object header occupies 12 bytes. Disable (-XX:-UseCompressedOops) pointer compression, and the object header occupies 16 bytes.
On the 5.64-bit JVM, the object header of the array object occupies 24 bytes, and 16 bytes after compression is enabled. The reason for occupying more memory than ordinary objects is that extra space is required to store the length of the array.
6. instance data in the object memory layout does not include the size of the static field of the class, because the static field belongs to the class and is shared by all objects of the class.

 

For more details, refer to "How much memory does a Java object occupy ?" This article. Java object layout is a small tool during online search, which can print the layout information of the class.

Project home page: http://openjdk.java.net/projects/code-tools/jol,

The project code is available at http://central.maven.org/maven2/org/openjdk/jol /.

 

After the jol-core-0.3.2.jar is downloaded, add it to the build path of the project and view the layout information of a class through the following code.

 

package jol;import org.openjdk.jol.info.ClassLayout;import org.openjdk.jol.util.VMSupport;public class T1 {public static void main(String[] args) throws Exception {System.out.println(VMSupport.vmDetails());        System.out.println(ClassLayout.parseClass(VO.class).toPrintable());}}
Run this code and you can see that jol outputs the following information:

 

 

Running 64-bit HotSpot VM.Objects are 8 bytes aligned.Field sizes by type: 8, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]Array element sizes: 8, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]jol.VO object internals: OFFSET  SIZE  TYPE DESCRIPTION                    VALUE      0    16       (object header)                N/A     16     8  long VO.b                           N/A     24     4   int VO.a                           N/A     28     4       (loss due to the next object alignment)Instance size: 32 bytes (estimated, the sample instance is not available)Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
The jol tool displays the size of the object header and the offset of each instance field, and calculates the memory occupied by the object.

The jol tool also provides a command line tool jol-cli-0.3.2-full.jar that contains the main method that can run directly on the command line to view the layout information of the class. You can download the jol-cli tool from here.

 

 

# View jdk system class layout information> java-jar jol-cli-0.3.2-full.jar internals java. lang. Object

 

 

# If we place our own jar Files in C: \ Program Files \ Java \ jdk1.7.0 _ 80 \ jre \ lib \ ext \, it will be automatically loaded; # If we place our own jar Files in C: \ Program Files \ Java \ jdk1.7.0 _ 80 \ jre \ lib \, it will not be automatically loaded (probably because of security issues #, bootstrap Class Loader only loads jdk core classes);> java-jar jol-cli-0.3.2-full.jar internals net. aty. VO

 

 

# Method 1: Specify classpath, view your own class layout> java-jar jol-cli-0.3.2-full.jar internals-cp mydemo. jar net. aty. VO

 

# Method 2: Specify classpath, view your class layout> java-cp mydemo. jar; jol-cli-0.3.2-full.jar org. openjdk. jol. Main internals net. aty. VO

The preceding methods can run correctly, but the following method is incorrect and the ClassNotFoundException is reported.

 

# Note: an error occurs in this method: java. lang. classNotFoundException: # This is because: if you use the-jar parameter to run the executable jar package, JVM will ignore the classpath you set and all the environment variables java-cp mydemo. the jar-jar jol-cli-0.3.2-full.jar internals net. aty. VO

 

Related Article

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.