以Java代码的方式总结几个典型的内存溢出案例
作者:冰 河 时间:2023-06-11 04:00:56
一、图示
我们先来看看今天要介绍哪些内存溢出案例,这里总结了一张图,如下所示。
二、定义主类结构
首先,我们创建一个名称为BlowUpJVM的类,之后所有的案例实验都是基于这个类进行。如下所示。
public class BlowUpJVM {
}
三、栈深度溢出
public static void testStackOverFlow(){
BlowUpJVM.testStackOverFlow();
}
栈不断递归,而且没有处理,所以虚拟机栈就不断深入不断深入,栈深度就这样溢出了。
四、永久代内存溢出
public static void testPergemOutOfMemory1(){
//方法一失败
List<String> list = new ArrayList<String>();
while(true){
list.add(UUID.randomUUID().toString().intern());
}
}
打算把String常量池堆满,没想到失败了,JDK1.7后常量池放到了堆里,也能进行垃圾回收了。
然后换种方式,使用cglib,用Class把老年代取堆满
public static void testPergemOutOfMemory2(){
try {
while (true) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(OOM.class);
enhancer.setUseCache(false);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
return proxy.invokeSuper(obj, args);
}
});
enhancer.create();
}
}
catch (Exception e){
e.printStackTrace();
}
}
虚拟机成功内存溢出了,那JDK * 产生的类能不能溢出呢?
public static void testPergemOutOfMemory3(){
while(true){
final OOM oom = new OOM();
Proxy.newProxyInstance(oom.getClass().getClassLoader(), oom.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(oom, args);
return result;
}
});
}
}
事实表明,JDK * 差生的类不会造成内存溢出,原因是:JDK * 产生的类信息,不会放到永久代中,而是放在堆中。
五、本地方法栈溢出
public static void testNativeMethodOutOfMemory(){
int j = 0;
while(true){
Printer.println(j++);
ExecutorService executors = Executors.newFixedThreadPool(50);
int i=0;
while(i++<10){
executors.submit(new Runnable() {
public void run() {
}
});
}
}
}
这个的原理就是不断创建线程池,而每个线程池都创建10个线程,这些线程池都是在本地方法区的,久而久之,本地方法区就溢出了。
六、JVM栈内存溢出
public static void testStackOutOfMemory(){
while (true) {
Thread thread = new Thread(new Runnable() {
public void run() {
while(true){
}
}
});
thread.start();
}
}
线程的创建会直接在JVM栈中创建,但是本例子中,没看到内存溢出,主机先挂了,不是JVM挂了,真的是主机挂了,无论在mac还是在windows,都挂了。
温馨提示,这个真的会死机的。
七、堆溢出
public static void testOutOfHeapMemory(){
List<StringBuffer> list = new ArrayList<StringBuffer>();
while(true){
StringBuffer B = new StringBuffer();
for(int i = 0 ; i < 10000 ; i++){
B.append(i);
}
list.add(B);
}
}
不断往堆中塞新增的StringBuffer对象,堆满了就直接溢出了。
八、测试案例完整代码
public class BlowUpJVM {
//栈深度溢出
public static void testStackOverFlow(){
BlowUpJVM.testStackOverFlow();
}
//不能引起永久代溢出
public static void testPergemOutOfMemory1(){
//方法一失败
List<String> list = new ArrayList<String>();
while(true){
list.add(UUID.randomUUID().toString().intern());
}
}
//永久代溢出
public static void testPergemOutOfMemory2(){
try {
while (true) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(OOM.class);
enhancer.setUseCache(false);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
return proxy.invokeSuper(obj, args);
}
});
enhancer.create();
}
}
catch (Exception e){
e.printStackTrace();
}
}
//不会引起永久代溢出
public static void testPergemOutOfMemory3(){
while(true){
final OOM oom = new OOM();
Proxy.newProxyInstance(oom.getClass().getClassLoader(), oom.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = method.invoke(oom, args);
return result;
}
});
}
}
//本地方法栈溢出
public static void testNativeMethodOutOfMemory(){
int j = 0;
while(true){
Printer.println(j++);
ExecutorService executors = Executors.newFixedThreadPool(50);
int i=0;
while(i++<10){
executors.submit(new Runnable() {
public void run() {
}
});
}
}
}
//JVM内存溢出
public static void testStackOutOfMemory(){
while (true) {
Thread thread = new Thread(new Runnable() {
public void run() {
while(true){
}
}
});
thread.start();
}
}
//堆溢出
public static void testOutOfHeapMemory(){
List<StringBuffer> list = new ArrayList<StringBuffer>();
while(true){
StringBuffer B = new StringBuffer();
for(int i = 0 ; i < 10000 ; i++){
B.append(i);
}
list.add(B);
}
}
}
来源:https://blog.csdn.net/l1028386804/article/details/117318571
标签:Java,内存溢出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#基础知识系列八const和readonly关键字详细介绍
2023-06-25 16:39:37
![](https://img.aspxhome.com/file/2023/0/102190_0s.png)
C#中事件处理的个人体会
2023-06-22 16:50:55
Android将项目导出为Library并在项目中使用教程
2022-01-31 14:57:17
![](https://img.aspxhome.com/file/2023/9/125439_0s.png)
通过Java查看程序资源占用情况
2023-11-10 02:01:19
![](https://img.aspxhome.com/file/2023/5/59175_0s.png)
Android中EditText屏蔽第三方输入法表情的方法示例
2021-07-23 01:13:11
Spring Boot项目维护全局json数据代码实例
2023-04-17 23:24:10
webuploader 实现图片批量上传功能附实例代码
2022-12-03 09:13:34
![](https://img.aspxhome.com/file/2023/8/67148_0s.png)
spring web.xml指定配置文件过程解析
2023-05-15 01:32:40
c#中值类型和引用类型的基础教程
2021-10-20 18:03:41
![](https://img.aspxhome.com/file/2023/3/83263_0s.png)
Android使用AIDL实现两个App间通信
2023-06-21 18:05:45
![](https://img.aspxhome.com/file/2023/5/137245_0s.gif)
浅谈@Aspect@Order各个通知的执行顺序
2021-10-24 19:00:38
![](https://img.aspxhome.com/file/2023/3/94313_0s.png)
Qt GUI图形图像开发之Qt表格控件QTableView简单使用方法及QTableView与QTableWidget区别
2022-02-23 05:02:59
![](https://img.aspxhome.com/file/2023/4/117774_0s.jpg)
C++实现哈夫曼树算法
2023-05-21 01:03:46
![](https://img.aspxhome.com/file/2023/9/127509_0s.png)
Java深入讲解异常处理try catch的使用
2023-11-04 13:00:32
![](https://img.aspxhome.com/file/2023/1/58921_0s.png)
C# SMTP发送邮件的示例
2021-06-20 12:36:10
C#中实现任意List的全组合算法代码
2022-09-23 01:06:48
Java实现多线程断点下载
2022-04-27 00:29:16
![](https://img.aspxhome.com/file/2023/9/76319_0s.png)
浅谈关于spring profile的误解
2021-07-25 05:48:43
Java线程池高频面试题总结
2023-10-22 02:12:34
基于C#实现的仿windows左侧伸缩菜单效果
2023-01-22 18:44:54