1001.3.jvm foundation 3JavaAgent_AboutObject

Keywords: Java jvm

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

  1. Object header: markword 8
  2. ClassPointer: - XX:+UseCompressedClassPointers is 4 bytes, not open is 8 bytes
  3. Instance data
    1. Reference type: - XX:+UseCompressedOops is 4 bytes, not open is 8 bytes
      Oops Ordinary Object Pointers
  4. Padding alignment, multiple of 8

Array object

  1. Object header: markword 8
  2. The ClassPointer is the same as above
  3. Array length: 4 bytes
  4. Array data
  5. Align multiples of 8

experiment

  1. New project ObjectSize (1.8)

  2. 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);
        }
    }
    
  3. 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

  4. Package jar files

  5. Introduce the Jar package into projects that need to use the Agent Jar
    project structure - project settings - library add the jar package

  6. The class of the Agent Jar is required at runtime. Add parameters:

    -javaagent:C:\work\ijprojects\ObjectSize\out\artifacts\ObjectSize_jar\ObjectSize.jar
    
  7. 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)

  1. Below 4G, directly cut off the high 32 bits
  2. 4G - 32G, memory compression ClassPointers Oops is enabled by default
  3. 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

  1. Handle pool
  2. Direct pointer

remarks

  1. Object creation process

    Load class - > linking - > initialization - > allocate memory - > assign default value - > attribute assignment - > construction method

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

  3. Object header content

    According to different states,

    stateOther informationLock information
    ordinaryhash gc ageLock flag: 01 bias bit: 0
    Lightweight Locking Pointer on stackLock mark: 00
    Heavyweight lockPointerLock mark: 10
    gcemptyLock mark: 11
    Bias lockThread, epoch, gc ageLock mark: 01 bias position: 1
  4. Object size

Posted by SWI03 on Mon, 18 Oct 2021 10:37:39 -0700