How to get generation sizes in JVM 1.4.2?
I've been poking around trying to find a way to get some heap usage statistics from inside a running 1.4.2 JVM. Basically what I need is:
1. Notification that a GC event has occurred (or a way to tell that one has occurred since the last time the code checked).
2. The sizes of the old (tenured) generation and the size of the permanent generation.
Is there any way to get at these statistics from inside a running Java program?
[454 byte] By [
spatula6a] at [2007-11-26 23:00:12]

# 1
Hi,
as far as I know there is no "official" API in 1.4.2 to do this (since 1.5 there are the Management APIs). But I had the same problem once and found a pretty easy solution: Put the "perf.jar" and "perfdata.jar" from the 1.4.2 jvmstat distribution into your classpath (I hope you can find the old download somewhere...).
The following piece of code demostrates how to access the perfdata from the JVM (note: with new VMIdentifier(0)
you access your own JVM, so running the little sample program without any argument (or argument "0") would introspect the own JVM).
import com.sun.jvmstat.perfdata.monitor.local.*;
import com.sun.jvmstat.monitor.*;
import java.util.*;
public class PerfDataReader {
public PerfDataReader() {
}
public void run(String[] args) {
try {
PerfDataBuffer perfDataBuffer = new PerfDataBuffer(new VMIdentifier((args.length == 0 ? "0" : args[0])));
System.out.println("VmId="+perfDataBuffer.getLocalVmId());
List l = perfDataBuffer.findByPattern(".*");
for (int i=0; i<l.size(); i++) {
Monitor m = (Monitor)l.get(i);
System.out.println(m.getName()+" = "+m.getValue());
}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
PerfDataReader main = new PerfDataReader();
main.run(args);
}
}
Hope this helps!
Nick.">
# 2
To add a bit to what Nick said.
Generational GC is one way to implement a GC and it would not make sense to add such implementation-specific information to standard APIs.
The monitoring and management API is supposed to allow you to find this information, but as Nick said it's available from 5.0 onwards.
Tony, HS GC Group
# 3
Thank you! That is exactly what I needed to know.Fortunately, this isn't for any kind of standard API or externally-facing tool. We have the luxury of control over our environment and get to dictate that a particular Sun Hotspot JVM is used.