首页   快速返回

metaspace OOM 实例     所属分类 java
groovy 动态编译,生成 class 

MetaSpaceOom.java

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

public class MetaSpaceOom {

	public static void main(String[] args) throws Exception {
		System.out.println("MetaSpaceOom");
		String expression = "(1+2+3)/2";
		int num = 1000000;
		GroovyClassLoader loader = new GroovyClassLoader();
		
		for (int i = 0; i < num; i++) {
			try {
				// loader = new GroovyClassLoader();
				// 表达式不一样才会生成新的class
				expression = "a+" + i;
				// 编译执行
				Class groovyClass = loader.parseClass(expression);
				
				GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
				// groovyObject 不可缓存
				groovyObject.setProperty("a", 1);
	
				Object result = groovyObject.invokeMethod("run", new Object[] {});
				System.out.println(i + "," + result + "," + groovyClass.getSimpleName());

			} catch (Throwable e) {
				System.out.println(e);
				// 不加这个会一直 java.lang.OutOfMemoryError: Metaspace
				loader = new GroovyClassLoader();
				// System.gc();
				Thread.sleep(3000);
			}
		}
	}
}


-XX:MaxMetaspaceSize=32m	  设置元空间最大空间 方便尽快触发OOM			
动态生成的class ,classloader 回收时才会被回收 

完整代码 
https://gitee.com/dyyx/mveldemo/blob/master/src/main/java/dyyx/MetaSpaceOom.java



ps aux|grep MetaSpaceOom
jcmd 33856   PerfCounter.print

jcmd 显示性能计算器数据

java.cls.loadedClasses=218754
java.cls.unloadedClasses=214036

java.rt.vmArgs="-XX:MaxMetaspaceSize=32m -Dfile.encoding=UTF-8"
java.rt.vmFlags=""

java.threads.daemon=3
java.threads.live=4
java.threads.livePeak=4
java.threads.started=4

sun.gc.cause="Last ditch collection"
sun.gc.lastCause="Metadata GC Threshold"



 jdk jcmd 使用 

 java8的方法区 

上一篇     下一篇
mediumtext和text的区别

Java线程池ExecutorService与CompletionService

使用aql更新lua脚本

规则引擎easyrules

Mysql锁机制

mysql连接信息查看