1

I am a beginner in C and C++ and I don't know very well how to use the JVMTI. I want to get the physical memory location (in hexadecimal) of a java object. Is it possible? I don't want to get answers like "get it from this profiler etc" ... So far I am using this command in linux terminal :

java -showversion -agentpath:../CPrg/DLL/bin/Debug/libDLL.so SimpleMemAlloc

The libDLL.so is the library I am creating using c where I run it with my java application. It actually works, it runs with my code but I don't know how to get the data I want.

For example I am using this simple code:

public class SimpleMemAlloc{

    Object [] oarray = new Object[10000];

    public static void main(String args []){
        Object [] o= new Object[100];
        for (int i= 0; i<100; i++){
        o[i] = new Integer [10000];
        }
    }
}

I want to get the memory location (0x...) of every new integer array that I create... I will be very happy if I get a nice sample code using the jvmti

A.H.
  • 63,967
  • 15
  • 92
  • 126
dm_poy
  • 39
  • 1
  • 8
  • 1
    You realise that objects can be moved around in memory? (Although they can be temporarily pinned.) – Tom Hawtin - tackline Dec 28 '11 at 17:55
  • Yes I know that ... Is there any way I can do this in JVMTI ... I don't care if the next second they move around ... I just want to find their actual memory location at that specific moment. – dm_poy Dec 28 '11 at 18:05
  • I believe you don't need it actually. Anyway `sun.misc.Unsafe` is what you search. – Stan Kurilin Dec 28 '11 at 18:09
  • I've tried the Unsafe class but I couldn't find the memory location of an array... If I can with the Unsafe class I would be very happy if you show me how. But I believe jvmti is the answer... It might not make sense but I really need this, I am researching on something and this answer would be very helpfull for me – dm_poy Dec 28 '11 at 18:24

1 Answers1

0

If the object doesn't overwrite the hashCode() the default implementation returns the internal address of the object as a integer. Again this might not be the best way of doing this.

This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.

For more info click here

Dimitry
  • 4,503
  • 6
  • 26
  • 40
  • This is wrong. The identity hash code of an object is not an address. The identity hash code is constant, but the address can change. In particular, when the garbage collector copies the object to reclaim space. – Raedwald Jul 21 '14 at 22:35