Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to identify memory leaks in Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to identify memory leaks in Java. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Identification of memory leaks

It is necessary to check for memory leaks before deploying the program to a production environment. Here, a preliminary judgment can be made by the indicators of the garbage collector.

If memory usage continues to rise after GC, then there may be memory leaks, such as the image above, where the code can look at GitHub (https://gist.github.com/dpryden/b2bb29ee2d146901b4ae). However, in reality, the possibility of a linear increase in memory as shown in the figure is very small, as shown in figure Old Gen, while the use of GC suspension times or Eden Space and Survivor space is not sufficient to identify memory leaks.

Narrow the scope of the problem

There are many tools available to find out the cause of the memory leak, such as JVisualVM or jStat. These tools come with JDK, so everyone can use them at any time. In addition to identifying some commonly used internal Java classes, some user-defined tiredness also needs to be identified.

Performance optimization

In the daily development process, as long as GC does not affect performance, developers will not pay attention to the memory setting in the configuration. Thus burying the potential danger: because memory problems are not just spills and leaks, too long GC time can also cause this problem. For example, GC takes up 16% of CPU in the following figure.

Heap Settin

Too small a Heap can lead to frequent GC, so it's not hard to imagine a scenario: adding GC consumes more CPU, while JVM is frozen during GC, resulting in poor performance. Generally speaking, if Hap is too small, the GC time will become shorter, but it will become more frequent.

Too much Heap results in long GC time. GC does not happen very often, but once triggered, the VM will be frozen for a long time.

Therefore, if a memory leak occurs in this case, the JVM will be very frequent or take a long time before the GC finally crashes due to a memory overflow.

GC version

GC has changed a lot since Java 6. Java 7 introduced G1GC as an alternative to CMS GC, while G1GC has become the default choice in Java 9. PermGen Space is removed from Java 8, and data previously stored in PermGen Space is stored in local memory or stack instead.

On how to identify memory leaks in Java to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report