In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of "how to check Java memory leaks". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to check Java memory leaks" can help you solve the problem.
How Java manages memory:
The memory management of Java is the problem of object allocation and release. In Java, programmers need to request memory space for each object (except the basic type) through the keyword new, and all objects allocate space in the Heap. In addition, the release of the object is determined and executed by the GC. In Java, the allocation of memory is done by the program, while the release of memory is done by GC. This two-line approach really simplifies the work of programmers. But at the same time, it also aggravates JVM's work. This is also one of the reasons why Java programs run slowly. Because, in order for GC to release objects correctly, GC must monitor the running status of each object, including object application, reference, referenced, assignment and so on. GC needs to monitor.
The purpose of monitoring the state of the object is to release the object in a more accurate and timely manner, and the fundamental principle of releasing the object is that the object is no longer referenced.
Java uses a directed graph for memory management. If an object is unreachable from the root vertex, then we think that the object is no longer referenced and can be reclaimed by GC.
The problem of reference loops can be eliminated in the way of digraphs, for example, there are three objects that reference each other, and GC can also recycle them as long as they are unreachable from the root process. The advantage of this approach is that the precision of managing memory is high, but the efficiency is low. Another commonly used memory management technique is to use counters. For example, the COM model uses counters to manage components, which is less precise than a directed graph (it is difficult to deal with circular references), but it is very efficient.
Memory leaks in Java and C++ compare:
In Java, a memory leak means that there are some allocated objects, which have the following two characteristics: first, these objects are reachable, that is, in a directed graph, there are paths that can be connected to them; second, these objects are useless, that is, the program will never use these objects again. If the objects meet these two conditions, the objects can be determined as memory leaks in Java, these objects will not be recycled by GC, but it takes up memory. Unconscious memory retention (called unconscious object retention in many places).
In C++, the range of memory leaks is larger. Some objects are allocated memory space that is then unreachable, and because there is no GC in C++, this memory will never be recovered. In Java, these unreachable objects are recycled by GC, so programmers do not need to consider this part of the memory leak.
GC:
Typically, threads in GC have a lower priority. There are also many strategies for JVM to call GC. Some start to work when memory usage reaches a certain level, some are executed regularly, some are slow execution of GC, and some are interrupted execution of GC. But generally speaking, we don't need to care about that. Unless in some specific situations, the execution of GC affects the performance of the application, for example, for real-time systems based on Web, such as online games, users do not want GC to suddenly interrupt the execution of the application and carry out garbage collection, then we need to adjust the parameters of GC so that GC can release memory in a gentle way, such as decomposing garbage collection into a series of small steps. HotSpot JVM provided by Sun supports this feature.
How to check for memory leaks:
Currently, we usually use some tools to check for memory leaks in Java programs. There are several professional tools on the market to check Java memory leaks, and their basic working principles are more or less the same. They all count, analyze and visualize all the information of memory management by monitoring the application and release of all objects when the Java program is running. The developer will use this information to determine whether the program has a memory leak. These tools include Optimizeit Profiler,JProbe Profiler,JinSight, Purify of Rational and so on.
Optimizeit Profiler version 4.11 supports Application,Applet,Servlet and Romote Application applications, and can support most types of JVM, including SUN JDK series, IBM's JDK series, and Jbuilder's JVM. Moreover, the software is written by Java, so it supports a variety of operating systems.
The Optimizeit series also includes two tools, Thread Debugger and Code Coverage, for monitoring thread status and code coverage at runtime, respectively.
When all the parameters are set, we can run the program under test in the OptimizeIt environment. During the program run, Optimizeit can monitor the memory usage curve, including the size of the heap requested by JVM, and the actual memory used. In addition, in the process of running, we can pause the running of the program at any time, or even forcibly call GC, and let GC reclaim memory. Through the memory usage curve, we can understand the overall memory usage of the program. This monitoring is necessary for long-running applications, and it is easy to detect memory leaks.
During the operation, we can also look at memory usage from different perspectives. Optimizeit provides four ways:
* heap perspective. This is a comprehensive perspective, we can understand all the object information (quantity and type) in the heap, and count, sort, and filter. Learn about the changes of related objects.
* method perspective. From the method perspective, we can know which methods each kind of objects are assigned to, and the number of them.
* object perspective. Given an object, from the object perspective, we can display all its outgoing and incoming reference objects, and we can understand all the reference relationships of this object.
* refer to the figure. Given a root, by referencing the graph, we can display all outgoing references from that vertex.
During the run, we can observe memory usage at any time, and in this way, we can quickly find objects that have not been released for a long time and are no longer in use. We check the lifecycle of these objects to see if they are memory leaks. In practice, finding memory leaks is a very troublesome thing, it requires programmers to be clear about the code of the whole program, and requires rich debugging experience, but this process is very important for many key Java programs.
This is the end of the content about "how to check for Java memory leaks". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.