This is the first day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Object creation
- class loading
- Class linking(verification, preparation, resolution)
- class initializing
- Allocates or allocates object memory
- Assign default values to member variables
- Calling a constructor
- Member variables are assigned their initial values in order
- Execute the constructor statement
The first three steps are done during class loading
The storage layout of objects in memory
Observe the VM configuration
java -XX:+PrintCommandLineFlags -version
-XX:InitialHeapSize=132670528 -XX:MaxHeapSize=2122728448 -XX:+PrintCommandLineFlags -XX:>+UseCompressedClassPointers + UseCompressedOops – – XX: XX: – UseLargePagesIndividualAllocation – XX: + UseParallelGC Java version “1.8.0 comes with _181” Java (TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, Mixed Mode)
Ordinary objects
-
Object header: Markword 8 bytes
-
ClassPointer pointer: – XX: + UseCompressedClassPointers is 4 bytes Don’t open is 8 bytes
-
The instance data
Reference type: -xx :+UseCompressedOops: 4 bytes if not enabled, 8 bytes
Int (INT); int (INT)
- Padding alignment, multiple of 8
The array object
- Object header: Markword 8
- The ClassPointer pointer is as above
- Array length: 4 bytes
- An array of data
- Align multiples of 8
Experiment (to see how many bytes an object occupies in memory)
When a class file is loaded to memory, the agent can intercept the information and modify it
-
ObjectSize (1.8)
-
Create file ObjectSizeAgent
package com.mashibing.jvm.agent;
import java.lang.instrument.Instrumentation;
public class ObjectSizeAgent {
private static Instrumentation inst;
// JVM call (fixed format)
public static void premain(String agentArgs, Instrumentation _inst) {
inst = _inst;
}
// Custom methods
public static long sizeOf(Object o) {
returninst.getObjectSize(o); }}Copy the code
- SRC create meta-INF/manifest.mf
Manifest-Version: 1.0
Created-By: mashibing.com
Premain-Class: com.mashibing.jvm.agent.ObjectSizeAgent
Copy the code
Note that the premain-class line must be a new line (carriage return + line feed), and make sure there is no error message for idea
-
Package JAR files
-
Introduce the Agent Jar package in the project that needs to use it
Project Structure-Project settings-library Adds the JAR package
- The runtime requires the class of the Agent Jar, with the following argument:
-javaagent:C:\work\ijprojects\ObjectSize\out\artifacts\ObjectSize_jar\ObjectSize.jar
Copy the code
7. How to use this class:
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()));
}
// How many bytes does an Object have
// -XX:+UseCompressedClassPointers -XX:+UseCompressedOops
// Oops = ordinary object pointers
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}}Copy the code
Hotspot turns on memory compression rules (64-bit machines)
You can see their muscles clearly. You can see their muscles clearly. You can see their muscles clearly.
The problem of IdentityHashCode
An object cannot enter a biased locked state after it has computed the identityHashCode
Cloud.tencent.com/developer/a…
Cloud.tencent.com/developer/a…
Cloud.tencent.com/developer/a…
Cloud.tencent.com/developer/a…
Object orientation
• blog.csdn.net/clover_lily…
- Handle to the pool
- Direct Pointers
What exactly does an object header contain
Markword 64
The figure below is for 32 bits and similar for 64 bits
- HashCode parts:
Question: Why is GC age 15 by default? (Max. 15)
Answer: the age of generation is 4bit, which can be expressed in the range 0-15