Java – how to improve applications to avoid heap space problems

I have an application that works with many custom objects created in methods and never needs to be outside of them The whole structure (in my opinion) is very object - oriented and uses services, utilities and di model

Now, when I run my first "large" test, I quickly encounter outofmemoryexceptions Now, I don't just want to increase the heap space and finish it, because I can imagine that this won't solve the problem, but delay it until my application grows more and encounters the same problem

I'm looking for some easy-to-use solutions, tricks and fragments that can help applications deal with garbage collection and heap space, especially when it involves many loops that use object creation operations

Things like "don't create objects in the loop, create them before the loop and overwrite it"

Solution

I'll first analyze your application and find memory hotspots using jvisualvm (part of the JDK) This will provide you with and indicate how large your object is and which method calls cause high memory usage It also tells you how long an object lasts in memory, which is usually a good starting point because you want to narrow it down as short as possible

The next step is to identify commonalities in objects by improving the design or implementing caching If you are loading data from fixed storage, you can use soft references so that when the JVM runs out of heap, these objects will be GC (if you make changes to these objects, you obviously need to keep them before deleting the hard references) Then, if you need them again, your application will only need to reload them from backup storage (DB, file, or other)

Make sure you understand how GC works and understand object references:

>Strong / direct > soft > weak > ghost

Here are some good articles explaining references and GC:

http://www.java-tips.org/java-se-tips/java.util/using-weakhashmap-for-listener-lists.html

http://pawlan.com/monica/articles/refobjs/

@L_ 301_ 2@

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>