第二季:5.请谈谈你对OOM的认识【Java面试题】

前言


2022 10/12 22:17

路漫漫其修远兮,吾将上下而求索


本文是根据尚硅谷学习所做笔记

仅供学习交流使用,转载注明出处


推荐

尚硅谷Java大厂面试题第2季,面试必刷,跳槽大厂神器

第二季大佬总结

5.请谈谈你对OOM的认识

说明

本文目录前是相关视频的名字和具体视频中思维导图的名字

题目

5.请谈谈你对OOM的认识

java.lang.StackOverflowError 81

package oom5;

/**
 * @author CSDN@日星月云
 * @date 2022/10/12 22:21
 */
public class StackOverflowErrorDemo {
    
    public static void main(String[] args) {
        stackOverflowError();
    }

    private static void stackOverflowError() {
        stackOverflowError();//Exception in thread "main" java.lang.StackOverflowError
    }
}

0809

java.lang.OutOfMemoryError: Java heap space 82

package oom5;

import java.util.Random;

/**
 * @author CSDN@日星月云
 * @date 2022/10/12 22:27
 */

//-Xms10m -Xmx10m
public class JavaHeapSpaceDemo {
    public static void main(String[] args) {
//        String str="atguigu";
//        while (true){
//            str+=str+new Random().nextInt(11111111)+new Random().nextInt(22222222);
//            str.intern();
//        }
         byte[] bytes=new byte[80*1024*1024];
         
        //Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    }
}

java.lang.OutOfMemoryError: GC overhead limit exceeded 83

0111

package oom5;

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

/**
 * @author CSDN@日星月云
 * @date 2022/10/12 22:35
 * JVM参数配置演示
 * -Xms10m -Xmx10m -XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m
 *
 * GC回收时间过长时会抛出OutOfMemoryError。过长的定义是,超过98%的时间用来做Gc并且回收了不到2%的堆内存
 * 连续多次GC都只回收了不到2%的极端情况下才会抛出。假如不抛出 GC overhead limit错误会发生什么情况呢?
 * 那就是GC清理的这么点内存很快会再次填满,迫使GC再次执行.这样就形成恶性循环,
 * CPU使用率一直是100%,而GC却没有任何成果
 */
public class GCOverheadDemo {
    public static void main(String[] args) {
        int i=0;
        List<String> list=new ArrayList<>();

        try {
            while (true){
                list.add(String.valueOf(++i).intern());
            }
        } catch (Throwable e) {
            System.out.println("*******i"+i);//*******i145921
            e.printStackTrace();
            throw e;
        }
    }
    //java.lang.OutOfMemoryError: GC overhead limit exceeded
}

[GC (Allocation Failure) [PSYoungGen: 2048K->480K(2560K)] 2048K->904K(9728K), 0.0020932 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 2528K->481K(2560K)] 2952K->2641K(9728K), 0.0024917 secs] [Times: user=0.06 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 2529K->488K(2560K)] 4689K->4509K(9728K), 0.0029540 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 2536K->504K(2560K)] 6557K->6566K(9728K), 0.0031731 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (Ergonomics) [PSYoungGen: 504K->0K(2560K)] [ParOldGen: 6062K->6197K(7168K)] 6566K->6197K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0486435 secs] [Times: user=0.09 sys=0.00, real=0.05 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2048K->782K(2560K)] [ParOldGen: 6197K->7041K(7168K)] 8245K->7824K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0296614 secs] [Times: user=0.13 sys=0.01, real=0.03 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2048K->2026K(2560K)] [ParOldGen: 7041K->7041K(7168K)] 9089K->9068K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0281900 secs] [Times: user=0.09 sys=0.00, real=0.03 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2048K->2047K(2560K)] [ParOldGen: 7041K->7041K(7168K)] 9089K->9089K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0181646 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7041K->7041K(7168K)] 9089K->9089K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0166141 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7042K->7042K(7168K)] 9090K->9090K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0168942 secs] [Times: user=0.11 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7044K->7044K(7168K)] 9092K->9092K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0176037 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7046K->7046K(7168K)] 9094K->9094K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0169613 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7048K->7048K(7168K)] 9096K->9096K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0176020 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7049K->7049K(7168K)] 9097K->9097K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0189166 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7051K->7051K(7168K)] 9099K->9099K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0181470 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7053K->7053K(7168K)] 9101K->9101K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0171954 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7055K->7055K(7168K)] 9103K->9103K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0177783 secs] [Times: user=0.09 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7056K->7056K(7168K)] 9104K->9104K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0169006 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7058K->7058K(7168K)] 9106K->9106K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0170453 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7060K->7060K(7168K)] 9108K->9108K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0179109 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7062K->7062K(7168K)] 9110K->9110K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0163138 secs] [Times: user=0.01 sys=0.02, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7063K->7063K(7168K)] 9111K->9111K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0174088 secs] [Times: user=0.02 sys=0.01, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7065K->7065K(7168K)] 9113K->9113K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0173568 secs] [Times: user=0.08 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7067K->7067K(7168K)] 9115K->9115K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0209319 secs] [Times: user=0.09 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7069K->7069K(7168K)] 9117K->9117K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0199145 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7070K->7070K(7168K)] 9118K->9118K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0193262 secs] [Times: user=0.01 sys=0.02, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7072K->7072K(7168K)] 9120K->9120K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0206345 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7074K->7074K(7168K)] 9122K->9122K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0162711 secs] [Times: user=0.14 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7076K->7076K(7168K)] 9124K->9124K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0226319 secs] [Times: user=0.00 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7077K->7077K(7168K)] 9125K->9125K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0314767 secs] [Times: user=0.06 sys=0.00, real=0.03 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7079K->7079K(7168K)] 9127K->9127K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0168665 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7081K->7081K(7168K)] 9129K->9129K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0183938 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7083K->7083K(7168K)] 9131K->9131K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0200589 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7084K->7084K(7168K)] 9132K->9132K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0207044 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7086K->7086K(7168K)] 9134K->9134K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0185979 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7088K->7088K(7168K)] 9136K->9136K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0220039 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7090K->7090K(7168K)] 9138K->9138K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0233020 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7091K->7091K(7168K)] 9139K->9139K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0168573 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7093K->7093K(7168K)] 9141K->9141K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0182798 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7095K->7095K(7168K)] 9143K->9143K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0331386 secs] [Times: user=0.05 sys=0.00, real=0.03 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7097K->7097K(7168K)] 9145K->9145K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0174839 secs] [Times: user=0.08 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7098K->7098K(7168K)] 9146K->9146K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0178674 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7100K->7100K(7168K)] 9148K->9148K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0219387 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7102K->7102K(7168K)] 9150K->9150K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0168792 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7106K->7106K(7168K)] 9154K->9154K(9728K), [Metaspace: 3265K->3265K(1056768K)], 0.0171701 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7108K->7088K(7168K)] 9156K->9136K(9728K), [Metaspace: 3268K->3268K(1056768K)], 0.0269909 secs] [Times: user=0.06 sys=0.00, real=0.03 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7095K->7094K(7168K)] 9143K->9142K(9728K), [Metaspace: 3269K->3269K(1056768K)], 0.0216285 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
*******i145921
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7139K->7119K(7168K)] 9187K->9167K(9728K), [Metaspace: 3302K->3302K(1056768K)], 0.0258615 secs] [Times: user=0.01 sys=0.00, real=0.03 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7143K->7121K(7168K)] 9191K->9169K(9728K), [Metaspace: 3340K->3340K(1056768K)], 0.0235428 secs] [Times: user=0.11 sys=0.00, real=0.02 secs] 
java.lang.OutOfMemoryError: GC overhead limit exceeded
	at java.lang.Integer.toString(Integer.java:401)
	at java.lang.String.valueOf(String.java:3099)
	at oom5.GCOverheadDemo.main(GCOverheadDemo.java:24)
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
[Full GC (Ergonomics) [PSYoungGen: 2047K->2047K(2560K)] [ParOldGen: 7132K->7122K(7168K)] 9180K->9170K(9728K), [Metaspace: 3349K->3349K(1056768K)], 0.0170922 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2047K->0K(2560K)] [ParOldGen: 7131K->648K(7168K)] 9179K->648K(9728K), [Metaspace: 3363K->3363K(1056768K)], 0.0051668 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
Heap
 PSYoungGen      total 2560K, used 86K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
  eden space 2048K, 4% used [0x00000000ffd00000,0x00000000ffd15a38,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
  to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 ParOldGen       total 7168K, used 648K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
  object space 7168K, 9% used [0x00000000ff600000,0x00000000ff6a22a8,0x00000000ffd00000)
 Metaspace       used 3381K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 368K, capacity 388K, committed 512K, reserved 1048576K
	at java.lang.Integer.toString(Integer.java:401)
	at java.lang.String.valueOf(String.java:3099)
	at oom5.GCOverheadDemo.main(GCOverheadDemo.java:24)

Process finished with exit code 1

java.lang.OutOfMemoryError: Direct buffer memory 84

Netty + NIO:这是由于NIO引起的

写NIO程序的时候经常会使用ByteBuffer来读取或写入数据,这是一种基于通道(Channel) 与 缓冲区(Buffer)的I/O方式,它可以使用Native 函数库直接分配堆外内存,然后通过一个存储在Java堆里面的DirectByteBuffer对象作为这块内存的引用进行操作。这样能在一些场景中显著提高性能,因为避免了在Java堆和Native堆中来回复制数据。

ByteBuffer.allocate(capability):第一种方式是分配JVM堆内存,属于GC管辖范围,由于需要拷贝所以速度相对较慢

ByteBuffer.allocteDirect(capability):第二种方式是分配OS本地内存,不属于GC管辖范围,由于不需要内存的拷贝,所以速度相对较快

但如果不断分配本地内存,堆内存很少使用,那么JVM就不需要执行GC,DirectByteBuffer对象就不会被回收,这时候怼内存充足,但本地内存可能已经使用光了,再次尝试分配本地内存就会出现OutOfMemoryError,那么程序就崩溃了。

一句话说:本地内存不足,但是堆内存充足的时候,就会出现这个问题

我们使用 -XX:MaxDirectMemorySize=5m 配置能使用的堆外物理内存为5M

-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m
然后我们申请一个6M的空间

// 只设置了5M的物理内存使用,但是却分配 6M的空间
ByteBuffer bb = ByteBuffer.allocateDirect(6 * 1024 * 1024);
这个时候,运行就会出现问题了

配置的maxDirectMemory:5.0MB

1243

package oom5;

import java.nio.ByteBuffer;

/**
 * @author CSDN@日星月云
 * @date 2022/10/12 22:50
 *
 * 配置参数:
 * -Xms10m -Xmx10m -XX:+PrintGcDetails -XX:NMaxDirectMemorySize=5m
 * 
 * 故障现象:
 * Exception in thread "main" java.Lang.OutOfMemoryError: Direct buffer memory
 * 
 * 导致原因:
 * 写NIo程序经常使用ByteBuffer来读取或者写入数据,这是一种基于通道(Channel )与缓冲区(Buffer)的I/O方式,
 * 它可以使用Native函数库直接分配堆外内存,然后通过一个存储在Java堆里面的DirectByteBuffer对象作为这块内存的引用进行操作。
 * 这样能在一些场景中显著提高性能,因为避免了在Java堆和 Native堆中来回复制数据。
 * 
 * ByteBuffer.allocate(capability)第一种方式是分配JVA堆内存,属于cc管辖范围,由于需要拷贝所以速度相对较慢
 * 
 * ByteBuffer.allocateDirect(capability)第一种方式是分配Os本地内存,不属于cc管辖范围,由于不需要内存拷贝所以速度相对较快。
 * 
 * 但如果不断分配本地内存,堆内存很少使用,那么JV就不需要执行GC,DirectByteBuffer对象们就不会被回收,
 * 这时候堆内存充足,但本地内存可能已经使用光了,再次尝试分配本地内存就会出现outOfMemoryError,那程序就直接崩溃了。
 * 
 */
public class DirectBufferMemoryDemo {
    public static void main(String[] args) {
        System.out.println("配置的maxDirectMemory:"+(sun.misc.VM.maxDirectMemory())/(double)1024/1024+"MB");//1676.5MB

        //tsleep
        try{ Thread.sleep(3000); }catch (InterruptedException e){ e.printStackTrace(); }

        //-XX:MaxDirectMemorySize=5m 我们配置为5MB,但实际使用6M,故意使坏
        ByteBuffer bb = ByteBuffer.allocateDirect(6 * 1024 * 1024);

    }
    //配置的maxDirectMemory:5.0MB
    //Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
}

配置的maxDirectMemory:5.0MB
[GC (Allocation Failure) [PSYoungGen: 2047K->504K(2560K)] 2047K->916K(9728K), 0.0015213 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (System.gc()) [PSYoungGen: 1218K->488K(2560K)] 1630K->1160K(9728K), 0.0011058 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) [PSYoungGen: 488K->0K(2560K)] [ParOldGen: 672K->995K(7168K)] 1160K->995K(9728K), [Metaspace: 3784K->3784K(1056768K)], 0.0069488 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 
Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
	at java.nio.Bits.reserveMemory(Bits.java:658)
	at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
	at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311)
	at oom5.DirectBufferMemoryDemo.main(DirectBufferMemoryDemo.java:19)
Heap
 PSYoungGen      total 2560K, used 69K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
  eden space 2048K, 3% used [0x00000000ffd00000,0x00000000ffd11450,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
  to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 ParOldGen       total 7168K, used 995K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
  object space 7168K, 13% used [0x00000000ff600000,0x00000000ff6f8c58,0x00000000ffd00000)
 Metaspace       used 3814K, capacity 4536K, committed 4864K, reserved 1056768K
  class space    used 422K, capacity 428K, committed 512K, reserved 1048576K

Process finished with exit code 1

java.lang.OutOfMemoryError: unable to create new native thread 85 86

不能够创建更多的新的线程了,也就是说创建线程的上限达到了

在高并发场景的时候,会应用到

高并发请求服务器时,经常会出现如下异常java.lang.OutOfMemoryError:unable to create new native thread,准确说该native thread异常与对应的平台有关

导致原因:
1 应用创建了太多线程,一个应用进程创建多个线程,超过系统承载极限
2 服务器并不允许你的应用程序创建这么多线程,linux系统默认运行单个进程可以创建的线程为1024个,如果应用创建超过这个数量,就会报 java.lang.OutOfMemoryError:unable to create new native thread

解决方法:
1想办法降低你应用程序创建线程的数量,分析应用是否真的需要创建这么多线程,如果不是,改代码将线程数降到最低
2对于有的应用,确实需要创建很多线程,远超过linux系统默认1024个线程限制,可以通过修改linux服务器配置,扩大linux默认限制

windows下运行

************** i = 78600
************** i = 78601
************** i = 78602
************** i = 78603
************** i = 78604
[thread 340756 also had an error]
An unrecoverable stack overflow has occurred.
[thread 340772 also had an error]
An unrecoverable stack overflow has occurred.
[thread 340776 also had an error]
An unrecoverable stack overflow has occurred.
[thread 340780 also had an error]
An unrecoverable stack overflow has occurred.
[thread 340796 also had an error]
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
	at java.lang.Thread.start0(Native Method)
	at java.lang.Thread.start(Thread.java:714)
	at oom5.UnableCreateNewThreadDemo.main(UnableCreateNewThreadDemo.java:29)

Process finished with exit code -1073741571 (0xC00000FD)

这个时候,就会出现下列的错误,线程数大概在 900多个

Exception in thread "main" java.lang.OutOfMemoryError: unable to cerate new native thread

非root用户登陆Linux系统测试

服务器级别调参调优

如何查看线程数

ulimit -u

0120

0147
0239

java.lang.OutOfMemoryError: Metaspace 87

使用java -XX:+PrintFlagslnitial命令查看本机的初始化参数,-XX:Metaspacesize21810376B(大约20.8M)

0200

package oom5;


/**
 * @author CSDN@日星月云
 * @date 2022/10/12 23:23
 * JVM参数
 * -XX:MetaspaceSize=8m -XX:MaxMetaspaceSize=8m
 *
 * Java 8及之后的版本使用Metaspace来替代永久代。
 *
 * WMetaspace是方法区在HotSpot中的实现,它与持久代最大的区别在于:Metaspace并不在虚拟机内存中而是使用本地内存
 * 也即在java8中, classe metadata(the virtual machines internal presentation of Java class),被存储在叫做
 * Metaspace的native memory
 *
 * 永久代(java8后被原空间Metaspace取代了)存放了以下信息:
 *
 * 虚拟机加载的类信息
 * 常量池
 * 静态变量
 * 即时编译后的代码
 *
 * 模拟Metaspace空间溢出,我们不断生成类往元空间灌,类占据的空间总是会超过Metaspace指定的空问大小的
 */
public class MetaspaceOOMDemo {
    static class OOMTest{

    }

    public static void main(String[] args) {
        int i=0;//模拟计数多少次以后发生异常
        try {
            while (true){
                i++;
                // 使用Spring的动态字节码技术
                Enhancer enhancer = new Enhancer();
                enhancer.setSuperclass(OOMTest.class);
                enhancer.setUseCache(false);
                enhancer.setCallback(new MethodInterceptor() {
                    @Override
                    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                        return methodProxy.invokeSuper(o, args);
                    }
                });
                enhancer.create();
            }
        }catch (Throwable e){
            System.out.println("*******多少次后发生异常:"+i);
            e.printStackTrace();
        }
    }
    //发生异常的次数: 201
    //java.lang.OutOfMemoryError:Metaspace
}

最后


2022 10/12 23:36


p81~p87


Markdown 18796 字数 462 行数
HTML 18279 字数 310 段落


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日星月云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值