The reason an article figuring out eight kinds JVM running out of memory (OOM) and solutions

Foreword

Java line and students, will encounter more or less out of memory (OOM) scene, but the cause of the OOM is varied.

v2-9a0d4c2ca70e4574a98c00aeba4c32cc_hd.png

Heap Overflow

This scenario is most common error message:

java.lang.OutOfMemoryError: Java heap space

the reason

1, the code may be present in large object allocation
2, there may be a memory leak, leading after many GC, still can not find a large enough memory to accommodate the current object.

Solution

1. Check for allocation of a large object, most likely a large array allocation
2, by jmap command, the heap memory dump down, using the mat tools to analyze, check if there is a problem of memory leaks
3, if not found significant memory leaks, increase the use of -Xmx heap memory
4, there is little likely to be ignored, check if there are a large number of custom Finalizable objects, there may be provided inside the frame, consider the need for its existence
welcome attention to my male Herd kind of ho [programmers], the article will be updated on the inside, finishing materials will be on the inside.

Permanent Generation / yuan overflow space

Error message:

java.lang.OutOfMemoryError: PermGen spacejava.lang.OutOfMemoryError: Metaspace

the reason

Permanent Generation is a concrete realization of the method area HotSot virtual machines, storage class information is loaded virtual machine, constants, static variables, compiled code, etc. JIT.

After JDK8, metaSpace replaced permanent generation element using a local memory space, as well as other details of the changes:

  • String constants transferred to a permanent heap substituting

  • JVM parameters and permanent generations removed related

There are several possible reasons:

1, before Java7, frequent misuse String.intern () Method
2, generate a large number of proxy class during operation, leading to method area be explode, can not uninstall
3, long-running application, no reboot

No restart JVM process generally occurs at the time of commissioning, such as a FAQ below tomcat official website:

Why does the memory usage increase when I redeploy a web application? That is because your web application has a memory leak. A common issue are “PermGen” memory leaks. They happen because the Classloader (and the Class objects it loaded) cannot be recycled unless some requirements are met (). They are stored in the permanent heap generation by the JVM, and when you redeploy a new class loader is created, which loads another copy of all these classes. This can cause OufOfMemoryErrors eventually. (*) The requirement is that all classes loaded by this classloader should be able to be gc’ed at the same time.

Solution

The reason is relatively simple because the OOM, there are several solutions:

1, to check whether the permanent generation of excessively small space or a space element disposed
2, whether there is a lot of reflection operation check code
3, a dump mat by checking whether there is generated a lot due to the reflection of the proxy class
4, enlarged strokes, restart JVM

v2-b26a29f62e69256653075aa6e05ce75d_hd.png

GC overhead limit exceeded

This exception is relatively rare, the error message:

java.lang.OutOfMemoryError:GC overhead limit exceeded

原因

这个是JDK6新加的错误类型,一般都是堆太小导致的。Sun 官方对此的定义:超过98%的时间用来做GC并且回收了不到2%的堆内存时会抛出此异常。

解决方法

1、检查项目中是否有大量的死循环或有使用大内存的代码,优化代码。

2、添加参数 -XX:-UseGCOverheadLimit  禁用这个检查,其实这个参数解决不了内存问题,只是把错误的信息延后,最终出现 java.lang.OutOfMemoryError: Java heap space。

3、dump内存,检查是否存在内存泄露,如果没有,加大内存。

方法栈溢出

报错信息:

java.lang.OutOfMemoryError : unable to create new native Thread

原因

出现这种异常,基本上都是创建的了大量的线程导致的,以前碰到过一次,通过jstack出来一共8000多个线程。

解决方法

1、通过 -Xss 降低的每个线程栈大小的容量
2、线程总数也受到系统空闲内存和操作系统的限制,检查是否该系统下有此限制:

  • /proc/sys/kernel/pid_max

  • /proc/sys/kernel/thread-max

  • maxuserprocess(ulimit -u)

  • /proc/sys/vm/maxmapcount

非常规溢出

下面这些OOM异常,可能大部分的同学都没有碰到过,但还是需要了解一下

分配超大数组

报错信息 :

java.lang.OutOfMemoryError: Requested array size exceeds VM limit

这种情况一般是由于不合理的数组分配请求导致的,在为数组分配内存之前,JVM 会执行一项检查。要分配的数组在该平台是否可以寻址(addressable),如果不能寻址(addressable)就会抛出这个错误。

The solution is to create a place to check whether there is a large array of your code.

v2-7daa361860ef4ee83a6f1748a71a04d4_hd.png

swap overflow

Error message:

java.lang.OutOfMemoryError: Out of swap space

This is usually caused by the operating system, there are possible reasons:

1, swap partition size allocation is insufficient;

2, all other processes that consume memory.

solution:

1, other service processes can be selectively split out 2, to increase swap partition size, or increase the installed RAM

Native Method overflow

Error message:

java.lang.OutOfMemoryError: stack_trace_with_native_method

Local method at run time memory allocation failure, and previous methods stack overflow, the method of JVM stack overflow occurs in the code level, and the local methods overflow occurs in native methods JNI code or place.

This abnormal probability is very low, can only be evaluated by the local operating system tools, the difficulty a bit large, or give up the better.

At last

Welcome to share with everyone, like I remember the article I focus a point like yo, thanks for the support!


Guess you like

Origin blog.51cto.com/14442094/2449151