1
2
3
4
5
6
7
8
9
10
11 package psiprobe.beans;
12
13 import java.lang.management.ManagementFactory;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Set;
17
18 import javax.management.MBeanServer;
19 import javax.management.MalformedObjectNameException;
20 import javax.management.ObjectInstance;
21 import javax.management.ObjectName;
22 import javax.management.openmbean.CompositeDataSupport;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import psiprobe.model.jmx.MemoryPool;
28 import psiprobe.tools.JmxTools;
29
30
31
32
33 public class JvmMemoryInfoAccessorBean {
34
35
36 private static final Logger logger = LoggerFactory.getLogger(JvmMemoryInfoAccessorBean.class);
37
38
39
40
41
42
43
44
45 public List<MemoryPool> getPools() throws MalformedObjectNameException {
46
47 MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
48 Set<ObjectInstance> objectInstanceMemoryPools =
49 mbeanServer.queryMBeans(new ObjectName("java.lang:type=MemoryPool,*"), null);
50
51 long totalInit = 0;
52 long totalMax = 0;
53 long totalUsed = 0;
54 long totalCommitted = 0;
55
56 List<MemoryPool> memoryPools = new LinkedList<>();
57 for (ObjectInstance objectInstance : objectInstanceMemoryPools) {
58 ObjectName objName = objectInstance.getObjectName();
59 MemoryPool memoryPool = new MemoryPool();
60 memoryPool.setName(JmxTools.getStringAttr(mbeanServer, objName, "Name"));
61 memoryPool.setType(JmxTools.getStringAttr(mbeanServer, objName, "Type"));
62
63 CompositeDataSupport cd =
64 (CompositeDataSupport) JmxTools.getAttribute(mbeanServer, objName, "Usage");
65
66
67
68
69 if (cd != null) {
70 memoryPool.setMax(JmxTools.getLongAttr(cd, "max"));
71 memoryPool.setUsed(JmxTools.getLongAttr(cd, "used"));
72 memoryPool.setInit(JmxTools.getLongAttr(cd, "init"));
73 memoryPool.setCommitted(JmxTools.getLongAttr(cd, "committed"));
74 } else {
75 logger.error("Oops, JVM problem? {} 'Usage' attribute is NULL!", objName);
76 }
77
78 totalInit += memoryPool.getInit();
79 totalMax += memoryPool.getMax();
80 totalUsed += memoryPool.getUsed();
81 totalCommitted += memoryPool.getCommitted();
82
83 memoryPools.add(memoryPool);
84 }
85
86 if (!memoryPools.isEmpty()) {
87 MemoryPool pool = new MemoryPool();
88 pool.setName("Total");
89 pool.setType("TOTAL");
90 pool.setInit(totalInit);
91 pool.setUsed(totalUsed);
92 pool.setMax(totalMax);
93 pool.setCommitted(totalCommitted);
94 memoryPools.add(pool);
95 }
96
97 return memoryPools;
98
99 }
100 }