`
conkeyn
  • 浏览: 1533039 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

Solve java.lang.OutOfMemoryError: Java heap space

    博客分类:
  • Java
阅读更多

http://blogs.opcodesolutions.com/roller/java/entry/solve_java_lang_outofmemoryerror_java

 

An OOM or OOME (OutOfMemoryError) simply means that the JVM ran out of memory. When this occurs, you basically have 2 choices:

  • Allow the JVM to use more memory using the -Xmx VM argument. For instance, to allow the JVM to use 1 GB (1024 MB) of memory:
java -Xmx1024m ...
  • Improve/Fix the application so that it uses less memory

In many cases, like in the case of a memory leak, the second option is the only sound choice. A memory leak happens when the application keeps more and more references to objects and never releases them. The garbage collector will therefore never collect those objects and less and less free memory will be available until we reach the point where not enough free memory is available for the application to function normally. At this point, the JVM will throw an OOM.

A memory leak can be very latent. For instance, the application might behave flawlessly during development and QA. However, it suddenly throws a OOM after several days in production at customer site. To solve that issue, you first need to find the root cause of it. The root cause can be very hard to find in development if it cannot be reproduced in-house. Here are the steps to follow in order to find the root cause and fix that issue:

  • Start the application with the VM argument -XX:+HeapDumpOnOutOfMemoryError . This will tell the VM to produce a heap dump when a OOM occurs:
java -XX:+HeapDumpOnOutOfMemoryError ...
  • Reproduce the problem. Well, if you cannot reproduce in dev, you will have to use the production environment.
  • Use VisualVM to read the heap dump file and diagnose the issue.

First of all, a heap dump is the dump of the heap (duh!). It will allow you to navigate the heap and see what objects use all the heap memory and which are the ones that still keep a reference on them, and so on and so forth. This will give you very strong hints and you will (hopefully) be able to find the root cause of the problem. The problem could be a cache that grows indefinitely, a list that keeps collecting business-specific data in memory, a huge request that tries to load almost all data from database in memory, etc.

Once you know the root cause of the problem, you can elaborate solutions to fix it. In case of a cache that grows indefinitely, a good solution could be to set a reasonable limit to that cache. In case of a query that tries to load almost all data from database in memory, you may have to change the way you manipulate data; you could even have to change the behavior of some functionalities of the application.

If you do not want to wait for a OOM or if you just want to see what is in memory, you can still generate heap dump. To manually trigger a heap dump, you have 2 choices:

  • Use VisualVM , right-click on the process on the left pane and select Heap Dump
  • If you do not have a graphical environment and can't use vnc (VisualVM needs a graphical environment), use jps and jmap to generate the heap dump file. Then copy the file to your workstation and use VisualVM to read the heap dump (File -> Load...):
user@host:~$ jps
20198
21734 WordFinder
21921 Jps
21168 Main


user@host:~$ jmap -dump:live,format=b,file=heap.bin 21734
Dumping heap to /home/user/heap.bin ...
Heap dump file created

Here is what VisualVM looks with a heap dump:

Heap Dump in VisualVM  

Alternatively, you can also use jhat to read heap dumps.

分享到:
评论

相关推荐

    gcr-io-distroless-stati.rar

    gcr.io/distroless/static:latest,制作k8s.gcr.io/metrics-server/metrics-server:v0.4.1的基础资源

    仁爱英语八年级下册(U5-U8)单词表标准版.pdf

    - difficult(adj.):困难的,例如:This math problem is really difficult to solve. U6T1: - field(n.):田野,例如:The children are playing in the field. - proper(adj.):恰当的,例如:You should ...

    英语必修一二基础词汇复习大全.pdf

    16. 按照,根据...:according to 17. 参加(游戏/活动)等:take part in 18. 如此...以至于...:so...that... 19. 面对面:face to face 20. 故意:on purpose **Unit 2 English Around The World** 1. 起着重要...

    ansysmingling.rar_ansys

    1. **SOLVE**: 开始求解过程,ANSYS会自动进行预处理、求解和后处理。 2. **POST1**: 后处理命令,可以查看结果数据,绘制图形。 3. **DISPLACEMENT**: 查看或显示位移结果。 4. **STRESS**: 显示或分析应力结果,...

    冲击动力学仿真软件:IMPETUS Afea Solver.docx冲击动力学仿真软件:IMPETUS Afea Solverv1.docx冲击动力学仿真软件:IMPETUS Afea Solve

    冲击动力学仿真软件:IMPETUS Afea Solver.docx 冲击动力学仿真软件:IMPETUS Afea Solverv1.docx 冲击动力学仿真软件:IMPETUS Afea Solver_(10).非线性动力学问题求解.docx 冲击动力学仿真软件:IMPETUS Afea ...

    大学英语四级3000单词

    2. **Ability**: 此名词指的是某人的才能或技能,如:She possesses the ability to solve complex problems. 3. **Abnormal**: 形容词,用于描述不符合正常标准或行为异常的情况,如:The child's behavior seemed...

    一轮复习必修1,5单元(公开课)(1).ppt

    1. 实验中需要找到解决问题的方法(We’ll have to find out a way to solve the problem.) 2. 是否要在咖啡中加糖(Will you add more sugar to your coffee?) 3. 回忆过去游泳的河流(This is the river where I...

    Apache.Maven.Cookbook.1785286129

    It is also meant for those familiar with Apache Maven, but want to understand the finer nuances of Maven and solve specific problems. What You Will Learn Install Apache Maven successfully on your ...

    中考英语考纲重点词汇用法复习总结.doc

    例如:She is able to solve the problem, but he is unable due to his disability. 2. **about**: 关于,常用于"be about to do sth."结构,表示即将做某事。如:We are about to start the exam. 3. **above/...

    英语四六级/自考词汇2

    2. **able**: 形容词,意为“有能力的,能干的”,如`He is able to solve the problem.`(他能解决这个问题。) 3. **abolish**: 动词,用于“废除”法律、规定或习惯,如`The old law was abolished last year.`...

    yum-3.4.3-168.el7.centos.noarch.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    人教高一英语必修二Unit词汇PPT教案.pptx

    15. **solve**: 动词,表示解决问题,如解决交通难题(We finally solved the difficulty of transportation)。 16. **solution**: 名词,解冑或答案,如找到问题的解决方案(Who can solve the mathematical ...

    (英语二)核心词汇.doc

    相关名词有 "solution" 解决方案,动词 "solve" 解决问题,形容词 "soluble" 表示可溶的。 9. **Absent**: 意为“缺席的”,如 "be absent from the meeting" 表示缺席会议。与其相反的动词 "attend" 表示出席。 ...

    高考英语140个基础动词例句.doc

    例句:She attempted to solve the difficult problem on her own. 12. attend(参加):参与活动或会议。 例句:I will attend the conference next week. 13. attract(吸引):引起注意或兴趣。 例句:The ...

    Java解惑(中文版)_java_java解惑_solve65p_

    《Java解惑(中文版)》是一本专为Java初学者设计的学习资料,旨在帮助读者解答在学习Java过程中遇到的各种困惑。"solve65p"可能代表这本书包含65个问题或主题,每个都深入浅出地进行了讲解,旨在解决初学者在编程...

    高中英语必修5重点词汇,短语,句子复习总结.doc

    29. solve(解决):找出并执行解决问题的方法。 30. confuse(使困惑):使人感到混淆或不明白。 【重点短语复习】 1. put forward(提出):提出意见、建议或理论。 2. draw a conclusion(得出结论):根据已知...

    与时雨高考英语考纲词汇A.doc

    1. **able**: 形容词,意为“有能力的”,常用于表达某人具备做某事的能力,如:She is able to solve complex math problems. (她能解决复杂的数学问题。) 2. **abnormal**: 形容词,表示“不正常的”,常用于描述...

    专升本英语复习资料全.doc

    - `ability`: 指人或物具有的能力,如:`She has the ability to solve complex problems.` - `enable`: 使某人具有做某事的能力,如:`The new technology enables us to communicate faster.` - `capable`: 也...

Global site tag (gtag.js) - Google Analytics