1 4 package com.tc.runtime; 5 6 import com.tc.logging.TCLogger; 7 import com.tc.logging.TCLogging; 8 9 import java.lang.management.GarbageCollectorMXBean ; 10 import java.lang.management.ManagementFactory ; 11 import java.lang.management.MemoryMXBean ; 12 import java.lang.management.MemoryPoolMXBean ; 13 import java.lang.management.MemoryType ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 class TCMemoryManagerJdk15 implements JVMMemoryManager { 21 22 private static final String OLD_GEN_NAME = "OLD GEN"; 23 private static final String TENURED_GEN_NAME = "TENURED GEN"; 25 26 private static final TCLogger logger = TCLogging.getLogger(TCMemoryManagerJdk15.class); 27 28 private final MemoryMXBean memoryBean; 29 30 private final MemoryPoolMXBean oldGenBean; 31 private final GarbageCollectorMXBean oldGenCollectorBean; 32 33 public TCMemoryManagerJdk15() { 34 memoryBean = ManagementFactory.getMemoryMXBean(); 35 java.lang.management.MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage(); 36 if (heapUsage.getMax() <= 0) { 37 logger.warn("Please specify Max memory using -Xmx flag for Memory manager to work properly"); 38 } 39 oldGenBean = getOldGenMemoryPoolBean(); 40 oldGenCollectorBean = getOldGenCollectorBean(); 41 } 42 43 private MemoryPoolMXBean getOldGenMemoryPoolBean() { 44 List pools = ManagementFactory.getMemoryPoolMXBeans(); 45 List <String > poolNames = new ArrayList <String >(); 46 for (Iterator i = pools.iterator(); i.hasNext();) { 47 MemoryPoolMXBean mpBean = (MemoryPoolMXBean ) i.next(); 48 String name = mpBean.getName(); 49 poolNames.add(name); 50 if (mpBean.getType() == MemoryType.HEAP && isOldGen(name)) { 51 return mpBean; 53 } 54 } 55 throw new AssertionError ("Old or Tenured Memory pool Not found : " + poolNames); 56 } 57 58 private GarbageCollectorMXBean getOldGenCollectorBean() { 59 List gcs = ManagementFactory.getGarbageCollectorMXBeans(); 60 HashMap <String , List <String >> gcs2Pools = new HashMap <String , List <String >>(); 61 for (Iterator i = gcs.iterator(); i.hasNext();) { 62 GarbageCollectorMXBean gc = (GarbageCollectorMXBean ) i.next(); 63 String [] managedPools = gc.getMemoryPoolNames(); 64 if (gc.isValid() && managedPools != null) { 65 for (int j = 0; j < managedPools.length; j++) { 66 if (isOldGen(managedPools[j])) { 67 return gc; 68 } 69 } 70 gcs2Pools.put(gc.getName(), Arrays.asList(managedPools)); 71 } 72 } 73 throw new AssertionError ("Old or Tenured Memory pool does not have a garbage collector : " + gcs2Pools); 74 } 75 76 private boolean isOldGen(String name) { 77 return (name.toUpperCase().indexOf(OLD_GEN_NAME) > -1 || name.toUpperCase().indexOf(TENURED_GEN_NAME) > -1); 78 } 79 80 public boolean isMemoryPoolMonitoringSupported() { 81 return true; 82 } 83 84 public MemoryUsage getMemoryUsage() { 85 java.lang.management.MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage(); 86 return new Jdk15MemoryUsage(heapUsage, "VM 1.5 Heap Usage"); 88 } 89 90 public MemoryUsage getOldGenUsage() { 91 java.lang.management.MemoryUsage oldGenUsage = oldGenBean.getUsage(); 92 return new Jdk15MemoryUsage(oldGenUsage, oldGenBean.getName(), oldGenCollectorBean.getCollectionCount()); 93 } 94 95 private static final class Jdk15MemoryUsage implements MemoryUsage { 96 97 private final long max; 98 private final long free; 99 private final long used; 100 private final int usedPercentage; 101 private final String desc; 102 private final long collectionCount; 103 104 public Jdk15MemoryUsage(java.lang.management.MemoryUsage stats, String desc, long collectionCount) { 105 long statsMax = stats.getMax(); 106 if (statsMax <= 0) { 107 this.max = stats.getCommitted(); 108 } else { 109 this.max = statsMax; 110 } 111 this.used = stats.getUsed(); 112 this.free = this.max - this.used; 113 this.usedPercentage = (int) (this.used * 100 / this.max); 114 this.desc = desc; 115 this.collectionCount = collectionCount; 116 } 117 118 public Jdk15MemoryUsage(java.lang.management.MemoryUsage usage, String desc) { 120 this(usage, desc, -1); 121 } 122 123 public String getDescription() { 124 return desc; 125 } 126 127 public long getFreeMemory() { 128 return free; 129 } 130 131 public int getUsedPercentage() { 132 return usedPercentage; 133 } 134 135 public long getMaxMemory() { 136 return max; 137 } 138 139 public long getUsedMemory() { 140 return used; 141 } 142 143 public String toString() { 144 return "Jdk15MemoryUsage ( max = " + max + ", used = " + used + ", free = " + free + ", used % = " 145 + usedPercentage + ", collectionCount = " + collectionCount +" )"; 146 } 147 148 public long getCollectionCount() { 149 return collectionCount; 150 } 151 152 } 153 } 154 | Popular Tags |