模拟栈和堆内存溢出
程序员文章站
2022-05-15 21:34:38
...
栈内存溢出
public class Demo {
public static void main(String[] args) {
// 模拟栈内存溢出
fun1();
}
public static void fun1(){
long x = 100000*999999999999L;
fun2();
}
public static void fun2(){
long x = 100000*999999999999L;
fun3();
}
public static void fun3(){
long x = 100000*999999999999L;
fun4();
}
public static void fun4(){
long x = 100000*999999999999L;
fun1();
}
}
利用方法的递归,使本地方法栈内存溢出
模拟堆内存溢出
// public class Demo {
public static void main(String[] args) {
// 模拟堆内存溢出
Fun1 fun6 = new Fun1();
Fun2 fun = new Fun2();
Fun3 fun1 = new Fun3();
Fun4 fun2 = new Fun4();
Fun5 fun3 = new Fun5();
Fun6 fun4 = new Fun6();
Fun7 fun5 = new Fun7();
}
static class Fun1{
Fun2 fun = new Fun2();
Fun3 fun1 = new Fun3();
Fun4 fun2 = new Fun4();
Fun5 fun3 = new Fun5();
Fun6 fun4 = new Fun6();
Fun7 fun5 = new Fun7();
}
static class Fun2{
Fun2 fun = new Fun2();
Fun3 fun1 = new Fun3();
Fun4 fun2 = new Fun4();
Fun5 fun3 = new Fun5();
Fun6 fun4 = new Fun6();
Fun7 fun5 = new Fun7();
}
static class Fun3{
Fun2 fun = new Fun2();
Fun3 fun1 = new Fun3();
Fun4 fun2 = new Fun4();
Fun5 fun3 = new Fun5();
Fun6 fun4 = new Fun6();
Fun7 fun5 = new Fun7();
}
static class Fun4{
Fun2 fun = new Fun2();
Fun3 fun1 = new Fun3();
Fun4 fun2 = new Fun4();
Fun5 fun3 = new Fun5();
Fun6 fun4 = new Fun6();
Fun7 fun5 = new Fun7();
}
static class Fun5{
Fun2 fun = new Fun2();
Fun3 fun1 = new Fun3();
Fun4 fun2 = new Fun4();
Fun5 fun3 = new Fun5();
Fun6 fun4 = new Fun6();
Fun7 fun5 = new Fun7();
}
static class Fun6{
Fun2 fun = new Fun2();
Fun3 fun1 = new Fun3();
Fun4 fun2 = new Fun4();
Fun5 fun3 = new Fun5();
Fun6 fun4 = new Fun6();
Fun7 fun5 = new Fun7();
}
static class Fun7{
Fun2 fun = new Fun2();
Fun3 fun1 = new Fun3();
Fun4 fun2 = new Fun4();
Fun5 fun3 = new Fun5();
Fun6 fun4 = new Fun6();
Fun7 fun5 = new Fun7();
}
}
抓住堆内存存储对象,制造堆内存溢出
后期要学会用java提供的工具来看内存占用情况(或者插件)
上一篇: php系统附件存储和显示有关问题!1
下一篇: Java内存溢出与栈溢出
推荐阅读
-
C语言中堆内存的开辟和释放与内存处理函数
-
栈内存与堆内存基本、引用类型讲解,JavaScript为什么会有栈内存和堆内存之分?
-
js中的栈、堆、队列、内存空间
-
C++内存分配方式详解(堆、栈、*存储区、全局/静态存储区和常量存储区)
-
C语言进阶指南(1)丨整型溢出和类型提升、内存申请和管理
-
一位面试官询问我:Java中的JVM内存溢出和内存泄露是什么
-
类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法
-
JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )(转载)
-
Java内存区域和内存溢出异常
-
浅谈从Java中的栈和堆,进而衍生到值传递