Java gets the memory address of an object

foreword

We know that in Java, synchronizedthe current instance object is locked for the synchronization method.
For example the following piece of code:

import java.util.concurrent.TimeUnit;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Test test = new Test();
        test.helper();
    }

    public synchronized void helper() {
    
    
        try {
    
    
            System.out.println("hello");
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
}

After dumping, we can get the following information:

"main" #1 prio=5 os_prio=0 tid=0x00c46c00 nid=0x1d60 waiting on condition [0x00ddf000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
	at java.lang.Thread.sleep(Native Method)
	at java.lang.Thread.sleep(Thread.java:340)
	at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
	at Test.helper(Test.java:16)
	- locked <0x09db48c0> (a Test)
	at Test.main(Test.java:10)

The memory address of the instance test of Test is <0x09db48c0>. How do we verify that this address is our memory address?

text

Introduce dependencies

Add dependencies in pom.xml:

        <dependency>
            <groupId>org.openjdk.jol</groupId>
            <artifactId>jol-core</artifactId>
            <version>0.10</version>
        </dependency>

use

It's simple to use:

VM.current().addressOf(test)

Putting it in our code is:

import org.openjdk.jol.vm.VM;

import java.util.concurrent.TimeUnit;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Test test = new Test();
        System.out.println("The memory address is " + VM.current().addressOf(test));
        System.out.println("The memory address is " + Long.toHexString(VM.current().addressOf(test)));
        test.helper();
    }

    public synchronized void helper() {
    
    
        try {
    
    
            System.out.println("hello");
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }
}

In the console we can see the output:

The memory address is 165365952
The memory address is 9db48c0
hello

in conclusion

By introducing org.openjdk.jolwe can quickly get the memory address of the object. A general look at the relevant source code shows that the bottom layer is unsafejudged using classes.

JOL (Java Object Layout) is the tiny toolbox to analyze object layout schemes in JVMs. These tools are using Unsafe, JVMTI, and Serviceability Agent (SA) heavily to decoder the actual object layout, footprint, and references. This makes JOL much more accurate than other tools relying on heap dumps, specification assumptions, etc.

(Java Object Layout) is a small toolbox for analyzing object layout schemes in the jvm. These tools make heavy use of Unsafe, JVMTI, and Serviceability Agent (SA) to decode the actual object layout, memory footprint, and references. This makes JOL more precise than other tools that rely on heap dumps, specification assumptions, etc.

Guess you like

Origin blog.csdn.net/sayWhat_sayHello/article/details/119334690