Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java dynamically list allocated objects

I'm searching a way to find a list of allocated objects inside the JVM.

I don't want to use a profiler as I want to see these object during runtime, inside the code itself. I wanna create a graph of all objects present inside the program and the interactions between each others.

Do you have a start of a way? I already searched for lots of reflection classes and profilers example but couldn't find something relevant for my case.

Thank you in advance

like image 859
Jikai Avatar asked May 15 '26 01:05

Jikai


2 Answers

You could achieve this with JVMTI. GetLoadedClasses function is a good entry point

JavaVM *jvm;
jvmtiEnv *jvmti;
jvmtiError err;

env->GetJavaVM(&jvm);
jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_2);

jint classCount = 0;
jclass * classes;

// get all classes loaded by jvm
jvmti->GetLoadedClasses(&classCount, &classes);

You can also traverse heap and thus build a graph of objects.

like image 177
vsminkov Avatar answered May 17 '26 15:05

vsminkov


Do you have a start of a way?

Yes, you can take a heap dump and analyse the heap dump.

Note: use a heap analyser which already exists would be simplest. e.g. visualvm, An application can have many millions of objects so a tool designed to do this will help you navigate the data.

like image 31
Peter Lawrey Avatar answered May 17 '26 15:05

Peter Lawrey