Best way to profile memory usage in a Java applica

2019-01-22 12:29发布

问题:

I realize that similar questions have been asked about this before here on SO, but let me describe exactly what I need to do:

I have a set of tests which run a command line java application and I'd like to add memory profiling to them. One option I see would be to add code (possibly using 3rd party tools/libraries) to my application that would provide a memory snapshot. Another option would be to use a third party tool which manages/instruments my application and the JVM for me (and ideally does not require me to change my code). I'm thinking of something like Valgrind but for Java. Also open source if at all possible.

What I'd really like to do is set up the memory tests so that my memory usage is being monitored at regular intervals, let's say every second, and dumped to a text file. That way I'd be able to see if the memory usage oscillates/increases/decreases over time. I'll also be able to calculate the max and min peaks.

Has anyone here done anything like this?

Thanks in advance.

回答1:

With something like JProfiler all you need to do is add certain parameters to the JVM. It uses JVMTI.

I think you should be reading up on profilers and exactly what they can do for you. I also suggest reading up on JVMTI.

The JVMTM Tool Interface (JVM TI) is a new native programming interface for use by tools. It provides both a way to inspect the state and to control the execution of applications running in the Java virtual machine (JVM). JVM TI supports the full breadth of tools that need access to JVM state, including but not limited to: profiling, debugging, monitoring, thread analysis, and coverage analysis tools.

Note: JVM TI replaces the Java Virtual Machine Profiler Interface (JVMPI) and the Java Virtual Machine Debug Interface (JVMDI). JVMPI and JVMDI will be removed in the next major release of J2SETM.



回答2:

Yourkit also has a pretty good profiler



回答3:

Have you checked

VisualVM and Eclipse-Callisto?



回答4:

A good place to start is to see if your JVM supports "java -Xrunhprof" as this can generate heap profiling information without making your scenario more complex.

See http://docs.oracle.com/javase/7/docs/technotes/samples/hprof.html

You might find that enough for you for starting.



回答5:

Several profiler's such as yourkit have API's for tracing memory allocations. Another option here are monitoring tools such as jxinsight or glassbox or jamon

For analyzing heap dumps theEclipse Memory Analyzer is the best tool you can get. It's free and open source, so you can automate the analysis of heap dumps as much as you want.



回答6:

I develop in Eclipse but I have Netbeans around to use its excellent Profiler. It is limited compared to some commercial ones but still good enough for spotting most bottlenecks



回答7:

You can use jrcmd which is a command line utility that comes with the JRockit JVM. If you know the pid of the Java process you can just do:

JROCKIt_HOME\bin\jrcmd <pid> print_object_summary

and it will give you:

31.8% 3198k    41907   -137k [C
11.9% 1196k      300     +0k [B
11.4% 1151k    49118     +6k java/lang/String
 6.1% 612k     5604     +0k java/lang/Class
 4.3% 431k     2388     +0k [I
 3.5% 353k    15097     +0k java/util/HashMap$Entry
 ...


回答8:

In addition to the above answers I also enjoyed using profiler a couple of years ago. Don't know if that helps.



回答9:

I do recommend dynaTrace production edition. Amazing tool to profile memory application. Low overhead and 100% of executed transactions.