天天看点

JVM 调优 —— OutOfMemory

零. 简介 OutOfMemory 意思就是需要申请更大的内存, 但是内存限制无法申请到需要的内存。 

一. 解决方法 基本上解决方向有两种:

  1. 检查程序是否有问题, 是不是写死循环不停地创建并持有对象导致内存不足
  1. 内存确实不够, 分为老年代内存不足和永久代内存不足

(1) 老年代内存不足

看一段 GC 日志:在 Full GC 结束后, 但是老年代占用空间还是接近或者等于老年代最大空间, 说明老年代空间确实分配不够。 调大 -Xms、 -Xmx 并保持新生代大小不变,直到 GC 日志不出现关键字 Exception in thread  “main” java.lang.OutOfMemoryError: Java heap space。 

2016-01-19T14:18:19.883+0800: 179.163: [CMS-concurrent-reset-start]
2016-01-19T14:18:19.893+0800: 179.173: [CMS-concurrent-reset: 0.009/0.009 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
{Heap before GC invocations=154 (full 1):
 par new generation   total 943744K, used 850746K [0x0000000757000000, 0x0000000797000000, 0x0000000797000000)
  eden space 838912K, 100% used [0x0000000757000000, 0x000000078a340000, 0x000000078a340000)
  from space 104832K,  11% used [0x000000078a340000, 0x000000078aecea30, 0x00000007909a0000)
  to   space 104832K,   0% used [0x00000007909a0000, 0x00000007909a0000, 0x0000000797000000)
 concurrent mark-sweep generation total 1560576K, used 1560576K [0x0000000797000000, 0x00000007f6400000, 0x00000007f6400000)
 concurrent-mark-sweep perm gen total 159744K, used 69030K [0x00000007f6400000, 0x0000000800000000, 0x0000000800000000)
           

(2) 永久代内存不足 看一段 GC 日志:在 Full GC 结束后, 但是永久代占用空间还是接近或者等于永久代最大空间, 说明永久代空间确实分配不够。 调大 -XX:PermSize、 -XX:MaxPermSize,直到 GC 日志不出现关键字 Exception in thread  “main” java.lang.OutOfMemoryError: PermGen space。 

2016-01-19T14:18:19.883+0800: 179.163: [CMS-concurrent-reset-start]
2016-01-19T14:18:19.893+0800: 179.173: [CMS-concurrent-reset: 0.009/0.009 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
{Heap before GC invocations=154 (full 1):
 par new generation   total 943744K, used 850746K [0x0000000757000000, 0x0000000797000000, 0x0000000797000000)
  eden space 838912K, 100% used [0x0000000757000000, 0x000000078a340000, 0x000000078a340000)
  from space 104832K,  11% used [0x000000078a340000, 0x000000078aecea30, 0x00000007909a0000)
  to   space 104832K,   0% used [0x00000007909a0000, 0x00000007909a0000, 0x0000000797000000)
 concurrent mark-sweep generation total 1560576K, used 345246K [0x0000000797000000, 0x00000007f6400000, 0x00000007f6400000)
 concurrent-mark-sweep perm gen total 159744K, used 159744K [0x00000007f6400000, 0x0000000800000000, 0x0000000800000000)