views:

1256

answers:

8

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.

+5  A: 

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.

Peter D
Thanks! The JVM TI looks like promising.
Matt
+2  A: 

Have you checked

VisualVM and Eclipse-Callisto?

Liran Orevi
+3  A: 

Yourkit also has a pretty good profiler

Conrad
+2  A: 

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.

kohlerm
+1  A: 

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

uriDium
+1  A: 

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://java.sun.com/developer/technicalArticles/Programming/HPROF.html

You might find that enough for you for starting.

Thorbjørn Ravn Andersen
+1  A: 

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

Matt Large
The netbeans profiler has been put into the VisualVM analyzer with Java 6 u10. It is VERY nice!
Thorbjørn Ravn Andersen
+1  A: 

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
 ...
Kire Haglin
That looks promising - thanks!
Matt