/* * MemTest.java * * ... * ... * @author Oystein Reigem */ package aksis.alignment; import java.lang.management.*; import java.util.*; public class MemTest { public static void print(String pool, String type) { // type = "Heap memory" or "Non-heap memory" or "" List pools = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean p: pools) { if ((pool == "") && (type == "")) { System.out.println("Memory pool name=" + p.getName() + " Memory type=" + p.getType() + " Memory usage=" + p.getUsage()); } else { if ( p.getName().startsWith(pool) || type.equals(p.getType().toString()) ) { //System.out.println(("" + (1000000000 + p.getUsage().getUsed())).substring(1, 10)); System.out.println( "used " + ("" + (1000000000 + p.getUsage().getUsed())).substring(1, 10) + " " + ", max " + ("" + (1000000000 + p.getUsage().getMax())).substring(1, 10) ); } } } } // 2006-10-03 // returns remaining heap space (??????????) in bytes public static long getRemainingHeap() { List pools = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean p: pools) { //if (p.getType().toString().equals("Heap memory")) { if (p.getName().startsWith("Tenured Gen")) { //System.out.println("max " + p.getUsage().getMax()); //System.out.println("used " + p.getUsage().getUsed()); return p.getUsage().getMax() - p.getUsage().getUsed(); } } // ### error return -1; } public static long[] getMemoryUsage() { //System.out.println("MemTest sin getMemoryUsage()"); List pools = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean p: pools) { if (p.getName().startsWith("Tenured Gen")) { long[] array = new long[2]; array[0] = p.getUsage().getMax(); //System.out.println(array[0]); array[1] = p.getUsage().getUsed(); //System.out.println(array[1]); return array; } } // ### error return null; } // end 2006-10-03 /* Added by boerre * Get memory percent usage */ public static int getMemoryPercentUsed() { List pools = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean p: pools) { if (p.getName().startsWith("Tenured Gen")) { long max = p.getUsage().getMax(); //System.out.println(array[0]); long used = p.getUsage().getUsed(); //System.out.println(array[1]); return Math.round((float)((float)(used) / max * 100.0)); } } // ### error return -1; } }