首页  

使用jmx监控jvm     所属分类 jvm 浏览量 274
jmx Java Management Extensions

java.lang.management.*

操作系统 线程 类加载 GC 内存池 运行时 堆内堆外内存


OperatingSystemMXBean op = ManagementFactory.getOperatingSystemMXBean(); 
ThreadMXBean tmb = ManagementFactory.getThreadMXBean(); 
ClassLoadingMXBean clmb = ManagementFactory.getClassLoadingMXBean();  
List< GarbageCollectorMXBean> gcmbs = ManagementFactory.getGarbageCollectorMXBeans();
List< MemoryPoolMXBean> mps = ManagementFactory.getMemoryPoolMXBeans();
Runtime rt = Runtime.getRuntime();
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();



Map< String,String> map = new LinkedHashMap< String,String>();
		
map.put("maxMemory", rt.maxMemory()+"");
map.put("totalMemory", rt.totalMemory()+"");
map.put("freeMemory", rt.freeMemory()+"");
	
map.put("lastMinuteLoad", 	op.getSystemLoadAverage()+"");
map.put("availableProcessors", rt.availableProcessors()+"");
		
		
map.put("peakThreadCount", tmb.getPeakThreadCount()+"");
map.put("threadCount", tmb.getThreadCount()+"");
map.put("totalStartedThreadCount", tmb.getTotalStartedThreadCount()+"");
map.put("daemonThreadCount", tmb.getDaemonThreadCount()+"");

	
map.put("totalLoadedClassCount" ,clmb.getTotalLoadedClassCount()+"");  
map.put("loadedClassCount",clmb.getLoadedClassCount()+"");  
map.put("unloadedClassCount" , clmb.getUnloadedClassCount()+"");
		
		
MemoryUsage mu = memoryBean.getHeapMemoryUsage();
// 堆内存
map.put("heapUsed", mu.getUsed() + "");
// 堆外内存
mu = memoryBean.getNonHeapMemoryUsage();
map.put("nonHeapUsed", mu.getUsed() + "");

		
		
int num = gcmbs.size();

for (int i = 0; i < num; i++) {
				GarbageCollectorMXBean mb = gcmbs.get(i);
				String key = "gc" + i;
				StringBuilder sb = new StringBuilder();
				sb.append("name=" + mb.getName());
				sb.append(",count=" + mb.getCollectionCount());
				sb.append(",time=" + mb.getCollectionTime());
				map.put(key, sb.toString());
}
		
int num = mps.size();

for (int i = 0; i < num; i++) {
				MemoryPoolMXBean mb = mps.get(i);
				String key = "mempool" + i;
				StringBuilder sb = new StringBuilder();
				sb.append("name=" + mb.getName());
				sb.append(",type=" + mb.getType());
				sb.append(",usage=" + mb.getUsage());
				sb.append(",collectionUsage=" + mb.getCollectionUsage());
				map.put(key, sb.toString());
}

完整代码

https://gitee.com/dyyx/hellocode/blob/master/project/jpress/src/main/java/dyyx/util/JvmUtil.java

	
jdk8以后 元数据区大小 -XX:MetaspaceSize=512m  -XX:MaxMetaspaceSize=512m 
jcmd PID PerfCounter.print

sun.gc.lastCause="Metadata GC Threshold"


XX:MaxMetaspaceSize 256M
noheap 内存最大稳定在 350M 

JAVA_OPTS="-Xms1024m -Xmx1024m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m"

MetaspaceSize  触及上限时  触发fullGC
jmap -histo:live PID
jstat -gccause PID 2000


 23.75   0.00  37.64  32.00  75.75  55.34   4962   39.078    14    3.877   42.955 Allocation Failure   No GC               
  0.00   0.00   6.77  15.75  54.17  35.80   4965   39.099    15    4.305   43.403 System.gc()          No GC               
  0.00  20.00  23.31  16.16  54.17  35.80   4969   39.123    15    4.305   43.428 Allocation Failure   No GC               
  
  LGCC   lastgccause
  GCC    gccause
      
  Metadata GC Threshold 
     
                 
 
{
    "maxMemory": "1071120384",
    "totalMemory": "1071120384",
    "freeMemory": "360241400",
    "lastMinuteLoad": "0.02",
    "availableProcessors": "8",
    "peakThreadCount": "221",
    "threadCount": "215",
    "totalStartedThreadCount": "237",
    "daemonThreadCount": "198",
    "totalLoadedClassCount": "360208",
    "loadedClassCount": "41723",
    "unloadedClassCount": "318485",
    "heapUsed": "710878984",
    "nonHeapUsed": "340381944",
    "gc0": "name=PS Scavenge,count=7273,time=58269",
    "gc1": "name=PS MarkSweep,count=18,time=5288",
    "mempool0": "name=Code Cache,type=Non-heap memory,usage=init = 2555904(2496K) used = 74047296(72311K) committed = 74973184(73216K) max = 251658240(245760K),collectionUsage=null",
    "mempool1": "name=Metaspace,type=Non-heap memory,usage=init = 0(0K) used = 236014848(230483K) committed = 249954304(244096K) max = 268435456(262144K),collectionUsage=null",
    "mempool2": "name=Compressed Class Space,type=Non-heap memory,usage=init = 0(0K) used = 30319800(29609K) committed = 34996224(34176K) max = 1073741824(1048576K),collectionUsage=null",
    "mempool3": "name=PS Eden Space,type=Heap memory,usage=init = 268435456(262144K) used = 299330032(292314K) committed = 352321536(344064K) max = 352321536(344064K),collectionUsage=init = 268435456(262144K) used = 0(0K) committed = 352321536(344064K) max = 352321536(344064K)",
    "mempool4": "name=PS Survivor Space,type=Heap memory,usage=init = 44564480(43520K) used = 1671168(1632K) committed = 2621440(2560K) max = 2621440(2560K),collectionUsage=init = 44564480(43520K) used = 1671168(1632K) committed = 2621440(2560K) max = 2621440(2560K)",
    "mempool5": "name=PS Old Gen,type=Heap memory,usage=init = 716177408(699392K) used = 409877784(400271K) committed = 716177408(699392K) max = 716177408(699392K),collectionUsage=init = 716177408(699392K) used = 118180920(115411K) committed = 716177408(699392K) max = 716177408(699392K)"
}

内存池  堆外内存分三部分 Code Cache ,Metaspace ,Compressed Class Space

"mempool1": "name=Metaspace,type=Non-heap memory,usage=init = 0(0K) used = 236014848(230483K) committed = 249954304(244096K) max = 268435456(262144K),collectionUsage=null",

used = 236014848(230483K)
 max = 268435456(262144K)
 
 
 
 jdk jcmd 使用 
 jcmd PerfCounter 说明 
 arthas使用   

 tomcat之JMXProxyServlet   
 prometheus使用jmx_exporter监控jvm    
 JMX配置及java客户端远程连接JMX端口   

上一篇     下一篇
arthas OGNL 使用

zookeeper内部原理

httpclient post 请求返回400

jvm参数查看

gc日志相关参数

MySQL事务机制