首页 > 其他分享 >学习笔记-JVM OOM实验

学习笔记-JVM OOM实验

时间:2024-05-02 17:56:36浏览次数:25  
标签:java OOM com 笔记 JavaVMStackSOF JVM lxm dameng public

堆内存溢出

package com.dameng.lxm;

import java.util.ArrayList;
import java.util.List;

public class HeapOOM { 
	static class OOMObject {	
	}
	
	public static void main(String[] args) {
		List<OOMObject> objlist= new ArrayList<OOMObject>();
		while(true) {
			objlist.add(new OOMObject());
		}
	}
}

内存参数设置: -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError

java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid15424.hprof ...
Heap dump file created [30880108 bytes in 0.034 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.base/java.util.Arrays.copyOf(Arrays.java:3512)
	at java.base/java.util.Arrays.copyOf(Arrays.java:3481)
	at java.base/java.util.ArrayList.grow(ArrayList.java:237)
	at java.base/java.util.ArrayList.grow(ArrayList.java:244)
	at java.base/java.util.ArrayList.add(ArrayList.java:454)
	at java.base/java.util.ArrayList.add(ArrayList.java:467)
	at testOom/com.dameng.lxm.HeapOOM.main(HeapOOM.java:14)

虚拟机栈和本地方法栈溢出

在《Java虚拟机规范》中描述了两种异常:

  • 如果线程请求的栈深度大于虚拟机所允许的最大深度,将抛出StackOverflowError异常。
  • 如果虚拟机的栈内存允许动态扩展,当扩展栈容量无法申请到足够的内存时,将抛出 OutOfMemoryError异常。

案例一:减小虚拟机栈容量

package com.dameng.lxm;

public class JavaVMStackSOF {
	private int stackLength=1;
	
	public void stackLeak() {
		stackLength++;
		stackLeak();
	} 
	 public static void main(String[] args) {
		 JavaVMStackSOF oom = new JavaVMStackSOF();
		 try {
			oom.stackLeak();
		} catch (Exception e) {
			System.out.print("stack length:" +oom.stackLength);
			throw e;
		}
		
	}
}

内存参数设置:-Xxss128k

The Java thread stack size specified is too small. Specify at least 180k

-Xxss180k

Exception in thread "main" java.lang.StackOverflowError
	at testOom/com.dameng.lxm.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:7)
	at testOom/com.dameng.lxm.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:8)
	at testOom/com.dameng.lxm.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:8)
	at testOom/com.dameng.lxm.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:8)
	at testOom/com.dameng.lxm.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:8)
	at testOom/com.dameng.lxm.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:8)
	at testOom/com.dameng.lxm.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:8)
	at testOom/com.dameng.lxm.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:8)

案例二:增大栈帧

package com.dameng.lxm;

public class JavaVMStackSOFB {
	private static int stackLength = 0;
	public static void test() {
		long unused1, unused2, unused3, unused4, unused5,
		unused6, unused7, unused8, unused9, unused10,
		unused11, unused12, unused13, unused14, unused15,
		unused16, unused17, unused18, unused19, unused20,
		unused21, unused22, unused23, unused24, unused25,
		unused26, unused27, unused28, unused29, unused30,
		unused31, unused32, unused33, unused34, unused35,
		unused36, unused37, unused38, unused39, unused40,
		unused41, unused42, unused43, unused44, unused45,
		unused46, unused47, unused48, unused49, unused50,
		unused51, unused52, unused53, unused54, unused55,
		unused56, unused57, unused58, unused59, unused60,
		unused61, unused62, unused63, unused64, unused65,
		unused66, unused67, unused68, unused69, unused70,
		unused71, unused72, unused73, unused74, unused75,
		unused76, unused77, unused78, unused79, unused80,
		unused81, unused82, unused83, unused84, unused85,
		unused86, unused87, unused88, unused89, unused90,
		unused91, unused92, unused93, unused94, unused95,
		unused96, unused97, unused98, unused99, unused100;
		stackLength ++;
		test();
		unused1 = unused2 = unused3 = unused4 = unused5 =
		unused6 = unused7 = unused8 = unused9 = unused10 =
		unused11 = unused12 = unused13 = unused14 = unused15 =
		unused16 = unused17 = unused18 = unused19 = unused20 =
		unused21 = unused22 = unused23 = unused24 = unused25 =
		unused26 = unused27 = unused28 = unused29 = unused30 =
		unused31 = unused32 = unused33 = unused34 = unused35 =
		unused36 = unused37 = unused38 = unused39 = unused40 =
		unused41 = unused42 = unused43 = unused44 = unused45 =
		unused46 = unused47 = unused48 = unused49 = unused50 =
		unused51 = unused52 = unused53 = unused54 = unused55 =
		unused56 = unused57 = unused58 = unused59 = unused60 =
		unused61 = unused62 = unused63 = unused64 = unused65 =
		unused66 = unused67 = unused68 = unused69 = unused70 =
		unused71 = unused72 = unused73 = unused74 = unused75 =
		unused76 = unused77 = unused78 = unused79 = unused80 =
		unused81 = unused82 = unused83 = unused84 = unused85 =
		unused86 = unused87 = unused88 = unused89 = unused90 =
		unused91 = unused92 = unused93 = unused94 = unused95 =
		unused96 = unused97 = unused98 = unused99 = unused100 = 0;
	}
	public static void main(String[] args) {
		try {
			test();
		}catch (Error e){
			System.out.println("stack length:" + stackLength);
			throw e;
		}
	}
}

-Xxss180k

stack length:87
Exception in thread "main" java.lang.StackOverflowError
	at testOom/com.dameng.lxm.JavaVMStackSOFB.test(JavaVMStackSOFB.java:26)
	at testOom/com.dameng.lxm.JavaVMStackSOFB.test(JavaVMStackSOFB.java:27)
	at testOom/com.dameng.lxm.JavaVMStackSOFB.test(JavaVMStackSOFB.java:27)
	at testOom/com.dameng.lxm.JavaVMStackSOFB.test(JavaVMStackSOFB.java:27)
	at testOom/com.dameng.lxm.JavaVMStackSOFB.test(JavaVMStackSOFB.java:27)
	at testOom/com.dameng.lxm.JavaVMStackSOFB.test(JavaVMStackSOFB.java:27)
	at testOom/com.dameng.lxm.JavaVMStackSOFB.test(JavaVMStackSOFB.java:27)

案例三:创建线程导致内存溢出

package com.dameng.lxm;

public class JavaVMStackOOM {
	
	private void dontStop() {
		while (true) {
		}
	}
	
	public void stackLeakByThread() {
		while (true) {
			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
				dontStop();
				}
			});
			thread.start();
		}
	}
	
	public static void main(String[] args) throws Throwable {
		JavaVMStackOOM oom = new JavaVMStackOOM();
		oom.stackLeakByThread();
	}
}
Exception in thread "main" java.lang.OutOfMemoryError: unable to create native thread

方法区和运行时常量池溢出

案例一:运行时常量池导致的内存溢出异常

package com.dameng.lxm;

import java.util.HashSet;
import java.util.Set;

public class RuntimeConstantPoolOOM {
	public static void main(String[] args) {
		// 使用Set保持着常量池引用,避免Full GC回收常量池行为
		Set<String> set = new HashSet<String>();
		// 在short范围内足以让6MB的PermSize产生OOM了
		short i = 0;
		while (true) {
			set.add(String.valueOf(i++).intern());
		}
	} 
}

内存参数设置:-XX:PermSize=6M -XX:MaxPermSize=6M

Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
at java.lang.String.intern(Native Method)
at org.fenixsoft.oom.RuntimeConstantPoolOOM.main(RuntimeConstantPoolOOM.java: 18)

案例二:借助cglib使得方法区出现内存溢出异常

package com.dameng.lxm;

public class JavaMethodAreaOOM {
	public static void main(String[] args) {
		while (true) {
			Enhancer enhancer = new Enhancer();
			enhancer.setSuperclass(OOMObject.class);
			enhancer.setUseCache(false);
			enhancer.setCallback(new MethodInterceptor() {
				public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
					return proxy.invokeSuper(obj, args);
				}
			});
			enhancer.create();
		}
	}
	static class OOMObject {
	}
}

内存参数设置:-XX:PermSize=10M -XX:MaxPermSize=10M

Caused by: java.lang.OutOfMemoryError: PermGen space
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
... 8 more

本机直接内存溢出

package com.dameng.lxm;

import java.lang.reflect.Field;

public class DirectMemoryOOM {
	private static final int _1MB = 1024 * 1024;
	public static void main(String[] args) throws Exception {
		Field unsafeField = Unsafe.class.getDeclaredFields()[0];
		unsafeField.setAccessible(true);
		Unsafe unsafe = (Unsafe) unsafeField.get(null);
		while (true) {
			unsafe.allocateMemory(_1MB);
		}
	}
}

内存参数设置:-Xmx20M -XX:MaxDirectMemorySize=10M

Exception in thread "main" java.lang.OutOfMemoryError
at sun.misc.Unsafe.allocateMemory(Native Method)
at org.fenixsoft.oom.DMOOM.main(DMOOM.java:20)

标签:java,OOM,com,笔记,JavaVMStackSOF,JVM,lxm,dameng,public
From: https://www.cnblogs.com/lixiaomeng/p/18170380

相关文章

  • Tinkercad学习笔记
    简介官网:https://www.tinkercad.com“Tinkercad是一款免费的在线软件工具集合,可帮助世界各地的用户进行思考、创造和制造。我们是三维设计、工程和娱乐软件领导企业Autodesk(//www.autodesk.com.cn/)的理想推介。”电路快速上手注册之后,在https://www.tinkercad.com/dashbo......
  • 网课-数论学习笔记
    埃氏筛P7960[NOIP2021]报数P1835素数密度:区间筛。预处理\(\sqrt{R}\)内的质数,然后用埃氏筛筛[L,R]的质数。线性筛-EOF-欧拉函数P10031「CfzRound3」XorwithGcd光速乘用于解决$$llTimes(lla,llb,llc){ ullt=(longdouble)a*b/c+0.5; llans=(u......
  • 数论学习笔记 (4):扩展欧几里得算法
    概述扩展欧几里得算法(\(exgcd\))可以用来求形如\(ax+by=c\)的不定方程的通解。铺垫-\(\small\texttt{ax+by=gcd(a,b)}\)的解\(exgcd\)的思想是在用辗转相除法递归\(gcd(a,b)\)的回溯时求出对应方程\(ax+by=gcd(a,b)\)的解。考虑方程\(ax+by=gcd(a,b)\)。看回辗......
  • 王道数据结构个人向笔记-第二章(线性表)
    目录2.1线性表的定义和基本操作2.2顺序表2.2.1顺序表的定义2.2.2顺序表的插入、删除(实现是基于静态分配)2.2.3顺序表的查找2.3链表2.3.1单链表的定义2.3.2单链表的插入删除2.3.3单链表的查找2.3.4单链表的建立2.3.4双链表2.3.5循环链表2.3.6静态链表2.3.7顺序表和链......
  • Web命令执行笔记(持续更新)
    Web命令执行笔记会将web命令执行的题目放到这篇博客来记录,方便自己日后查阅。XYCTF-ezRCE(只允许数字、$、<、\)<?phphighlight_file(__FILE__);functionwaf($cmd){$white_list=['0','1','2','3','4','5','6','7'......
  • 《自动机理论、语言和计算导论》阅读笔记:p215-p351
    《自动机理论、语言和计算导论》学习第11天,p215-p351总结,总计37页。一、技术总结1.constrainedproblem2.Fermat'slatstheoremFermat'sLastTheoremstatesthatnothreepositiveintegersa,bandcsatisfytheequationa^n+b^n=c^nforanyintegervalue......
  • 网课-组合数学学习笔记
    排列\[A_n^m=\dfrac{n!}{(n-m)!}\]组合\[\dbinom{n}{m}=\dfrac{n!}{(n-m)!}\]下降幂&上升幂\[\]二项式定理隔板法如果隔板法的每个间隔有下界(下界可以不同),可以先把下界从整体减去。P5520[yLOI2019]青原樱:可将树看作隔板。环排列\(n\)的长度,\(m\)种颜色。可以......
  • 读天才与算法:人脑与AI的数学思维笔记15_声响的数学之旅
    1. 音乐1.1. 巴赫的作品以严格的对位著称,他十分中意对称的结构1.2. 巴托克的作品很多都以黄金比例为结构基础,他非常喜欢并善于使用斐波纳契数列1.3. 有时,作曲家是本能地或者不自知地被数学的模式和结构所吸引,而他们并没有意识到这些数学模式的意义1.4. 有时,他们主动去寻......
  • Unity 热更--AssetBundle学习笔记 0.7
    AssetBundleAB包是什么?AssetBundle又称AB包,是Unity提供的一种用于存储资源的资源压缩包。Unity中的AssetBundle系统是对资源管理的一种扩展,通过将资源分布在不同的AB包中可以最大程度地减少运行时的内存压力,可以动态地加载和卸载AB包,继而有选择地加载内容。AB包的优势有哪些?......
  • ROS2官方文档阅读笔记:Managed nodes
    原文目录ManagednodesstatetransitionManagednodes这篇文章讲解了节点的生命周期蓝色方块里的被称为PrimaryState,即基本状态黄色方块里的被称为transition,即转换state在这里总结一下的节点的各个状态:1.一旦节点被实例化,则到达unconfigured的状态2.经过转换(transitio......