OOM(OutOfMemoryError) 问题归根结底三点原因:

  • 本身资源不够
  • 申请的太多内存
  • 资源耗尽

解决思路,换成Java服务分析,三个原因也可以解读为:

  • 有可能是内存分配确实过小,而正常业务使用了大量内存
  • 某一个对象被频繁申请,却没有释放,内存不断泄漏,导致内存耗尽
  • 某一个资源被频繁申请,系统资源耗尽,例如:不断创建线程,不断发起网络连接

因此,针对解决思路,快速定位OOM问题的三板斧是:

  • 确认是不是内存本身就分配过小
  • 找到最耗内存的对象
  • 确认是否是资源耗尽

确认是不是内存本身就分配过小

  • top 命令查看 内存和java pid
  • jmap -heap pid, 观察堆、新生代、老年代的内存使用情况
    • 这行这条命令可能出现问题,解决方法如下: 这是因为新版的Linux系统加入了 ptrace-scope 机制. 这种机制为了防止用户访问当前正在运行的进程的内存和状态, 而一些调试软件本身就是利用 ptrace 来进行获取某进程的内存状态的(包括GDB),所以在新版本的Linux系统, 默认情况下不允许再访问了. 可以临时开启. 如:
      echo 0 > /proc/sys/kernel/yama/ptrace_scope

      永久写到文件来持久化:

      emacs /etc/sysctl.d/10-ptrace.conf

添加或修改为以下这一句:(0:允许, 1:不允许) kernel.yama.ptrace_scope = 0

#### 找到最耗内存的对象

jmap -histo:live 1246| more

- 输入命令后,会以表格的形式显示存活对象的信息,并按照所占内存大小排序:
    - 实例数
    - 所占内存大小
    - 类名

通过观察,虽然我不知道 [B 是什么类,但是最大也只有72M,对内存来说简直没有知觉。

如果发现某类对象占用内存很大(例如几个G),很可能是类对象创建太多,且一直未释放。例如:

申请完资源后,未调用close()或dispose()释放资源

消费者消费速度慢(或停止消费了),而生产者不断往队列中投递任务,导致队列中任务累积过多


#### task oom 实例数据

root@iZ234dyagmyZ:~# /usr/local/jdk1.7.0_71/bin/jmap -heap 11071 Attaching to process ID 11071, please wait... Debugger attached successfully. Server compiler detected. JVM version is 24.71-b01

using thread-local object allocation. Mark Sweep Compact GC

Heap Configuration: MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 67108864 (64.0MB) NewSize = 1310720 (1.25MB) MaxNewSize = 17592186044415 MB OldSize = 5439488 (5.1875MB) NewRatio = 2 SurvivorRatio = 8 PermSize = 21757952 (20.75MB) MaxPermSize = 134217728 (128.0MB) G1HeapRegionSize = 0 (0.0MB)

Heap Usage: New Generation (Eden + 1 Survivor Space): capacity = 20119552 (19.1875MB) used = 19863776 (18.943572998046875MB) free = 255776 (0.243927001953125MB) 98.72871920806189% used Eden Space: capacity = 17891328 (17.0625MB) used = 17891328 (17.0625MB) free = 0 (0.0MB) 100.0% used From Space: capacity = 2228224 (2.125MB) used = 1972448 (1.881072998046875MB) free = 255776 (0.243927001953125MB) 88.5210822610294% used To Space: capacity = 2228224 (2.125MB) used = 0 (0.0MB) free = 2228224 (2.125MB) 0.0% used tenured generation: capacity = 44761088 (42.6875MB) used = 44761080 (42.68749237060547MB) free = 8 (7.62939453125E-6MB) 99.99998212733345% used Perm Generation: capacity = 58851328 (56.125MB) used = 58812576 (56.088043212890625MB) free = 38752 (0.036956787109375MB) 99.93415271784521% used

27292 interned Strings occupying 3134000 bytes.



jmap -histo:live 11071| more 11071: Unable to open socket file: target process not responding or HotSpot VM not loaded The -F option can be used when the target process is not responding

问题解决方法: http://blog.51cto.com/zhangshaoxiong/1310166

## 整理资料- 
- Java多线程中锁的理解与使用 https://blog.csdn.net/tyyj90/article/details/78236053
- https://www.cnblogs.com/lexiaofei/p/6808152.html
- http://www.cnblogs.com/yangecnu/p/Introduce-2-3-Search-Tree.html
- https://www.cnblogs.com/vincently/p/4526560.html
- https://blog.csdn.net/yeyazhishang/article/details/82353846
- https://www.cnblogs.com/shijingxiang/articles/4743324.html
- http://www.cnblogs.com/aspirant/p/8663911.html
- https://www.cnblogs.com/aspirant/p/8623864.html
- https://www.cnblogs.com/ming-zi/p/6670351.html
- https://blog.csdn.net/leishenop/article/details/53728605
- https://blog.csdn.net/finish_dream/article/details/79118414
- https://blog.csdn.net/lwl2014100338/article/details/79940886
- https://baijiahao.baidu.com/s?id=1612276374572403696&wfr=spider&for=pc
- https://www.cnblogs.com/CoreXin/articles/5688019.html
- https://www.cnblogs.com/skying555/p/8473500.html
- https://www.cnblogs.com/digdeep/p/4512368.html
- http://www.cnblogs.com/middleware/p/9444435.html
- https://www.cnblogs.com/boonya/p/5545960.html
- https://www.cnblogs.com/Leo_wl/p/6581982.html
- https://www.jianshu.com/p/9a42d5372f13
- https://blog.csdn.net/u010963948/article/details/78963572
- https://blog.csdn.net/sdmei/article/details/78696868
- https://www.jianshu.com/p/f2f4256a2310
- https://blog.csdn.net/qq_30764991/article/details/80573352
- https://blog.csdn.net/ialexanderi/article/details/78945951
- http://www.cnblogs.com/wxisme/p/5243631.html
- https://blog.csdn.net/zhuqiuhui/article/details/51849692
- http://www.cnblogs.com/chengxiao/p/6059914.html
- http://www.cnblogs.com/xjnotxj/p/9024962.html
- https://www.cnblogs.com/aurain/p/7063898.html
- https://blog.csdn.net/bourne_again/article/details/82775817
- https://blog.csdn.net/ligh_sqh/article/details/79481040
- https://blog.csdn.net/tieselingzhi/article/details/79764048
- http://www.importnew.com/7010.html
- https://blog.csdn.net/tyyj90/article/details/78236053
- https://blog.csdn.net/u013309797/article/details/81395238
- https://www.cnblogs.com/200911/p/5869097.html