简体   繁体   中英

java: General approach to out-of-memory error

I have been working on this project on Java with multiple modules. Since quite some time, I have been occasionally getting "java: Out Of Memory" error! I am pretty new to this 'popular' error and wanted to know the general approach to solve such errors.
Also, are there standard tools accepted by the industry to help figure out the cause of such errors?
The modules in my project include every minute polling from a third party (using web service), multi-threading among other things. However, this is just a pointer and I seek a general approach and not something very specific to my project.
Thanks.

Sometimes you just have an class that uses a lot of memory and you need to increase the heap size or make a more space-efficient algorithm. Other times it is a leak and you need to deference objects.

  1. Run jvisualvm (it's included in the JDK).
  2. Connect to your process and try if you can to recreate the out-of-memory error while keeping an eye on the heap size.
  3. Perform a heap dump when the memory grows large. Search for the largest objects by size - often that will give you the culprit class.
  4. Look at the dependencies to see what is holding a references. If it is a memory leak make sure to dereference unneeded objects.

Also, are there standard tools accepted by the industry to help figure out the cause of such errors?

Yes, there are memory profilers such as VisualVM and YourKit . I use the latter extensively, for both CPU and memory profiling, and find it extremely useful. To get some idea of what it's capable of, take a look at this page: link .

If you can't increase the available memory you have to consume less.

Don't keep references to Objects that you don't need at the time of execution (like data you can reload dynamically) and if necessary redesign your flow (eg don't process all objects in parallel and do it sequentially) to require less memory at that time. The garbage collection should do the rest for you.

Especially if you load big data objects into memory consider to use a streaming approach if possible. Eg you don't need to load a whole file into memory if you want to search through it. You can just step through it.

Besides architectural problems you can also have leaks: keeping unintentional references to objects you don't need anymore. Since they are referenced, the garbage collector can't free the memory and you run out of memory at some point. That is probably the #1 reason for OutOfMemoryExceptions and it usually has to do with static references since classes and therefore the static s are usually not unloaded after the first time you touch a class. The internet has many articles on finding / fixing those, eg How to Fix Memory Leaks in Java

one tool I know of is MAT

You likely have a memory leak. Finding it is a challenge. Netbeans has some tools to help you profile the VM . You can profile your project and view men usage while it runs. Apache JMeter is also available as a plug-in or you can run it on its own. JMeter.apache.org

如果您经常遇到OOM,请使用正确的选项启动Java,获取堆转储,并使用jap或eclipse中的内存分析器对其进行分析(http://www.eclipse.org/mat/)

 -XX:+HeapDumpOnOutOfMemoryError   -XX:HeapDumpPath=<path to dump file>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM