Some recent knowledge of the memory layout of Java objects is primarily about how much memory space a Java object occupies, and what the Java object looks like in memory. The sizeof operator in C + + can easily tell us which memory space a variable occupies, but this mechanism is not directly available in Java. If you want to get the memory size that Java objects occupy, you can take advantage of the Java instrumentation mechanism. Java.lang.instrument.Instrumentation This interface provides getobjectsize (object objecttosize) to tell us the size of an object. How to get instrumentation object, there is a lot of information on the Internet, no longer repeat here.
public static void Main (string[] args) {//Pre-agent gets instrumentation object Instrumentation instr = Agentgetter.getinstrumentation (); System.out.println (Instr.getobjectsize (New Object ()));}
memory layout of Java Objects: Object Header (header), instance data (Instance), and aligned padding (Padding). Both 32-bit and 64-bit hotspots use 8-byte alignment. This means that each Java object occupies an integer multiple of 8 bytes. (Object header + instance data + padding)% 8 equals 0 and 0 <= padding < 8. on the Internet to see a variety of articles on how to manually calculate the size of the article, summed up the points:
1. The number of bytes used by the base data type, as specified in the JVM specification, is the same as the memory size of a 32-bit or 64-bit virtual machine.
The 2.reference type consumes 4 bytes under a 32-bit JVM, but can consume 4 bytes or 8 bytes under 64 bits, depending on whether the pointer compression parameter usecompressedoops of the 64-bit JVM is enabled.
The 3.new object () represents 8 bytes on a 32-bit JVM and 16 bytes on a 64-bit JVM.
4. Turn on (-xx:+usecompressedoops) pointer compression, the object head is 12 bytes; Close (-xx:-usecompressedoops) pointer compression with Object header 16 bytes.
On a 5.64-bit JVM, the object header of an array object occupies 24 bytes, and 16 bytes is used after compression is enabled. More memory is consumed than ordinary objects because additional space is required to store the length of the array.
6. The 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 that class.
More specifically, you can refer to "How much memory does a Java object occupy?" "This article. In the process of searching the web, I saw the Java object Layout gadget, which can print out the layout information of the class.
The project's homepage is: http://openjdk.java.net/projects/code-tools/jol/,
The project code is in http://central.maven.org/maven2/org/openjdk/jol/.
After downloading Jol-core-0.3.2.jar, add it to the project's build path, and you can view the layout information for a class with 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 ());}}
running this code, you can see the Jol output the following information:
Running 64-bit HotSpot VM. Objects is 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 (object header) n/a 16 8 Long vo.b n /a 4 int vo.a N/a 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 byte s external = 4 bytes Total
you can see the Jol tool, which shows the size of the object header and the offset of each instance field, thus calculating the amount of memory the object occupies.
The Jol tool also provides a command-line tool, Jol-cli-0.3.2-full.jar, that contains the main method to run directly on the command line to view the layout information of the class. You can download the Jol-cli tool from here.
#查看jdk系统类的布局信息 >java-jar Jol-cli-0.3.2-full.jar Internals Java.lang.Object
#如果我们自己的jar放在C: \program Files\java\jdk1.7.0_80\jre\lib\ext\, then automatically loaded; #如果我们自己的jar放在C: \program files\java\jdk1.7.0 _80\jre\lib\, then it does not load automatically (possibly because of a security issue #,bootstrap the ClassLoader simply loads the core class of the JDK); >java-jar Jol-cli-0.3.2-full.jar Internals Net.aty.VO
#方式1: Specify Classpath, view your class layout >java-jar Jol-cli-0.3.2-full.jar internals-cp Mydemo.jar Net.aty.VO
#方式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
There are several ways to run correctly, but the following is a wrong way to get an error classnotfoundexception.
#注意: This will cause an error: Java.lang.ClassNotFoundException: #这是因为: If you run the executable jar package with the-jar parameter, the JVM ignores the classpath you set and all the environment variables JAVA-CP Mydemo.jar-jar Jol-cli-0.3.2-full.jar Internals Net.aty.VO
Memory layout of Java Objects (i): Calculating the memory space occupied by Java objects and the use of the Java Object Layout tool