src/share/classes/sun/tools/jconsole/MemoryPoolProxy.java

Print this page
rev 10195 : 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
Reviewed-by: chegar, psandoz
Contributed-by: otaviojava@java.net


  39 public class MemoryPoolProxy {
  40     private String poolName;
  41     private ProxyClient client;
  42     private MemoryPoolMXBean pool;
  43     private Map<ObjectName,Long> gcMBeans;
  44     private GcInfo lastGcInfo;
  45 
  46     public MemoryPoolProxy(ProxyClient client, ObjectName poolName) throws java.io.IOException {
  47         this.client = client;
  48         this.pool = client.getMXBean(poolName, MemoryPoolMXBean.class);
  49         this.poolName = this.pool.getName();
  50         this.gcMBeans = new HashMap<ObjectName,Long>();
  51         this.lastGcInfo = null;
  52 
  53         String[] mgrNames = pool.getMemoryManagerNames();
  54         for (String name : mgrNames) {
  55             try {
  56                 ObjectName mbeanName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +
  57                                                       ",name=" + name);
  58                 if (client.isRegistered(mbeanName)) {
  59                     gcMBeans.put(mbeanName, new Long(0));
  60                 }
  61             } catch (Exception e) {
  62                 assert false;
  63             }
  64 
  65         }
  66     }
  67 
  68     public boolean isCollectedMemoryPool() {
  69         return (gcMBeans.size() != 0);
  70     }
  71 
  72     public MemoryPoolStat getStat() throws java.io.IOException {
  73         long usageThreshold = (pool.isUsageThresholdSupported()
  74                                   ? pool.getUsageThreshold()
  75                                   : -1);
  76         long collectThreshold = (pool.isCollectionUsageThresholdSupported()
  77                                   ? pool.getCollectionUsageThreshold()
  78                                   : -1);
  79         long lastGcStartTime = 0;
  80         long lastGcEndTime = 0;
  81         MemoryUsage beforeGcUsage = null;
  82         MemoryUsage afterGcUsage = null;
  83         long gcId = 0;
  84         if (lastGcInfo != null) {
  85             gcId = lastGcInfo.getId();
  86             lastGcStartTime = lastGcInfo.getStartTime();
  87             lastGcEndTime = lastGcInfo.getEndTime();
  88             beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(poolName);
  89             afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(poolName);
  90         }
  91 
  92         Set<Map.Entry<ObjectName,Long>> set = gcMBeans.entrySet();
  93         for (Map.Entry<ObjectName,Long> e : set) {
  94             GarbageCollectorMXBean gc =
  95                 client.getMXBean(e.getKey(),
  96                                  com.sun.management.GarbageCollectorMXBean.class);
  97             Long gcCount = e.getValue();
  98             Long newCount = gc.getCollectionCount();
  99             if (newCount > gcCount) {
 100                 gcMBeans.put(e.getKey(), new Long(newCount));
 101                 lastGcInfo = gc.getLastGcInfo();
 102                 if (lastGcInfo.getEndTime() > lastGcEndTime) {
 103                     gcId = lastGcInfo.getId();
 104                     lastGcStartTime = lastGcInfo.getStartTime();
 105                     lastGcEndTime = lastGcInfo.getEndTime();
 106                     beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(poolName);
 107                     afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(poolName);
 108                     assert(beforeGcUsage != null);
 109                     assert(afterGcUsage != null);
 110                 }
 111             }
 112         }
 113 
 114         MemoryUsage usage = pool.getUsage();
 115         return new MemoryPoolStat(poolName,
 116                                   usageThreshold,
 117                                   usage,
 118                                   gcId,
 119                                   lastGcStartTime,
 120                                   lastGcEndTime,


  39 public class MemoryPoolProxy {
  40     private String poolName;
  41     private ProxyClient client;
  42     private MemoryPoolMXBean pool;
  43     private Map<ObjectName,Long> gcMBeans;
  44     private GcInfo lastGcInfo;
  45 
  46     public MemoryPoolProxy(ProxyClient client, ObjectName poolName) throws java.io.IOException {
  47         this.client = client;
  48         this.pool = client.getMXBean(poolName, MemoryPoolMXBean.class);
  49         this.poolName = this.pool.getName();
  50         this.gcMBeans = new HashMap<ObjectName,Long>();
  51         this.lastGcInfo = null;
  52 
  53         String[] mgrNames = pool.getMemoryManagerNames();
  54         for (String name : mgrNames) {
  55             try {
  56                 ObjectName mbeanName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +
  57                                                       ",name=" + name);
  58                 if (client.isRegistered(mbeanName)) {
  59                     gcMBeans.put(mbeanName, 0L);
  60                 }
  61             } catch (Exception e) {
  62                 assert false;
  63             }
  64 
  65         }
  66     }
  67 
  68     public boolean isCollectedMemoryPool() {
  69         return (gcMBeans.size() != 0);
  70     }
  71 
  72     public MemoryPoolStat getStat() throws java.io.IOException {
  73         long usageThreshold = (pool.isUsageThresholdSupported()
  74                                   ? pool.getUsageThreshold()
  75                                   : -1);
  76         long collectThreshold = (pool.isCollectionUsageThresholdSupported()
  77                                   ? pool.getCollectionUsageThreshold()
  78                                   : -1);
  79         long lastGcStartTime = 0;
  80         long lastGcEndTime = 0;
  81         MemoryUsage beforeGcUsage = null;
  82         MemoryUsage afterGcUsage = null;
  83         long gcId = 0;
  84         if (lastGcInfo != null) {
  85             gcId = lastGcInfo.getId();
  86             lastGcStartTime = lastGcInfo.getStartTime();
  87             lastGcEndTime = lastGcInfo.getEndTime();
  88             beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(poolName);
  89             afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(poolName);
  90         }
  91 
  92         Set<Map.Entry<ObjectName,Long>> set = gcMBeans.entrySet();
  93         for (Map.Entry<ObjectName,Long> e : set) {
  94             GarbageCollectorMXBean gc =
  95                 client.getMXBean(e.getKey(),
  96                                  com.sun.management.GarbageCollectorMXBean.class);
  97             Long gcCount = e.getValue();
  98             Long newCount = gc.getCollectionCount();
  99             if (newCount > gcCount) {
 100                 gcMBeans.put(e.getKey(), newCount);
 101                 lastGcInfo = gc.getLastGcInfo();
 102                 if (lastGcInfo.getEndTime() > lastGcEndTime) {
 103                     gcId = lastGcInfo.getId();
 104                     lastGcStartTime = lastGcInfo.getStartTime();
 105                     lastGcEndTime = lastGcInfo.getEndTime();
 106                     beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(poolName);
 107                     afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(poolName);
 108                     assert(beforeGcUsage != null);
 109                     assert(afterGcUsage != null);
 110                 }
 111             }
 112         }
 113 
 114         MemoryUsage usage = pool.getUsage();
 115         return new MemoryPoolStat(poolName,
 116                                   usageThreshold,
 117                                   usage,
 118                                   gcId,
 119                                   lastGcStartTime,
 120                                   lastGcEndTime,