import java.io.*; import java.util.*; import sun.jvm.hotspot.memory.*; import sun.jvm.hotspot.oops.*; import sun.jvm.hotspot.debugger.*; import sun.jvm.hotspot.runtime.*; import sun.jvm.hotspot.tools.*; import sun.jvm.hotspot.utilities.*; public class DirectMemorySize extends Tool { public void run() { // Ready to go with the database... try { long reservedMemory = getStaticLongFieldValue("java.nio.Bits", "reservedMemory"); long directMemory = getStaticLongFieldValue("sun.misc.VM", "directMemory"); System.out.println("NIO direct memory:"); System.out.printf(" reserved size = %fMB (%d bytes)\n", toM(reservedMemory), reservedMemory); System.out.printf(" max size = %fMB (%d bytes)\n", toM(directMemory), directMemory); } catch (AddressException e) { System.err.println("Error accessing address 0x" + Long.toHexString(e.getAddress())); e.printStackTrace(); } } public static long getStaticLongFieldValue(String className, String fieldName) { InstanceKlass klass = SystemDictionaryHelper.findInstanceKlass(className); LongField field = (LongField) klass.findField(fieldName, "J"); return field.getValue(klass); } public static double toM(long value) { return value / (1024 * 1024.0); } public String getName() { return "directMemorySize"; } public static void main(String[] args) { DirectMemorySize tool = new DirectMemorySize(); tool.start(args); tool.stop(); } }