Take the 2-minute tour ×

I need to measure how much of memory do my JUnit tests consume.

  1. So the most obvious (but not convinient) way is to run JVM with arguments like "-Xms128m -Xmx512m" and track when I'll get the OutOfMemory error.

  2. The second way is to request a heapdump after running all my tests and then use Memory Analyzer Tool. But it isn't useful to do this every time I'm running tests.

The desired way is, let's say, to log memory consumption into a file with such values as maximum memory consumption, average consumption, GC calls count etc. Or even draw a diagram which will show how memory was used.

And the question is, are there any tools, or methods, or instruments to do this? Or, maybe, my wishes are unreal and naive and there is no way to gather such data?

Tests are running under IntelliJ Idea with JUnit4. I'm free to use any other environment. Thanks in advance for any advice!

share|improve this question
Well, if you use some Linux or Unix server, an easy way would be to make a shell that monitors your PID and print out the memory size at given intervals. You would have to start the program with a low -Xms and a high -Xmx. This way you would not monitor the exact size of your object heap, but at least the real consumption of your process. Another idea that occurs to me is to find out how ApplicationServers print GC information to log files and see if you can configure your setup to print "Verbose GC info". This normally includes detailed information which you can interpret later as needed. –  Martin Jun 13 '13 at 15:16
jvmmonitor.org for Eclipse, or just use jconsole, etc. –  Charlie Collins Jun 13 '13 at 15:17
Can't you just use a profiler, say from Eclipse? –  Bob Flannigon Jun 13 '13 at 15:17
Thanks for your answers! Ways to go with nix-servers are ok, but I better prefer something local. jvmmonitor and profiler are interesting, so I've started looking something the same but for IntelliJ IDEA. –  sainr Jun 14 '13 at 6:55

1 Answer 1

up vote 4 down vote accepted

You can use JConsole,


It comes with your JDK

share|improve this answer
Thank you, JConsole is quite useful! I wonder, whether is it possible to automatically run JConsole and connect to newly created process with unit-tests? –  sainr Jun 14 '13 at 6:52
I'm not fully sure, but I think it is not possible what you are mentioning. You can introduce a delay in your test in order to connect the JConsole to your JUnit process when the test is executed at first time. For example @Test public void delayTestForConnectJC(){Thread.sleep(30 * 1000);}. But you will have to connect manually JConsole with your JUnit process. –  Juan Jose Fidalgo Jun 14 '13 at 9:10
The idea with delay looks interesting, thank you! But anyway, I'll try to find something which will go automatically. –  sainr Jun 17 '13 at 9:29

Your Answer


