简体   繁体   English

Java动态列出已分配的对象

[英]Java dynamically list allocated objects

I'm searching a way to find a list of allocated objects inside the JVM. 我正在寻找一种方法来查找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 先感谢您

You could achieve this with JVMTI . 您可以使用JVMTI实现此目的。 GetLoadedClasses function is a good entry point GetLoadedClasses函数是一个很好的切入点

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. 您还可以遍历堆 ,从而构建对象图。

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. 注意:使用已经存在的堆分析器是最简单的。 eg visualvm, An application can have many millions of objects so a tool designed to do this will help you navigate the data. 例如visualvm,一个应用程序可以拥有数百万个对象,因此设计用于执行此操作的工具将帮助您导航数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM