4

How can one look up how much direct memory is currently allocated (and may be allocated) by Java? As the evaluator of http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4879883 mentions, Java maintains these buffers outside the normal Java heap.

Gili
  • 86,244
  • 97
  • 390
  • 689

1 Answers1

5

There are probably other ways to do this with native code (or profiling, etc.), but something like this might work in Java:

Class c = java.nio.Bits.class;
Field maxMemory = c.getDeclaredField("maxMemory");
maxMemory.setAccessible(true);
Field reservedMemory = c.getDeclaredField("reservedMemory");
reservedMemory.setAccessible(true);
synchronized (c) {
    Long maxMemoryValue = (Long)maxMemory.get(null);
    Long reservedMemoryValue = (Long)reservedMemory.get(null);
}
kschneid
  • 5,626
  • 23
  • 31
  • 2
    Thanks. I rather did this to get the class instance: `javaNioBitsClass = Class.forName("java.nio.Bits");` because this class is package-protected by default. – gouessej Mar 28 '11 at 12:09