JVM 调优--查看jvm可用内存

开发环境  eclipse + Tomcat

配置如下

JVM 调优--查看jvm可用内存_第1张图片

JVM 调优--查看jvm可用内存_第2张图片


设置完成以后 启动

在Conntroller中执行以下代码

	MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
		long xms = memoryBean.getHeapMemoryUsage().getInit();
		long xmx = memoryBean.getHeapMemoryUsage().getMax();//
//		long comm = memoryBean.getHeapMemoryUsage().getCommitted();
		long used = memoryBean.getHeapMemoryUsage().getUsed();
		logger.info(xms / 1024 / 1024 + " " + xmx / 1024 / 1024 + " " + used / 1024 / 1024);

		Runtime rt = Runtime.getRuntime();
		long totalMemorySize = rt.totalMemory(); // 初始的总内存
		long maxMemorySiz = rt.maxMemory(); // 最大可用内存
		long freeMemorySize = rt.freeMemory(); // 当前可用内存
		logger.info(totalMemorySize / 1024 / 1024 + " " + maxMemorySiz / 1024 / 1024 + " " + freeMemorySize / 1024 / 1024);
		
执行结果如下

2017-12-22 11:33:07 INFO  c.c.controller.- 100 98 72
2017-12-22 11:33:07 INFO  c.c.controller.- 98 98 25
设置成-Xms50 -Xmx以后

2017-12-22 11:37:18 INFO  c.c.controller.- 50 45 37
2017-12-22 11:37:18 INFO  c.c.controller.- 45 45 7

不太理解的是用第一种方式 init 比 max大

如果把-Xms -Xmx设置的过小,比如设置成 30 则启动的时候会报错

java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded
	at java.util.concurrent.FutureTask.report(FutureTask.java:122)
	at java.util.concurrent.FutureTask.get(FutureTask.java:192)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
	at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

如果在程序里把剩下的空间全部开辟

byte[] data = new byte[(int) freeMemorySize];
java.lang.OutOfMemoryError: Java heap space

以上代码报错

byte[] data = new byte[(int) freeMemorySize-1]; // -1 就没问题了



eclipse + Tomcat

你可能感兴趣的:(JVM,JVM调优)