您的位置:首页 > 编程语言 > Java开发

遇到了“java.lang.OutOfMemoryError: Java heap space”怎么办?

2017-03-27 17:25 417 查看
–引自《深入理解Java虚拟机》

package mytest;

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

/**
* 遇到了“java.lang.OutOfMemoryError: Java heap space”怎么办?<BR>
* 首先判断是“内存泄漏”还是“内存溢出”。一般的手段是首先通过内存映射分析工具对dump出来的【堆转储快照】进行分析,重点是确认内存中的对象【时否必要存活】<BR>
* 内存泄漏:内存中对象已经没有任何用途,但依然存活。通过GC Root的引用链(通过工具查看)定位泄漏代码的位置<BR>
* 内存溢出:内存中对象确实都必须存活,那就必须检查虚拟机的堆参数(-Xmx与-Xms),与机器物理内存对比看是否可以调大。从代码上检查是否存在某些对象生命周期太长<BR>
* -verbose:gc  虚拟机发生内存回收时在输出设备显示信息
* -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails
* -XX:SurvivorRatio=8<BR>
* -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${目录}:配置内存溢出时,生成堆转储文件
*/
public class OutOfMemoryErrorTest {
static class OOMObject {

}

public static void main(String[] args) {
List<OOMObject> list = new ArrayList<>();
while (true) {
list.add(new OOMObject());
}
}
}


[GC (Allocation Failure) [PSYoungGen: 8008K->1018K(9216K)] 8008K->5396K(19456K), 0.0118146 secs] [Times: user=0.05 sys=0.00, real=0.01 secs]

[GC (Allocation Failure) –[PSYoungGen: 9210K->9210K(9216K)] 13588K->19442K(19456K), 0.0141172 secs] [Times: user=0.06 sys=0.00, real=0.01 secs]

[Full GC (Ergonomics) [PSYoungGen: 9210K->0K(9216K)] [ParOldGen: 10232K->10192K(10240K)] 19442K->10192K(19456K), [Metaspace: 2695K->2695K(1056768K)], 0.2243290 secs] [Times: user=0.31 sys=0.02, real=0.23 secs]

[Full GC (Ergonomics) [PSYoungGen: 7500K->7912K(9216K)] [ParOld
4000
Gen: 10192K->8082K(10240K)] 17693K->15995K(19456K), [Metaspace: 2695K->2695K(1056768K)], 0.1979443 secs] [Times: user=0.44 sys=0.00, real=0.20 secs]

[Full GC (Allocation Failure) [PSYoungGen: 7912K->7912K(9216K)] [ParOldGen: 8082K->8077K(10240K)] 15995K->15990K(19456K), [Metaspace: 2695K->2695K(1056768K)], 0.1343948 secs] [Times: user=0.33 sys=0.00, real=0.13 secs]

java.lang.OutOfMemoryError: Java heap space

Dumping heap to java_pid113100.hprof …

Heap dump file created [28131760 bytes in 0.124 secs]

Exception in thread “main” Heap

PSYoungGen total 9216K, used 8192K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)

eden space 8192K, 100% used [0x00000000ff600000,0x00000000ffe00000,0x00000000ffe00000)

from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)

to space 1024K, 47% used [0x00000000fff00000,0x00000000fff7ac50,0x0000000100000000)

ParOldGen total 10240K, used 8077K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)

object space 10240K, 78% used [0x00000000fec00000,0x00000000ff3e36a8,0x00000000ff600000)

Metaspace used 2727K, capacity 4486K, committed 4864K, reserved 1056768K

class space used 290K, capacity 386K, committed 512K, reserved 1048576K

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 mytest.OutOfMemoryErrorTest.main(OutOfMemoryErrorTest.java:18)

ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2

JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [util.c:840]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: