Java Heap Profiling Between Two Events

I want to collect a bunch of using the program. In particular, from a specific trigger event to a final event and with a specified interval, for example, every 10 ms. I tried to implement something similar to myself, this is the following code. It works at slow intervals, for example, 1 second, but for lower intervals, for example 100 ms, it becomes buggy. Does the System.gc () method use to block all threads until it finishes? I am currently accepting this.

public void startAsync() { running = true; Thread thread = new Thread(new Runnable() { @Override public void run() { while (running) { measure(); try { Thread.sleep(wait); } catch (InterruptedException e) { throw new RuntimeException(); } } } }); thread.start(); } public void measure() { MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); memoryBean.gc(); long currentTime = System.currentTimeMillis(); MemoryUsage memoryUsage = memoryBean.getHeapMemoryUsage(); writer.exec(currentTime - startTime, memoryUsage.getUsed()); } 
+4
source share
5 answers

The numbers returned by java.lang.Runtime don't cost much - ignore them.

You will get better results from the management API, for example

 MemoryUsage memoryUsage = ManagementFactory.getMemoryMXBean().getMemoryUsage(); 

... and related methods that are likely to give you much more information than you ever wanted to know.

+2
source

YourKit excellent (no communication except satisfied customer.)

+2
source

Instead of wasting time on the command line, I suggest you take the exact measurements provided by NetBeans.

+1
source

You can do this from the command line using jmap -heap

http://download.oracle.com/javase/1.5.0/docs/tooldocs/share/jmap.html

1 line script to encode this every x seconds.

0
source

JProfiler is best for monitoring such issues.

0
source

All Articles