周末了,觉得我还有很多作业没有写,针对目前大家对OOM的类型不太熟悉,那么我们来总结一下各种OOM出现的情况以及解决方法。

  我们把各种OOM的情况列出来,然后逐一进行代码编写复现和提供解决方法。

  1. 堆溢出-java.lang.OutOfMemoryError: Java heap space。

  2. 栈溢出-java.lang.OutOfMemorryError。

  3. 栈溢出-java.lang.StackOverFlowError。

  4. 元信息溢出-java.lang.OutOfMemoryError: Metaspace。

  5. 直接内存溢出-java.lang.OutOfMemoryError: Direct buffer memory。

  6. GC超限-java.lang.OutOfMemoryError: GC overhead limit exceeded。

  第一种,堆溢出异常,相信大家很常见。即堆内对象不能进行回收了,堆内存持续增大,这样达到了堆内存的最大值,数据满了,所以就出来了。我们直接放溢出的代码样例。我们需要设置好idea的VM Options: -Xmx100m,这样我们设置为最大堆内存,这样运行起来就很快就出来错误了。

package oom;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit; /**
* @author huangqingshi
* @Date 2020-07-18
*/
public class HeapOOM { static class OOMObject {
} public static void main(String[] args) throws InterruptedException {
List<OOMObject> list = new ArrayList<>(); while(true) {
// TimeUnit.MILLISECONDS.sleep(1);
list.add(new OOMObject());
}
} }

  运行的异常如下,代码直接就出来我们看到的异常了。

/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java -Xmx100m -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/tools.jar:/Users/huangqingshi/Downloads/flink-project/target/classes:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-java/1.7.2/flink-java-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-core/1.7.2/flink-core-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-annotations/1.7.2/flink-annotations-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-metrics-core/1.7.2/flink-metrics-core-1.7.2.jar:/Users/huangqingshi/.m2/repository/com/esotericsoftware/kryo/kryo/2.24.0/kryo-2.24.0.jar:/Users/huangqingshi/.m2/repository/com/esotericsoftware/minlog/minlog/1.2/minlog-1.2.jar:/Users/huangqingshi/.m2/repository/org/objenesis/objenesis/2.1/objenesis-2.1.jar:/Users/huangqingshi/.m2/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-compress/1.18/commons-compress-1.18.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-asm/5.0.4-5.0/flink-shaded-asm-5.0.4-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-lang3/3.3.2/commons-lang3-3.3.2.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-math3/3.5/commons-math3-3.5.jar:/Users/huangqingshi/.m2/repository/org/slf4j/slf4j-api/1.7.15/slf4j-api-1.7.15.jar:/Users/huangqingshi/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/force-shading/1.7.2/force-shading-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-streaming-java_2.11/1.7.2/flink-streaming-java_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-runtime_2.11/1.7.2/flink-runtime_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-queryable-state-client-java_2.11/1.7.2/flink-queryable-state-client-java_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-hadoop-fs/1.7.2/flink-hadoop-fs-1.7.2.jar:/Users/huangqingshi/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-netty/4.1.24.Final-5.0/flink-shaded-netty-4.1.24.Final-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-jackson/2.7.9-5.0/flink-shaded-jackson-2.7.9-5.0.jar:/Users/huangqingshi/.m2/repository/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.jar:/Users/huangqingshi/.m2/repository/org/javassist/javassist/3.19.0-GA/javassist-3.19.0-GA.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/scala-library/2.11.12/scala-library-2.11.12.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-actor_2.11/2.4.20/akka-actor_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/com/typesafe/config/1.3.0/config-1.3.0.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/modules/scala-java8-compat_2.11/0.7.0/scala-java8-compat_2.11-0.7.0.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-stream_2.11/2.4.20/akka-stream_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/org/reactivestreams/reactive-streams/1.0.0/reactive-streams-1.0.0.jar:/Users/huangqingshi/.m2/repository/com/typesafe/ssl-config-core_2.11/0.2.1/ssl-config-core_2.11-0.2.1.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/modules/scala-parser-combinators_2.11/1.0.4/scala-parser-combinators_2.11-1.0.4.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-protobuf_2.11/2.4.20/akka-protobuf_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-slf4j_2.11/2.4.20/akka-slf4j_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/org/clapper/grizzled-slf4j_2.11/1.3.2/grizzled-slf4j_2.11-1.3.2.jar:/Users/huangqingshi/.m2/repository/com/github/scopt/scopt_2.11/3.5.0/scopt_2.11-3.5.0.jar:/Users/huangqingshi/.m2/repository/org/xerial/snappy/snappy-java/1.1.4/snappy-java-1.1.4.jar:/Users/huangqingshi/.m2/repository/com/twitter/chill_2.11/0.7.6/chill_2.11-0.7.6.jar:/Users/huangqingshi/.m2/repository/com/twitter/chill-java/0.7.6/chill-java-0.7.6.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-clients_2.11/1.7.2/flink-clients_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-optimizer_2.11/1.7.2/flink-optimizer_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-guava/18.0-5.0/flink-shaded-guava-18.0-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-0.10_2.11/1.7.2/flink-connector-kafka-0.10_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-0.9_2.11/1.7.2/flink-connector-kafka-0.9_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-base_2.11/1.7.2/flink-connector-kafka-base_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/kafka/kafka-clients/0.10.2.1/kafka-clients-0.10.2.1.jar:/Users/huangqingshi/.m2/repository/net/jpountz/lz4/lz4/1.3.0/lz4-1.3.0.jar:/Users/huangqingshi/.m2/repository/org/slf4j/slf4j-log4j12/1.7.7/slf4j-log4j12-1.7.7.jar:/Users/huangqingshi/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/huangqingshi/.m2/repository/com/alibaba/fastjson/1.2.62/fastjson-1.2.62.jar:/Users/huangqingshi/.m2/repository/com/google/guava/guava/28.1-jre/guava-28.1-jre.jar:/Users/huangqingshi/.m2/repository/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar:/Users/huangqingshi/.m2/repository/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:/Users/huangqingshi/.m2/repository/org/checkerframework/checker-qual/2.8.1/checker-qual-2.8.1.jar:/Users/huangqingshi/.m2/repository/com/google/errorprone/error_prone_annotations/2.3.2/error_prone_annotations-2.3.2.jar:/Users/huangqingshi/.m2/repository/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar:/Users/huangqingshi/.m2/repository/org/codehaus/mojo/animal-sniffer-annotations/1.18/animal-sniffer-annotations-1.18.jar:/Users/huangqingshi/.m2/repository/redis/clients/jedis/3.1.0/jedis-3.1.0.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-pool2/2.6.2/commons-pool2-2.6.2.jar:/Users/huangqingshi/.m2/repository/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16.jar:/Users/huangqingshi/.m2/repository/com/google/protobuf/protobuf-java/3.6.1/protobuf-java-3.6.1.jar:/Users/huangqingshi/.m2/repository/com/alibaba/druid/1.1.20/druid-1.1.20.jar oom.HeapOOM
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3210)
at java.util.Arrays.copyOf(Arrays.java:3181)
at java.util.ArrayList.grow(ArrayList.java:261)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
at java.util.ArrayList.add(ArrayList.java:458)
at oom.HeapOOM.main(HeapOOM.java:21) Process finished with exit code 1

  细心的同学可以发现我在里边设置了一个休眠,目的是我们看一下堆内存的结构和数据图。我们将休眠代码打开,然后打开JDK自带的jconsole命令,连接上之后我们看一下概览图,通过下图我们发现堆内存持续不断的增长。

  我们打开内存界面,看一下内存,然后点一下GC按钮,这个时候会有一些类进行回收,但是还是会继续增长,看一下下面的图。

点开信息标签看一下。经过几次GC回收之后,类的数据量还是变化不大,说明没有进行回收。

  

  以上这种情况的解决方法就是找到问题点,分析哪个地方是否存储了大量类没有被回收的情况,通过JMAP命令将线上的堆内存导出来后进行分析。

  第二种情况,我们看一下栈溢出的情况,“上菜”,下面的代码就是无限的创建线程,直到没法再创建线程。

package oom;

import java.util.concurrent.TimeUnit;

/**
* @author huangqingshi
* @Date 2020-07-18
*/
public class StackOOM { public static void infiniteRun() {
while(true) {
Thread thread = new Thread(() -> { while (true) {
try {
TimeUnit.HOURS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
} });
thread.start();
}
} public static void main(String[] args) {
infiniteRun();
} }

  抛出来的异常如下,如果真的需要创建线程,我们需要调整帧栈的大小-Xss512k,默认帧栈大小为1M,如果设置小了,可以创建更多线程。

/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java -Xss512k -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/tools.jar:/Users/huangqingshi/Downloads/flink-project/target/classes:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-java/1.7.2/flink-java-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-core/1.7.2/flink-core-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-annotations/1.7.2/flink-annotations-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-metrics-core/1.7.2/flink-metrics-core-1.7.2.jar:/Users/huangqingshi/.m2/repository/com/esotericsoftware/kryo/kryo/2.24.0/kryo-2.24.0.jar:/Users/huangqingshi/.m2/repository/com/esotericsoftware/minlog/minlog/1.2/minlog-1.2.jar:/Users/huangqingshi/.m2/repository/org/objenesis/objenesis/2.1/objenesis-2.1.jar:/Users/huangqingshi/.m2/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-compress/1.18/commons-compress-1.18.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-asm/5.0.4-5.0/flink-shaded-asm-5.0.4-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-lang3/3.3.2/commons-lang3-3.3.2.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-math3/3.5/commons-math3-3.5.jar:/Users/huangqingshi/.m2/repository/org/slf4j/slf4j-api/1.7.15/slf4j-api-1.7.15.jar:/Users/huangqingshi/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/force-shading/1.7.2/force-shading-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-streaming-java_2.11/1.7.2/flink-streaming-java_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-runtime_2.11/1.7.2/flink-runtime_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-queryable-state-client-java_2.11/1.7.2/flink-queryable-state-client-java_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-hadoop-fs/1.7.2/flink-hadoop-fs-1.7.2.jar:/Users/huangqingshi/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-netty/4.1.24.Final-5.0/flink-shaded-netty-4.1.24.Final-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-jackson/2.7.9-5.0/flink-shaded-jackson-2.7.9-5.0.jar:/Users/huangqingshi/.m2/repository/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.jar:/Users/huangqingshi/.m2/repository/org/javassist/javassist/3.19.0-GA/javassist-3.19.0-GA.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/scala-library/2.11.12/scala-library-2.11.12.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-actor_2.11/2.4.20/akka-actor_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/com/typesafe/config/1.3.0/config-1.3.0.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/modules/scala-java8-compat_2.11/0.7.0/scala-java8-compat_2.11-0.7.0.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-stream_2.11/2.4.20/akka-stream_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/org/reactivestreams/reactive-streams/1.0.0/reactive-streams-1.0.0.jar:/Users/huangqingshi/.m2/repository/com/typesafe/ssl-config-core_2.11/0.2.1/ssl-config-core_2.11-0.2.1.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/modules/scala-parser-combinators_2.11/1.0.4/scala-parser-combinators_2.11-1.0.4.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-protobuf_2.11/2.4.20/akka-protobuf_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-slf4j_2.11/2.4.20/akka-slf4j_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/org/clapper/grizzled-slf4j_2.11/1.3.2/grizzled-slf4j_2.11-1.3.2.jar:/Users/huangqingshi/.m2/repository/com/github/scopt/scopt_2.11/3.5.0/scopt_2.11-3.5.0.jar:/Users/huangqingshi/.m2/repository/org/xerial/snappy/snappy-java/1.1.4/snappy-java-1.1.4.jar:/Users/huangqingshi/.m2/repository/com/twitter/chill_2.11/0.7.6/chill_2.11-0.7.6.jar:/Users/huangqingshi/.m2/repository/com/twitter/chill-java/0.7.6/chill-java-0.7.6.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-clients_2.11/1.7.2/flink-clients_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-optimizer_2.11/1.7.2/flink-optimizer_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-guava/18.0-5.0/flink-shaded-guava-18.0-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-0.10_2.11/1.7.2/flink-connector-kafka-0.10_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-0.9_2.11/1.7.2/flink-connector-kafka-0.9_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-base_2.11/1.7.2/flink-connector-kafka-base_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/kafka/kafka-clients/0.10.2.1/kafka-clients-0.10.2.1.jar:/Users/huangqingshi/.m2/repository/net/jpountz/lz4/lz4/1.3.0/lz4-1.3.0.jar:/Users/huangqingshi/.m2/repository/org/slf4j/slf4j-log4j12/1.7.7/slf4j-log4j12-1.7.7.jar:/Users/huangqingshi/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/huangqingshi/.m2/repository/com/alibaba/fastjson/1.2.62/fastjson-1.2.62.jar:/Users/huangqingshi/.m2/repository/com/google/guava/guava/28.1-jre/guava-28.1-jre.jar:/Users/huangqingshi/.m2/repository/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar:/Users/huangqingshi/.m2/repository/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:/Users/huangqingshi/.m2/repository/org/checkerframework/checker-qual/2.8.1/checker-qual-2.8.1.jar:/Users/huangqingshi/.m2/repository/com/google/errorprone/error_prone_annotations/2.3.2/error_prone_annotations-2.3.2.jar:/Users/huangqingshi/.m2/repository/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar:/Users/huangqingshi/.m2/repository/org/codehaus/mojo/animal-sniffer-annotations/1.18/animal-sniffer-annotations-1.18.jar:/Users/huangqingshi/.m2/repository/redis/clients/jedis/3.1.0/jedis-3.1.0.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-pool2/2.6.2/commons-pool2-2.6.2.jar:/Users/huangqingshi/.m2/repository/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16.jar:/Users/huangqingshi/.m2/repository/com/google/protobuf/protobuf-java/3.6.1/protobuf-java-3.6.1.jar:/Users/huangqingshi/.m2/repository/com/alibaba/druid/1.1.20/druid-1.1.20.jar oom.StackOOM
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:717)
at oom.StackOOM.infiniteRun(StackOOM.java:24)
at oom.StackOOM.main(StackOOM.java:29) Process finished with exit code 130 (interrupted by signal 2: SIGINT)

  以上这种情况是帧栈不够用了,如果出现了这种情况,我们需要了解什么地方创建了很多线程,线上程序需要用jstack命令,将当前线程的状态导出来放到文件里边,然后将文件上传到fastthread.io网站上进行分析。

  第三种情况,我们看一下栈溢出的另一种情况,这就是栈的StackOverFlow的情况。下面就是一个死循环递归调用。

package oom;

/**
* @author huangqingshi
* @Date 2020-07-18
*/
public class StackOFE { public static void stackOverFlowErrorMethod() {
stackOverFlowErrorMethod();
} public static void main(String[] args) {
stackOverFlowErrorMethod();
} }

  运行之后出现的错误如下,程序每次递归的时候,程序会把数据结果压入栈,包括里边的指针等,这个时候就需要帧栈大一些才能承受住更多的递归调用。通过-Xss进行设置,上边的例子需要设置小一些,以分配更多的帧栈,这次是一个帧栈需要记录程序数据,所以需要更大的值。

/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java -Xss2m -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/tools.jar:/Users/huangqingshi/Downloads/flink-project/target/classes:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-java/1.7.2/flink-java-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-core/1.7.2/flink-core-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-annotations/1.7.2/flink-annotations-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-metrics-core/1.7.2/flink-metrics-core-1.7.2.jar:/Users/huangqingshi/.m2/repository/com/esotericsoftware/kryo/kryo/2.24.0/kryo-2.24.0.jar:/Users/huangqingshi/.m2/repository/com/esotericsoftware/minlog/minlog/1.2/minlog-1.2.jar:/Users/huangqingshi/.m2/repository/org/objenesis/objenesis/2.1/objenesis-2.1.jar:/Users/huangqingshi/.m2/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-compress/1.18/commons-compress-1.18.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-asm/5.0.4-5.0/flink-shaded-asm-5.0.4-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-lang3/3.3.2/commons-lang3-3.3.2.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-math3/3.5/commons-math3-3.5.jar:/Users/huangqingshi/.m2/repository/org/slf4j/slf4j-api/1.7.15/slf4j-api-1.7.15.jar:/Users/huangqingshi/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/force-shading/1.7.2/force-shading-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-streaming-java_2.11/1.7.2/flink-streaming-java_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-runtime_2.11/1.7.2/flink-runtime_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-queryable-state-client-java_2.11/1.7.2/flink-queryable-state-client-java_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-hadoop-fs/1.7.2/flink-hadoop-fs-1.7.2.jar:/Users/huangqingshi/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-netty/4.1.24.Final-5.0/flink-shaded-netty-4.1.24.Final-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-jackson/2.7.9-5.0/flink-shaded-jackson-2.7.9-5.0.jar:/Users/huangqingshi/.m2/repository/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.jar:/Users/huangqingshi/.m2/repository/org/javassist/javassist/3.19.0-GA/javassist-3.19.0-GA.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/scala-library/2.11.12/scala-library-2.11.12.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-actor_2.11/2.4.20/akka-actor_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/com/typesafe/config/1.3.0/config-1.3.0.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/modules/scala-java8-compat_2.11/0.7.0/scala-java8-compat_2.11-0.7.0.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-stream_2.11/2.4.20/akka-stream_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/org/reactivestreams/reactive-streams/1.0.0/reactive-streams-1.0.0.jar:/Users/huangqingshi/.m2/repository/com/typesafe/ssl-config-core_2.11/0.2.1/ssl-config-core_2.11-0.2.1.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/modules/scala-parser-combinators_2.11/1.0.4/scala-parser-combinators_2.11-1.0.4.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-protobuf_2.11/2.4.20/akka-protobuf_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-slf4j_2.11/2.4.20/akka-slf4j_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/org/clapper/grizzled-slf4j_2.11/1.3.2/grizzled-slf4j_2.11-1.3.2.jar:/Users/huangqingshi/.m2/repository/com/github/scopt/scopt_2.11/3.5.0/scopt_2.11-3.5.0.jar:/Users/huangqingshi/.m2/repository/org/xerial/snappy/snappy-java/1.1.4/snappy-java-1.1.4.jar:/Users/huangqingshi/.m2/repository/com/twitter/chill_2.11/0.7.6/chill_2.11-0.7.6.jar:/Users/huangqingshi/.m2/repository/com/twitter/chill-java/0.7.6/chill-java-0.7.6.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-clients_2.11/1.7.2/flink-clients_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-optimizer_2.11/1.7.2/flink-optimizer_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-guava/18.0-5.0/flink-shaded-guava-18.0-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-0.10_2.11/1.7.2/flink-connector-kafka-0.10_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-0.9_2.11/1.7.2/flink-connector-kafka-0.9_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-base_2.11/1.7.2/flink-connector-kafka-base_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/kafka/kafka-clients/0.10.2.1/kafka-clients-0.10.2.1.jar:/Users/huangqingshi/.m2/repository/net/jpountz/lz4/lz4/1.3.0/lz4-1.3.0.jar:/Users/huangqingshi/.m2/repository/org/slf4j/slf4j-log4j12/1.7.7/slf4j-log4j12-1.7.7.jar:/Users/huangqingshi/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/huangqingshi/.m2/repository/com/alibaba/fastjson/1.2.62/fastjson-1.2.62.jar:/Users/huangqingshi/.m2/repository/com/google/guava/guava/28.1-jre/guava-28.1-jre.jar:/Users/huangqingshi/.m2/repository/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar:/Users/huangqingshi/.m2/repository/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:/Users/huangqingshi/.m2/repository/org/checkerframework/checker-qual/2.8.1/checker-qual-2.8.1.jar:/Users/huangqingshi/.m2/repository/com/google/errorprone/error_prone_annotations/2.3.2/error_prone_annotations-2.3.2.jar:/Users/huangqingshi/.m2/repository/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar:/Users/huangqingshi/.m2/repository/org/codehaus/mojo/animal-sniffer-annotations/1.18/animal-sniffer-annotations-1.18.jar:/Users/huangqingshi/.m2/repository/redis/clients/jedis/3.1.0/jedis-3.1.0.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-pool2/2.6.2/commons-pool2-2.6.2.jar:/Users/huangqingshi/.m2/repository/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16.jar:/Users/huangqingshi/.m2/repository/com/google/protobuf/protobuf-java/3.6.1/protobuf-java-3.6.1.jar:/Users/huangqingshi/.m2/repository/com/alibaba/druid/1.1.20/druid-1.1.20.jar oom.StackOFE
Exception in thread "main" java.lang.StackOverflowError
at oom.StackOFE.stackOverFlowErrorMethod(StackOFE.java:10)
at oom.StackOFE.stackOverFlowErrorMethod(StackOFE.java:10)
at oom.StackOFE.stackOverFlowErrorMethod(StackOFE.java:10)
at oom.StackOFE.stackOverFlowErrorMethod(StackOFE.java:10)
at oom.StackOFE.stackOverFlowErrorMethod(StackOFE.java:10)
at oom.StackOFE.stackOverFlowErrorMethod(StackOFE.java:10)

  遇到上面的情况下,那么就需要通过jstack将线程数据导到文件进行分析。找到递归的点,如果程序就是需要递归的次数的话,那么这个时候就需要增大帧栈的大小以适应程序。

  第四种情况,元数据区域溢出,元数据区域也成为方法区,存储着类的相关信息,常量池,方法描述符,字段描述符,运行时产生大量的类就会造成这个区域的溢出。我们运行的时候指定一下元数据区域的大小,设置到idea的VM options里边:-XX:MetaspaceSize=10M -XX:MaxMetaspaceSize=30M。

package oom;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy; import java.lang.reflect.Method; /**
* @author huangqingshi
* @Date 2020-07-18
*/
public class MetaspaceOOM { static class OOMObject{} 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();
}
} }

  运行的结果如下,元数据信息溢出了。这种情况产生的原因有: 通过CBLIG大量生成类,导致Meta信息满了;JDK7的时候使用String.intern()不当,会产生大量常量数据;加载大量的jsp以及动态生成jsp文件。我们需要调整元数据空间的大小,如果调大了之后还出现了这种异常,我们需要分析哪里出现的溢出并fix掉。

/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java -XX:MetaspaceSize=10M -XX:MaxMetaspaceSize=30M -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/tools.jar:/Users/huangqingshi/Downloads/flink-project/target/classes:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-java/1.7.2/flink-java-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-core/1.7.2/flink-core-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-annotations/1.7.2/flink-annotations-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-metrics-core/1.7.2/flink-metrics-core-1.7.2.jar:/Users/huangqingshi/.m2/repository/com/esotericsoftware/kryo/kryo/2.24.0/kryo-2.24.0.jar:/Users/huangqingshi/.m2/repository/com/esotericsoftware/minlog/minlog/1.2/minlog-1.2.jar:/Users/huangqingshi/.m2/repository/org/objenesis/objenesis/2.1/objenesis-2.1.jar:/Users/huangqingshi/.m2/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-compress/1.18/commons-compress-1.18.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-asm/5.0.4-5.0/flink-shaded-asm-5.0.4-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-lang3/3.3.2/commons-lang3-3.3.2.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-math3/3.5/commons-math3-3.5.jar:/Users/huangqingshi/.m2/repository/org/slf4j/slf4j-api/1.7.15/slf4j-api-1.7.15.jar:/Users/huangqingshi/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/force-shading/1.7.2/force-shading-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-streaming-java_2.11/1.7.2/flink-streaming-java_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-runtime_2.11/1.7.2/flink-runtime_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-queryable-state-client-java_2.11/1.7.2/flink-queryable-state-client-java_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-hadoop-fs/1.7.2/flink-hadoop-fs-1.7.2.jar:/Users/huangqingshi/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-netty/4.1.24.Final-5.0/flink-shaded-netty-4.1.24.Final-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-jackson/2.7.9-5.0/flink-shaded-jackson-2.7.9-5.0.jar:/Users/huangqingshi/.m2/repository/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.jar:/Users/huangqingshi/.m2/repository/org/javassist/javassist/3.19.0-GA/javassist-3.19.0-GA.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/scala-library/2.11.12/scala-library-2.11.12.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-actor_2.11/2.4.20/akka-actor_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/com/typesafe/config/1.3.0/config-1.3.0.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/modules/scala-java8-compat_2.11/0.7.0/scala-java8-compat_2.11-0.7.0.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-stream_2.11/2.4.20/akka-stream_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/org/reactivestreams/reactive-streams/1.0.0/reactive-streams-1.0.0.jar:/Users/huangqingshi/.m2/repository/com/typesafe/ssl-config-core_2.11/0.2.1/ssl-config-core_2.11-0.2.1.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/modules/scala-parser-combinators_2.11/1.0.4/scala-parser-combinators_2.11-1.0.4.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-protobuf_2.11/2.4.20/akka-protobuf_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-slf4j_2.11/2.4.20/akka-slf4j_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/org/clapper/grizzled-slf4j_2.11/1.3.2/grizzled-slf4j_2.11-1.3.2.jar:/Users/huangqingshi/.m2/repository/com/github/scopt/scopt_2.11/3.5.0/scopt_2.11-3.5.0.jar:/Users/huangqingshi/.m2/repository/org/xerial/snappy/snappy-java/1.1.4/snappy-java-1.1.4.jar:/Users/huangqingshi/.m2/repository/com/twitter/chill_2.11/0.7.6/chill_2.11-0.7.6.jar:/Users/huangqingshi/.m2/repository/com/twitter/chill-java/0.7.6/chill-java-0.7.6.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-clients_2.11/1.7.2/flink-clients_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-optimizer_2.11/1.7.2/flink-optimizer_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-guava/18.0-5.0/flink-shaded-guava-18.0-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-0.10_2.11/1.7.2/flink-connector-kafka-0.10_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-0.9_2.11/1.7.2/flink-connector-kafka-0.9_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-base_2.11/1.7.2/flink-connector-kafka-base_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/kafka/kafka-clients/0.10.2.1/kafka-clients-0.10.2.1.jar:/Users/huangqingshi/.m2/repository/net/jpountz/lz4/lz4/1.3.0/lz4-1.3.0.jar:/Users/huangqingshi/.m2/repository/org/slf4j/slf4j-log4j12/1.7.7/slf4j-log4j12-1.7.7.jar:/Users/huangqingshi/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/huangqingshi/.m2/repository/com/alibaba/fastjson/1.2.62/fastjson-1.2.62.jar:/Users/huangqingshi/.m2/repository/com/google/guava/guava/28.1-jre/guava-28.1-jre.jar:/Users/huangqingshi/.m2/repository/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar:/Users/huangqingshi/.m2/repository/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:/Users/huangqingshi/.m2/repository/org/checkerframework/checker-qual/2.8.1/checker-qual-2.8.1.jar:/Users/huangqingshi/.m2/repository/com/google/errorprone/error_prone_annotations/2.3.2/error_prone_annotations-2.3.2.jar:/Users/huangqingshi/.m2/repository/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar:/Users/huangqingshi/.m2/repository/org/codehaus/mojo/animal-sniffer-annotations/1.18/animal-sniffer-annotations-1.18.jar:/Users/huangqingshi/.m2/repository/redis/clients/jedis/3.1.0/jedis-3.1.0.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-pool2/2.6.2/commons-pool2-2.6.2.jar:/Users/huangqingshi/.m2/repository/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16.jar:/Users/huangqingshi/.m2/repository/com/google/protobuf/protobuf-java/3.6.1/protobuf-java-3.6.1.jar:/Users/huangqingshi/.m2/repository/com/alibaba/druid/1.1.20/druid-1.1.20.jar:/Users/huangqingshi/.m2/repository/cglib/cglib/3.2.5/cglib-3.2.5.jar:/Users/huangqingshi/.m2/repository/org/ow2/asm/asm/5.2/asm-5.2.jar:/Users/huangqingshi/.m2/repository/org/apache/ant/ant/1.9.6/ant-1.9.6.jar:/Users/huangqingshi/.m2/repository/org/apache/ant/ant-launcher/1.9.6/ant-launcher-1.9.6.jar oom.MetaspaceOOM
Exception in thread "main" java.lang.OutOfMemoryError: Metaspace
at net.sf.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:345)
at net.sf.cglib.proxy.Enhancer.generate(Enhancer.java:492)
at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:114)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:480)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:305)
at oom.MetaspaceOOM.main(MetaspaceOOM.java:28) Process finished with exit code 1

  第五种情况,直接内存溢出,我们除了使用堆内存外,我们还可能用直接内存,即堆外内存。NIO为了提高性能,避免在Java Heap和native Heap中切换,所以使用直接内存,默认情况下,直接内存的大小和对内存大小一致。堆外内存不受JVM的限制,但是受制于机器整体内存的大小限制。如下代码设置堆最大内存为128m,直接内存为100m,然后我们每次分配1M放到list里边。  

-Xmx128m -XX:MaxDirectMemorySize=100M

package oom;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List; /**
* @author huangqingshi
* @Date 2020-07-18
*/
public class DirectBufferOOM { public static void main(String[] args) {
final int _1M = 1024 * 1024 * 1;
List<ByteBuffer> buffers = new ArrayList<>();
int count = 1;
while (true) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(_1M);
buffers.add(byteBuffer);
System.out.println(count++);
} }
}

  这个时候,当输出100次的时候,下次再分配的时候会报OOM-Direct buffer memory。

/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java -Xmx128m -XX:MaxDirectMemorySize=100M -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/lib/tools.jar:/Users/huangqingshi/Downloads/flink-project/target/classes:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-java/1.7.2/flink-java-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-core/1.7.2/flink-core-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-annotations/1.7.2/flink-annotations-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-metrics-core/1.7.2/flink-metrics-core-1.7.2.jar:/Users/huangqingshi/.m2/repository/com/esotericsoftware/kryo/kryo/2.24.0/kryo-2.24.0.jar:/Users/huangqingshi/.m2/repository/com/esotericsoftware/minlog/minlog/1.2/minlog-1.2.jar:/Users/huangqingshi/.m2/repository/org/objenesis/objenesis/2.1/objenesis-2.1.jar:/Users/huangqingshi/.m2/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-compress/1.18/commons-compress-1.18.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-asm/5.0.4-5.0/flink-shaded-asm-5.0.4-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-lang3/3.3.2/commons-lang3-3.3.2.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-math3/3.5/commons-math3-3.5.jar:/Users/huangqingshi/.m2/repository/org/slf4j/slf4j-api/1.7.15/slf4j-api-1.7.15.jar:/Users/huangqingshi/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/force-shading/1.7.2/force-shading-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-streaming-java_2.11/1.7.2/flink-streaming-java_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-runtime_2.11/1.7.2/flink-runtime_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-queryable-state-client-java_2.11/1.7.2/flink-queryable-state-client-java_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-hadoop-fs/1.7.2/flink-hadoop-fs-1.7.2.jar:/Users/huangqingshi/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-netty/4.1.24.Final-5.0/flink-shaded-netty-4.1.24.Final-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-jackson/2.7.9-5.0/flink-shaded-jackson-2.7.9-5.0.jar:/Users/huangqingshi/.m2/repository/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.jar:/Users/huangqingshi/.m2/repository/org/javassist/javassist/3.19.0-GA/javassist-3.19.0-GA.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/scala-library/2.11.12/scala-library-2.11.12.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-actor_2.11/2.4.20/akka-actor_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/com/typesafe/config/1.3.0/config-1.3.0.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/modules/scala-java8-compat_2.11/0.7.0/scala-java8-compat_2.11-0.7.0.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-stream_2.11/2.4.20/akka-stream_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/org/reactivestreams/reactive-streams/1.0.0/reactive-streams-1.0.0.jar:/Users/huangqingshi/.m2/repository/com/typesafe/ssl-config-core_2.11/0.2.1/ssl-config-core_2.11-0.2.1.jar:/Users/huangqingshi/.m2/repository/org/scala-lang/modules/scala-parser-combinators_2.11/1.0.4/scala-parser-combinators_2.11-1.0.4.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-protobuf_2.11/2.4.20/akka-protobuf_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/com/typesafe/akka/akka-slf4j_2.11/2.4.20/akka-slf4j_2.11-2.4.20.jar:/Users/huangqingshi/.m2/repository/org/clapper/grizzled-slf4j_2.11/1.3.2/grizzled-slf4j_2.11-1.3.2.jar:/Users/huangqingshi/.m2/repository/com/github/scopt/scopt_2.11/3.5.0/scopt_2.11-3.5.0.jar:/Users/huangqingshi/.m2/repository/org/xerial/snappy/snappy-java/1.1.4/snappy-java-1.1.4.jar:/Users/huangqingshi/.m2/repository/com/twitter/chill_2.11/0.7.6/chill_2.11-0.7.6.jar:/Users/huangqingshi/.m2/repository/com/twitter/chill-java/0.7.6/chill-java-0.7.6.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-clients_2.11/1.7.2/flink-clients_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-optimizer_2.11/1.7.2/flink-optimizer_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-shaded-guava/18.0-5.0/flink-shaded-guava-18.0-5.0.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-0.10_2.11/1.7.2/flink-connector-kafka-0.10_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-0.9_2.11/1.7.2/flink-connector-kafka-0.9_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/flink/flink-connector-kafka-base_2.11/1.7.2/flink-connector-kafka-base_2.11-1.7.2.jar:/Users/huangqingshi/.m2/repository/org/apache/kafka/kafka-clients/0.10.2.1/kafka-clients-0.10.2.1.jar:/Users/huangqingshi/.m2/repository/net/jpountz/lz4/lz4/1.3.0/lz4-1.3.0.jar:/Users/huangqingshi/.m2/repository/org/slf4j/slf4j-log4j12/1.7.7/slf4j-log4j12-1.7.7.jar:/Users/huangqingshi/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/huangqingshi/.m2/repository/com/alibaba/fastjson/1.2.62/fastjson-1.2.62.jar:/Users/huangqingshi/.m2/repository/com/google/guava/guava/28.1-jre/guava-28.1-jre.jar:/Users/huangqingshi/.m2/repository/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar:/Users/huangqingshi/.m2/repository/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:/Users/huangqingshi/.m2/repository/org/checkerframework/checker-qual/2.8.1/checker-qual-2.8.1.jar:/Users/huangqingshi/.m2/repository/com/google/errorprone/error_prone_annotations/2.3.2/error_prone_annotations-2.3.2.jar:/Users/huangqingshi/.m2/repository/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar:/Users/huangqingshi/.m2/repository/org/codehaus/mojo/animal-sniffer-annotations/1.18/animal-sniffer-annotations-1.18.jar:/Users/huangqingshi/.m2/repository/redis/clients/jedis/3.1.0/jedis-3.1.0.jar:/Users/huangqingshi/.m2/repository/org/apache/commons/commons-pool2/2.6.2/commons-pool2-2.6.2.jar:/Users/huangqingshi/.m2/repository/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16.jar:/Users/huangqingshi/.m2/repository/com/google/protobuf/protobuf-java/3.6.1/protobuf-java-3.6.1.jar:/Users/huangqingshi/.m2/repository/com/alibaba/druid/1.1.20/druid-1.1.20.jar:/Users/huangqingshi/.m2/repository/cglib/cglib/3.2.5/cglib-3.2.5.jar:/Users/huangqingshi/.m2/repository/org/ow2/asm/asm/5.2/asm-5.2.jar:/Users/huangqingshi/.m2/repository/org/apache/ant/ant/1.9.6/ant-1.9.6.jar:/Users/huangqingshi/.m2/repository/org/apache/ant/ant-launcher/1.9.6/ant-launcher-1.9.6.jar oom.DirectBufferOOM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
at java.nio.Bits.reserveMemory(Bits.java:694)
at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311)
at oom.DirectBufferOOM.main(DirectBufferOOM.java:18) Process finished with exit code 1

  这种情况是我们使用直接内存造成溢出,这个时候我们需要检查一下程序里边是否使用的NIO及NIO,比如Netty,里边的直接内存的配置。

  第6种情况,JDK1.6之后新增了一个错误类型,如果堆内存太小的时候会报这个错误。如果98%的GC的时候回收不到2%的时候会报这个错误,也就是最小最大内存出现了问题的时候会报这个错误。如果代码配置了最小最大堆内存都为10m。

-Xmx10m -Xms10m

package oom;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; /**
* @author huangqingshi
* @Date 2020-07-18
*/
public class GCOverheadOOM { public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < Integer.MAX_VALUE; i++) {
executor.execute(() -> {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
//do nothing
}
});
} } }

  这个我们创建了一个线程池,如果线程池执行的时候如果核心线程处理不过来的时候会把数据放到LinkedBlockingQueue里边,也就是堆内存当中。这个时候我们需要检查-Xms -Xmx最小最大堆配置是否合理。再一个dump出现当前内存来分析一下是否使用了大量的循环或使用大量内存代码。

  以上就是我们经常遇到的情况,我们需要针对出现的不行情况进行分析和处理。

  

  

  

JAVA各种OOM代码样例及解决方法的更多相关文章

  1. 2020JAVA最新应对各种OOM代码样例及解决办法

    引言 作者:黄青石 链接:https://www.cnblogs.com/huangqingshi/p/13336648.html?utm_source=tuicool&utm_medium= ...

  2. Java操作HDFS代码样例

    代码在GitHub上. 包括如下几种样例代码: 新建文件夹 删除文件/文件夹 重命名文件/文件夹 查看指定路径下的所有文件 新建文件 读文件 写文件 下载文件至本地 上传本地文件 https://gi ...

  3. java servlet 代码样例 (demo)

    今天又搞了下jsp +servlet 的代码样例,感觉虽然搭了好多次,可是每次还是不记得那些参数,都要去网上搜索,索性自己把这次的简单demo给记录下来,供下次使用的时候直接复制吧. 这个web逻辑 ...

  4. zookeeper实战:SingleWorker代码样例

    我们需要一个“单点worker”系统,此系统来确保系统中定时任务在分布式环境中,任意时刻只有一个实例处于活跃:比如,生产环境中,有6台机器支撑一个应用,但是一个应用中有30个定时任务,这些任务有些必须 ...

  5. 33个超级有用必须要收藏的PHP代码样例

    作为一个正常的程序员,会好几种语言是十分正常的,相信大部分程序员也都会编写几句PHP程序,如果是WEB程序员,PHP一定是必备的,即使你没用开发过大型软件项目,也一定多少了解它的语法. 在PHP的流行 ...

  6. 30个php操作redis经常用法代码样例

    这篇文章主要介绍了30个php操作redis经常用法代码样例,本文事实上不止30个方法,能够操作string类型.list类型和set类型的数据,须要的朋友能够參考下 redis的操作非常多的,曾经看 ...

  7. PHP代码样例

    1 <?php 2 3 /** 4 * 时间:2015-8-6 5 * 作者:River 6 * 超级有用.必须收藏的PHP代码样例 7 */ 8 class Helper { 9 10 /** ...

  8. 1篇文章搞清楚8种JVM内存溢出(OOM)的原因和解决方法

    前言 撸Java的同学,多多少少会碰到内存溢出(OOM)的场景,但造成OOM的原因却是多种多样. 堆溢出 这种场景最为常见,报错信息: java.lang.OutOfMemoryError: Java ...

  9. java开发中遇到的问题及解决方法(持续更新)

    摘自 http://blog.csdn.net/pony12/article/details/38456261 java开发中遇到的问题及解决方法(持续更新) 工作中,以C/C++开发为主,难免与其他 ...

随机推荐

  1. 效率思维模式与Zombie Scrum

    Scrum是由Ken Schwaber和Jeff Sutherland在20世纪90年代提出的概念,并在1995年首次正式确定.起初Scrum是为了解决产品和软件开发固有的复杂性,然而现在Scrum被 ...

  2. WeChair项目Alpha冲刺(9/10)

    团队项目进行情况 1.昨日进展    Alpha冲刺第九天 昨日进展: 前端:安排页面美化,设计实名认证 后端:更新dao层代码 数据库:修改数据表属性,与后端部署数据库交互 2.今日安排 前端:继续 ...

  3. 经典卷积神经网络算法(5):ResNet

    .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...

  4. mysql性能优化总结(MySql避免重复插入记录的几种方法)

    如果我们创建了(area, age,salary)的复合索引,那么其实相当于创建了:(area,age,salary),(area,age).(area)三个索引,这被称为最佳左前缀特性.因此我们在创 ...

  5. VulnHub CengBox2靶机渗透

    ​本文首发于微信公众号:VulnHub CengBox2靶机渗透,未经授权,禁止转载. 难度评级:☆☆☆☆官网地址:https://download.vulnhub.com/cengbox/CengB ...

  6. 01[了解] Dubbo

    什么是Dubbo? 概述 Dubbo是阿里巴巴内部使用的分布式业务框架,2012年由阿里巴巴开源. 由于Dubbo在阿里内部经过广泛的业务验证,在很短时间内,Dubbo就被许多互联网公司所采用,并产生 ...

  7. web之robots.txt

    什么是roots协议 robots协议也叫robots.txt(统一小写)是一种存放于网站根目录下的ASCII编码的文本文件,它通常告诉网络搜索引擎的漫游器(又称网络蜘蛛),此网站中的哪些内容是不应被 ...

  8. IndentationError: unindent does not match any outer indentation level解决策略

    [亲测有效]Nodepad++/Sublime Text3中Python脚本运行出现语法错误:IndentationError: unindent does not match any outer i ...

  9. java的运行

    1.打成war包 war需要部署到tomcat中运行. 2.jar包 A 可执行jar包 java -jar some.jar B 普通jar包 java -cp "dir/*" ...

  10. CSS五种方式实现 Footer 置底

    页脚置底(Sticky footer)就是让网页的footer部分始终在浏览器窗口的底部.当网页内容足够长以至超出浏览器可视高度时,页脚会随着内容被推到网页底部:但如果网页内容不够长,置底的页脚就会保 ...