简体   繁体   English

如何剖析Java中的对象创建?

[英]How to profile object creation in Java?

The system I work with is creating a whole lot of objects and garbage collecting them all the time which results in a very steeply jagged graph of heap consumption. 我正在使用的系统正在创建大量对象并一直对其进行垃圾收集,这导致了堆消耗的锯齿状曲线。 I would like to know which objects are being generated to tune the code, but I can't figure out a way to dump the heap at the moment the garbage collection starts. 我想知道正在生成哪些对象来优化代码,但是在垃圾回收开始时,我想不出一种方法来转储堆。 When I tried to initiate dumpHeap via JConsole manually at random times, I always got results after GC finished its run, and didn't get any useful data. 当我尝试通过JConsole手动手动启动dumpHeap时,我总是在GC完成运行后得到结果,而没有得到任何有用的数据。

Any notes on how to track down excessive temporary object creation are welcome. 欢迎提供有关如何追踪过多临时对象创建的任何注释。

What you're looking for are the most intensive allocation sites in your program. 您正在寻找的是程序中最密集的分配站点。 You can use a tool like: Allocation Instrumentor for Java . 您可以使用以下工具: Java分配工具 The answer to your comment: 您的评论的答案:

When I tried to initiate dumpHeap via JConsole manually at random times, I always got results after GC finished its run, and didn't get any useful data. 当我尝试通过JConsole手动手动启动dumpHeap时,我总是在GC完成运行后得到结果,而没有得到任何有用的数据。

is that a heap dump triggers a GC in the VM because the heap dump is a report of what is live in the heap (for the most part) - the VM wants the most accurate picture of what is live at the time the heap dump was triggered and therefore a GC event will always happen right after the dump request is initiated. 的原因是堆转储会触发VM中的GC,因为堆转储是(大部分情况下)堆中活动内容的报告-VM希望获得堆转储时活动内容的最准确信息触发,因此GC事件将始终在启动转储请求后立即发生。

Tuning the Java heap is an art - and there is plenty of material out on the web on how to do it. 调优Java堆是一门艺术-网上有很多关于如何做到这一点的资料。

看一下BTrace可以做什么( http://kenai.com/projects/btrace/pages/Home ),或者尝试在JDK 6u18中使用jvisualvm进行实时内存采样。

Apache JMeter Apache JMeter

Quote from the projects page: 从项目页面引用:

Apache JMeter may be used to test performance both on static and dynamic resources (files, Servlets, Perl scripts, Java Objects, Data Bases and Queries, FTP Servers and more). Apache JMeter可用于测试静态和动态资源(文件,Servlet,Perl脚本,Java对象,数据库和查询,FTP服务器等)的性能。 It can be used to simulate a heavy load on a server, network or object to test its strength or to analyze overall performance under different load types. 它可用于模拟服务器,网络或对象上的重负载,以测试其强度或分析不同负载类型下的整体性能。

I remember M. Fowler mentioned that it's almost impossible to guess what causes performance issues until you test the whole system with a profiler. 我记得M. Fowler提到过几乎不可能猜测导致性能问题的原因,除非您使用探查器测试整个系统。

You could use the JVMPI interface (and tools like Dr.MEM) and create your own custom solution if non of the solutions prescribed above solve your purpose. 如果以上规定的解决方案均无法解决您的目的,则可以使用JVMPI接口 (和Dr.MEM等工具)并创建自己的自定义解决方案。 This could however be an overkill depending on your problem. 但是,根据您的问题,这可能是一个过大的杀伤力。

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

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