47 checkMemoryUsage("Compressed Class Space", "sun.gc.compressedclassspace"); 48 } 49 } 50 51 private static MemoryPoolMXBean getMemoryPool(String memoryPoolName) { 52 List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); 53 for (MemoryPoolMXBean pool : pools) { 54 if (pool.getName().equals(memoryPoolName)) { 55 return pool; 56 } 57 } 58 59 throw new RuntimeException("Excpted to find a memory pool with name " + 60 memoryPoolName); 61 } 62 63 private static void checkMemoryUsage(String memoryPoolName, String perfNS) 64 throws Exception { 65 MemoryPoolMXBean pool = getMemoryPool(memoryPoolName); 66 67 // Must do a GC to update performance counters 68 System.gc(); 69 assertEQ(getMinCapacity(perfNS), pool.getUsage().getInit()); 70 71 // Must do a second GC to update the perfomance counters again, since 72 // the call pool.getUsage().getInit() could have allocated some 73 // metadata. 74 System.gc(); 75 assertEQ(getUsed(perfNS), pool.getUsage().getUsed()); 76 assertEQ(getCapacity(perfNS), pool.getUsage().getCommitted()); 77 } 78 79 private static long getMinCapacity(String ns) throws Exception { 80 return PerfCounters.findByName(ns + ".minCapacity").longValue(); 81 } 82 83 private static long getCapacity(String ns) throws Exception { 84 return PerfCounters.findByName(ns + ".capacity").longValue(); 85 } 86 87 private static long getUsed(String ns) throws Exception { 88 return PerfCounters.findByName(ns + ".used").longValue(); 89 } 90 } | 47 checkMemoryUsage("Compressed Class Space", "sun.gc.compressedclassspace"); 48 } 49 } 50 51 private static MemoryPoolMXBean getMemoryPool(String memoryPoolName) { 52 List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); 53 for (MemoryPoolMXBean pool : pools) { 54 if (pool.getName().equals(memoryPoolName)) { 55 return pool; 56 } 57 } 58 59 throw new RuntimeException("Excpted to find a memory pool with name " + 60 memoryPoolName); 61 } 62 63 private static void checkMemoryUsage(String memoryPoolName, String perfNS) 64 throws Exception { 65 MemoryPoolMXBean pool = getMemoryPool(memoryPoolName); 66 67 // First, call all the methods to let them allocate their own slab of metadata 68 getMinCapacity(perfNS); 69 getCapacity(perfNS); 70 getUsed(perfNS); 71 pool.getUsage().getInit(); 72 pool.getUsage().getUsed(); 73 pool.getUsage().getCommitted(); 74 assertEQ(1L, 1L); 75 76 // Must do a GC to update performance counters 77 System.gc(); 78 assertEQ(getMinCapacity(perfNS), pool.getUsage().getInit()); 79 assertEQ(getUsed(perfNS), pool.getUsage().getUsed()); 80 assertEQ(getCapacity(perfNS), pool.getUsage().getCommitted()); 81 } 82 83 private static long getMinCapacity(String ns) throws Exception { 84 return PerfCounters.findByName(ns + ".minCapacity").longValue(); 85 } 86 87 private static long getCapacity(String ns) throws Exception { 88 return PerfCounters.findByName(ns + ".capacity").longValue(); 89 } 90 91 private static long getUsed(String ns) throws Exception { 92 return PerfCounters.findByName(ns + ".used").longValue(); 93 } 94 } |