How to profile object creation in Java?

2019-06-28 01:51发布

问题:

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.

Any notes on how to track down excessive temporary object creation are welcome.

回答1:

Have a look at what BTrace can do (http://kenai.com/projects/btrace/pages/Home), alternatively try using jvisualvm in JDK 6u18 which does live memory sampling.



回答2:

What you're looking for are the most intensive allocation sites in your program. You can use a tool like: Allocation Instrumentor for 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.

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.

Tuning the Java heap is an art - and there is plenty of material out on the web on how to do it.



回答3:

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). 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.



回答4:

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. This could however be an overkill depending on your problem.