1

Possible Duplicate:
How to get adress of a Java Object?
Memory address of variables in Java

Is there a way in Java to see or write the heap address of an object. Say I have a Long Object

Long mylong = Long.valueof(1);

I want to see the heap address of mylong instead of its value (1). I am just curious if it is possible! I know it is possible and may be it is not useful in the application. But I just looking for a way to know how the JVM is working in some cases.

If you a tool which do this like an eclipse plugin I will appreciate if you let me know about it!

trincot
  • 317,000
  • 35
  • 244
  • 286
Govan
  • 2,079
  • 4
  • 31
  • 53
  • 1
    There are some unsafe() methods. But they are not safe and are not fast either. They are used with some fields and the address is relative. Not absolute. Then you gotta stick with some exception handling too! The only gain is the use of each byte no matter what the type of variable is. Its something like malloc() but without a [i] usage. You use setters and getters which has an overhead. – huseyin tugrul buyukisik Aug 30 '12 at 14:05
  • 1
    Possible duplicate: http://stackoverflow.com/q/1961146/451590 – David B Aug 30 '12 at 14:05
  • I know it maybe is unsafe and not necessary but I just want know how the JVM is working. So I am not going to use it in a production. – Govan Aug 30 '12 at 14:16

2 Answers2

1

Is there a way in Java to see or write the heap address of an object

Short answer; no.

Long answer is; you don't want to as it's not useful. You can use Unsafe to get this information but the reference can change at any time.

Note: the reference is the address in 32-bit Java, but in 64-bit Java the reference is often multiplied by 8 and/or offset to allow the JVM to use 32-bit references for up to 32 GB of heap.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • do you know about a tool which can show me the current heap. I don't want to use it in a product! I just want to understand how the JVM is working. The unsafe method is not useful for me because I want to create the object exact as the jvm is doing – Govan Aug 30 '12 at 14:21
  • I would say the best tool is YourKit. You can get an eval license for free. It allows you to see the memory and cpu usage while the program is running, where objects are being created, and view a heap dump graphically. – Peter Lawrey Aug 30 '12 at 14:24
1
import java.lang.reflect.Field;

import sun.misc.Unsafe;

public class Direct {

    public static void main(String... args) {
        Unsafe unsafe = null;

        try {
            Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            unsafe = (sun.misc.Unsafe) field.get(null);
        } catch (Exception e) {
            throw new AssertionError(e);
        }

        long value = 12345;
        byte size = 1;
        long allocateMemory = unsafe.allocateMemory(size);
        unsafe.putAddress(allocateMemory, value);
        long readValue = unsafe.getAddress(allocateMemory);
        System.out.println("read value : " + readValue);

    }
}

Taken from: taken from here

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97