JDK 1.5 contains a number of useful interfaces to monitor the performance of the JVM. This includes
support for both Memory and CPU usage. These metrics are exposed via JMX as well as via plain interfaces
in the JDK.
The code below iterates over the different elements in the memory pool and prints out their
current statistics.
List mpMBList = ManagementFactory.getMemoryPoolMBeans(); for (Iterator iter = mpMBList.iterator(); iter.hasNext(); ) { MemoryPoolMBean element = (MemoryPoolMBean) iter.next(); System.out.println(element.getName() + " " + element.getPeakUsage()); }
Output:
Code Cache initSize = 196608, used = 450112, committed = 458752, maxSize = 33554432 Eden Space initSize = 524288, used = 518712, committed = 524288, maxSize = -1 Survivor Space 1 initSize = 65536, used = 0, committed = 65536, maxSize = -1 Survivor Space 2 initSize = 65536, used = 65528, committed = 65536, maxSize = -1 Tenured Gen initSize = 1441792, used = 42944, committed = 1441792, maxSize = 61997056 Perm Gen initSize = 8388608, used = 1718928, committed = 8388608, maxSize = 67108864
The information you get is similar to what you can get using the
JVM Stat tool in Sun's
JDK 1.4.2 VM.