JAVA 内存溢出案例汇总
作者:冰河团队 时间:2022-02-16 08:49:22
写在前面
作为程序员,多多少少都会遇到一些内存溢出的场景,如果你还没遇到,说明你工作的年限可能比较短,或者你根本就是个假程序员!哈哈,开个玩笑。今天,我们就以Java代码的方式来列举几个典型的内存溢出案例,希望大家在日常工作中,尽量避免写这些low水平的代码。
定义主类结构
首先,我们创建一个名称为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://www.cnblogs.com/binghe001/p/12952772.html
标签:JAVA,内存,溢出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot ApplicationContext接口深入分析
2021-10-21 23:00:03
![](https://img.aspxhome.com/file/2023/6/61056_0s.png)
java实现一个简单的网络爬虫代码示例
2021-08-05 13:59:12
详解Eclipse 字体、字号的设置、最佳字体推荐
2023-11-26 12:25:32
![](https://img.aspxhome.com/file/2023/9/60149_0s.png)
深入了解Java ServletContext
2023-11-08 22:36:27
![](https://img.aspxhome.com/file/2023/7/59387_0s.png)
Flutter 通过Clipper实现各种自定义形状的示例代码
2023-06-19 14:25:11
![](https://img.aspxhome.com/file/2023/7/59397_0s.jpg)
基于springboot 配置文件context-path的坑
2021-07-04 17:37:27
基于java构造方法Vector遍历元素源码分析
2023-11-25 05:21:40
spring cloud gateway如何获取请求的真实地址
2023-11-28 20:20:12
![](https://img.aspxhome.com/file/2023/9/60599_0s.png)
Java Selenium实现多窗口切换的示例代码
2022-01-22 22:48:08
Spring中的@Transactional的工作原理
2023-08-06 02:00:04
![](https://img.aspxhome.com/file/2023/8/58028_0s.webp)
Java中接收键盘输入的三种方法
2023-11-13 16:11:29
一文搞懂Mybatis-plus的分页查询操作
2023-11-25 10:23:17
实例解析Json反序列化之ObjectMapper(自定义实现反序列化方法)
2023-11-23 18:07:25
mybatis水平分表实现动态表名的项目实例
2023-04-02 22:11:42
![](https://img.aspxhome.com/file/2023/2/59772_0s.png)
IntelliJ IDEA(2020.2)的下载、安装步骤详细教程
2023-11-25 07:10:16
![](https://img.aspxhome.com/file/2023/0/59900_0s.png)
详解Spring Data JPA使用@Query注解(Using @Query)
2023-11-29 14:49:34
java关键字static的使用详解
2023-09-24 05:04:11
![](https://img.aspxhome.com/file/2023/1/58181_0s.jpg)
hibernate 命名查询如何实现
2023-11-04 02:35:41
java实现简单斗地主(看牌排序)
2023-09-12 14:42:40
Zookeeper和Eureka哪个更好?
2023-11-10 02:57:35