Using JavaAgent to test the size of objects
Author: Ma Bingbing http://www.mashibing.com
Object size (64 bit machine)
Observe virtual machine configuration
java -XX:+PrintCommandLineFlags -version
Common object
- Object header: markword 8
- ClassPointer: - XX:+UseCompressedClassPointers is 4 bytes, not open is 8 bytes
- Instance data
- Reference type: - XX:+UseCompressedOops is 4 bytes, not open is 8 bytes
Oops Ordinary Object Pointers
- Reference type: - XX:+UseCompressedOops is 4 bytes, not open is 8 bytes
- Padding alignment, multiple of 8
Array object
- Object header: markword 8
- The ClassPointer is the same as above
- Array length: 4 bytes
- Array data
- Align multiples of 8
experiment
-
New project ObjectSize (1.8)
-
Create file ObjectSizeAgent
package com.mashibing.jvm.agent; import java.lang.instrument.Instrumentation; public class ObjectSizeAgent { private static Instrumentation inst; public static void premain(String agentArgs, Instrumentation _inst) { inst = _inst; } public static long sizeOf(Object o) { return inst.getObjectSize(o); } }
-
Create META-INF/MANIFEST.MF under src directory
Manifest-Version: 1.0 Created-By: mashibing.com Premain-Class: com.mashibing.jvm.agent.ObjectSizeAgent
Note that the premain class line must be a new line (enter + line feed). Confirm that the idea cannot have any error prompt
-
Package jar files
-
Introduce the Jar package into projects that need to use the Agent Jar
project structure - project settings - library add the jar package -
The class of the Agent Jar is required at runtime. Add parameters:
-javaagent:C:\work\ijprojects\ObjectSize\out\artifacts\ObjectSize_jar\ObjectSize.jar
-
How to use this class:
```java package com.mashibing.jvm.c3_jmm; import com.mashibing.jvm.agent.ObjectSizeAgent; public class T03_SizeOfAnObject { public static void main(String[] args) { System.out.println(ObjectSizeAgent.sizeOf(new Object())); System.out.println(ObjectSizeAgent.sizeOf(new int[] {})); System.out.println(ObjectSizeAgent.sizeOf(new P())); } private static class P { //8 _markword //4 _oop pointer int id; //4 String name; //4 int age; //4 byte b1; //1 byte b2; //1 Object o; //4 byte b3; //1 } }
Hotspot turns on the rule of memory compression (64 bit machine)
- Below 4G, directly cut off the high 32 bits
- 4G - 32G, memory compression ClassPointers Oops is enabled by default
- 32G, invalid compression, 64 bit
The larger the memory, the better (-)
Problems with IdentityHashCode
Answer the question that white horse is not a horse:
After an object has calculated the identityHashCode, it cannot enter the bias lock state
https://cloud.tencent.com/developer/article/1480590
https://cloud.tencent.com/developer/article/1484167
https://cloud.tencent.com/developer/article/1485795
https://cloud.tencent.com/developer/article/1482500
Object positioning
•https://blog.csdn.net/clover_lily/article/details/80095580
- Handle pool
- Direct pointer
remarks
-
Object creation process
Load class - > linking - > initialization - > allocate memory - > assign default value - > attribute assignment - > construction method
-
Object memory layout (compressed)
General object
Object header 8 bytes
Type pointer 4 bytes
Attribute instance
panding
Array object
Object header 8 bytes
Type pointer 4 bytes
Array length
Array contents
panding
-
Object header content
According to different states,
state Other information Lock information ordinary hash gc age Lock flag: 01 bias bit: 0 Lightweight Locking Pointer on stack Lock mark: 00 Heavyweight lock Pointer Lock mark: 10 gc empty Lock mark: 11 Bias lock Thread, epoch, gc age Lock mark: 01 bias position: 1 -
Object size