Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a class instance in a running JVM

Tags:

java

I am trying to figure out a way to find a class instance inside a working JVM. The JVM is embedded into another process and an object is created by this process. The same process executes my Java code. I do not have a direct access to the Java object reference in my Java code, but I know the class of that object. I want to find out if there are objects of this class already instantiated in JVM, and if they are, I want to find them (get say an Object array with references to all the instances of this class). This mechanism can be either a Java API or an JNI API.

I know it is possible since debuggers let me inspect all instances. I just cannot seem to find the way.

Thanks

Nikita

like image 264
Nikita Visnevski Avatar asked Nov 14 '22 08:11

Nikita Visnevski


1 Answers

In case anybody is curious, It is possible and I am doing it now using JVMTI. Not straightforward, but very doable. You can look at my other post about jvmti and you will find the answer.

Perplexed by jvmti object allocation callback behavior

Thanks

like image 62
Nikita Visnevski Avatar answered Nov 16 '22 03:11

Nikita Visnevski