fork download
  1. import java.lang.management.*;
  2.  
  3. class Memory {
  4. public static void main(String[] args) {
  5. MemoryMXBean m = ManagementFactory.getMemoryMXBean();
  6. for(MemoryType type: MemoryType.values()) {
  7. usage(type, type == MemoryType.HEAP?
  8. m.getHeapMemoryUsage(): m.getNonHeapMemoryUsage());
  9. System.out.println();
  10. for(MemoryPoolMXBean mp: ManagementFactory.getMemoryPoolMXBeans())
  11. if(mp.getType() == type) usage(mp.getName(), mp.getUsage());
  12. System.out.println();
  13. }
  14. }
  15.  
  16. private static void usage(Object header, MemoryUsage mu) {
  17. long used = mu.getUsed(), max = mu.getMax();
  18. System.out.printf(
  19. max > 0? "%-30s %,d (%,d MiB) of %,d (%,d MiB)%n": "%-30s %,d (%,d MiB)%n",
  20. header, used, used >>> 20, max, max >>> 20);
  21. }
  22. }
  23.  
Success #stdin #stdout 0.19s 56816KB
stdin
Standard input is empty
stdout
Heap memory                    2,820,696 (2 MiB) of 1,037,959,168 (989 MiB)

Tenured Gen                    0 (0 MiB) of 715,849,728 (682 MiB)
Eden Space                     4,231,056 (4 MiB) of 286,326,784 (273 MiB)
Survivor Space                 0 (0 MiB) of 35,782,656 (34 MiB)

Non-heap memory                2,833,312 (2 MiB) of 352,321,536 (336 MiB)

CodeHeap 'non-nmethods'        1,079,040 (1 MiB) of 5,828,608 (5 MiB)
Metaspace                      1,078,264 (1 MiB) of 67,108,864 (64 MiB)
CodeHeap 'profiled nmethods'   489,472 (0 MiB) of 122,912,768 (117 MiB)
Compressed Class Space         114,560 (0 MiB) of 33,554,432 (32 MiB)
CodeHeap 'non-profiled nmethods' 89,856 (0 MiB) of 122,916,864 (117 MiB)