以编程方式打印类直方图
有没有办法以编程方式打印当前java应用程序中最常用的N个类?
样本输出:N=10
num #instances #bytes class name
--------------------------------------
1: 23 4723136 [I
2: 19 4718928 [J
3: 18 4718880 [D
4: 73925 1774200 java.lang.String
5: 208 1226400 [C
6: 28 1205064 [B
7: 18 1179936 [F
8: 68 297040 [Ljava.lang.String;
9: 332 14136 [Ljava.lang.Object;
10: 32 10240 <objArrayKlassKlass>
is there any way to print top used N classes on the current java application programmatically?
sample output: N=10
num #instances #bytes class name
--------------------------------------
1: 23 4723136 [I
2: 19 4718928 [J
3: 18 4718880 [D
4: 73925 1774200 java.lang.String
5: 208 1226400 [C
6: 28 1205064 [B
7: 18 1179936 [F
8: 68 297040 [Ljava.lang.String;
9: 332 14136 [Ljava.lang.Object;
10: 32 10240 <objArrayKlassKlass>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
Runtime#exec
以编程方式运行jmap
,如下所示:输出将是:
You can use
Runtime#exec
programatically to run ajmap
like this:And the output will be:
我认为你不能在同一个 JVM 中完成它,因为你需要遍历对象堆,并且最终可能会陷入无限循环。出于好奇,我尝试在同一个 JVM 甚至不同的 JVM 上使用 Runtime.exec 生成 jmap,但它只是挂起?
I don't think you can do it within the same JVM because you need to traverse the object heap, and you might end up going in an infinite loop. Just out of curiosity, I tried spawning
jmap
usingRuntime.exec
against the same JVM and even against a different JVM and it just hangs?如果您的意思是最常用的初始化最多的类,您可以在构造函数周围定义切入点并跟踪每种类型的初始化。您可以使用 AspectJ 来解决这个问题。
If you mean by top used, classes that are initialized most, you can define a pointcut around the constructors and keep track of the initializations for each type. You can use AspectJ for that matter.
可能需要经过 JVM 工具接口 (JVM TI) 或篡改 Object 的实现(这是一件棘手的事情)。
这篇文章也许有用:使用 JVMTI 创建调试和分析代理。
Probably not without going through JVM Tool Interface (JVM TI) or tampering with the implementation of Object (which is a tricky business).
This article is perhaps useful: Creating a Debugging and Profiling Agent with JVMTI.
您可以启动
jmap
作为 java 包装器脚本的一部分,并在循环中连续运行它:例如,如果您在 Unix 上,您可以执行以下操作:
You could kick off
jmap
as part of your java wrapper script and run it continuously in a loop:For example, if you are on Unix, you could do something like: