Alex Rivera | Logout

Best way to profile memory usage in a Java application?

Asked 2009-04-16T16:11:23.690
24

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.

Edit
Report

5 Answers

4

Have you checked

VisualVM and Eclipse-Callisto?

answered 2009-04-16T16:24:31.537
3

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.

answered 2009-04-16T17:43:54.030
2

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
 ...
answered 2009-04-26T10:40:12.377
1

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

answered 2009-04-16T16:36:17.983
1

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

answered 2013-03-11T14:36:36.850

Your Answer