博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Solve java.lang.OutOfMemoryError: Java heap space
阅读量:2455 次
发布时间:2019-05-10

本文共 3259 字,大约阅读时间需要 10 分钟。

 

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 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 , 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 ( needs a graphical environment), use and to generate the heap dump file. Then copy the file to your workstation and use 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 looks with a heap dump:

Heap Dump in VisualVM  

Alternatively, you can also use to read heap dumps.

转载地址:http://tbdhb.baihongyu.com/

你可能感兴趣的文章
nginx mozilla_我发现Mozilla的私人浏览模式存在重大缺陷。
查看>>
databricks_如何开始使用Databricks
查看>>
盖茨比乔布斯_如何使用盖茨比创建您的博客并通过手机进行处理
查看>>
如何使用React Native构建嵌套的抽屉菜单
查看>>
bdd cucumber_如何使用BDD构建坚如磐石的Ruby on Rails应用
查看>>
react发送和接收请求_React行为编程简介:请求,等待和阻止
查看>>
orcale可视化建立用户_建立动态可视化的新方法
查看>>
列出薪金高于在部门30_我如何在五个月内将薪金提高一倍并获得一份了不起的工作...
查看>>
gis计算各省河流长度_用河流和各方解释安全漏洞
查看>>
代码编写工具_我希望在开始编写代码时就已经知道的工具:已复习
查看>>
把转变为json_如何使用7行JSON将您的网站转变为移动应用程序
查看>>
如何使用TensorFlow对象检测API播放Quidditch
查看>>
交付方式 saas_我在全职工作时如何交付我的第一个SaaS副项目
查看>>
instagram技术_Instagram9位科技女孩进行技术采访的主要技巧
查看>>
系统在此应用程序堆栈溢出_从部署我的第一个完整堆栈Web应用程序中学到的经验教训...
查看>>
angular面试题及答案_关于最流行的Angular问题的StackOverflow上的48个答案
查看>>
zeppelin连接数据源_使用开放源代码合同(open-zeppelin)创建以太坊令牌
查看>>
ai人工智能程序_简单解释:一个AI程序如何掌握Go的古老游戏
查看>>
以下是ECMAScript 2016、2017和2018中所有新增功能的示例
查看>>
初创团队最重要的是什么_我从一家出色的初创公司工作中学到的最重要的教训...
查看>>