Java's memory leak
content:
The question is how Java manages memory what is the memory leak in Java how to detect memory leaks related resources about the author
One of the important advantages of a problem with Java is that the programmer does not need to release the memory by calling a function by automatically managing memory by garbage collection, gc. Therefore, many programmers believe that Java does not have memory leaks, or that even if there is a memory leak, it is not the responsibility of the program, but the problem with GC or JVM. In fact, this idea is incorrect because Java also exists in memory, but its performance is different from C . As more and more server programs use Java technology, such as JSP, servlet, EJB, etc., server programs are often run for a long time. In addition, in many embedded systems, the total amount of memory is very limited. Memory leakage issues also become very critical, even after each running a small amount of leakage, the system is also dangerous in the crash after long-term operation. II Java how to manage memory in order to determine if there is memory leak in Java, we must first understand how Java manages memory. Java's memory management is the allocation and release of objects. In Java, programmers need to apply for memory spaces for each object via the keyword NEW, all objects allocate space in heap (HEAP). In addition, the release of the object is determined and executed by the GC. In Java, the distribution of memory is done by the program, and the release of memory is completed by GC. This method of paying two lines does have simplified programmers. But at the same time, it also added JVM's work. This is also one of the reasons why Java program is slower. Because GC must monitor the correct release of objects, GC must monitor the running status of each object, including the application, reference, reference, assignment, etc., GC needs to monitor. The monitoring object status is to more accurately, timely release objects, and the root of the release object is that the object is no longer referenced. In order to better understand the working principle of GC, we can consider the object to be a vertex of the map, accounting the reference relationship as a view of the map, and pointing from the reference to the subject. Alternatively, each thread object can be used as a starting top point of a graph, such as most programs start from the MAIN process, then the figure is a root tree starting at the top of the MAIN process. In this figure, the root top can be a valid object, and the GC will not reclaim these objects. If an object (connecting sub-map) is unreachable with this root point (note that the picture is a map), then we think this (these) objects are no longer referenced, which can be recycled by GC. Hereinafter, we will give an example to indicate how to use a map to represent memory management. For every moment of the program, we have a memory allocation of the JVM to represent the JVM. The following picture is a schematic diagram of the left program runs to Chain 6.
Java uses a way to perform memory management in a way that can eliminate the problem of reference loops, such as three objects, citing each other, as long as they are not arrogant, the GC can also recover them. The advantage of this approach is that the management of memory is high, but the efficiency is low. Another commonly used memory management technology is a counter, such as a COM model, a counter mode management component, which is a low precision line (it is difficult to handle cyclic references) compared to the map, but the execution efficiency is high. Three What is the memory disclosure in Java, and we can describe what is memory leak. In Java, memory leaks exist some assigned objects, which have the following two features. First, these objects are up, that is, in the figure, there is a path to be connected thereto; secondly, these objects are useless These objects will not be used after the program will be used later. If the object satisfies these two conditions, these objects can be determined as memory leaks in Java, which will not be recovered by GC, but it takes up memory. In C , the range of memory leaks is larger. Some objects are assigned a memory space, and then they are not arrogant. Due to the GC in C , these memory will never receive it. In Java, these unreachable objects are responsible for recycling by GC, so the programmer does not need to consider this part of the memory leak. Through analysis, we know that for C , programmers need to manage their side and vertices themselves, and for Java programmers only need to manage edges (do not need to manage vertices release). In this way, Java improves the efficiency of programming.
Therefore, through the above analysis, we know that there is also memory leak in Java, but the range is smaller than C . Because Java guarantees from language, any object is up to, and all non-objects are managed by GC. For programmers, GC is basically transparent and invisible. Although we only have several functions to access GC, such as running GC's function system.gc (), but according to Java language specification definition, the function does not guarantee the JVM garbage collector will be executed. Because, different JVM implementations may use different algorithms to manage GC. Typically, the priority of the GC thread is lower. There are many kinds of JVM calling GC, and some are when memory uses to a certain extent, GC starts working, and there is a timed execution, some are gentle execution GC, and some are interrupt implementation GC. But in general, we don't need to care about it. Unless otherwise on some specific occasions, GC performs the performance of the application, such as web-based real-time systems, such as online games, etc., users do not want GC to suddenly interrupt the application execution and garbage recycling, then we need to adjust GC parameters Allow GC to release memory through a gentle manner, for example, decompose garbage recovery into a series of small steps, and the HotSpot JVM provided by Sun supports this feature. An example of a simple memory leak will be given below. In this example, we loop to apply for the object object and put the object applied in a vector. If we only release the reference itself, then the vector still references the object, so this object is not recyclable to the GC. Therefore, if the object is added to the Vector, you must also delete from the vector, the easiest way is to set the Vector object to NULL. Vector v = new vector (10); for (int i = 1; i <100; i ) {Object o = new object (); v.add (o); o = null;} / / At this time, all Object objects are not released because the variable V references these objects. Festening how to detect the last important issue of memory leaks, how to detect Java's memory leakage. Currently, we usually use some tools to check the Memory leak problem of the Java program. There are several specializes in the market, and the basic working principle is similar, and all the information of all objects, all the information, etc., and analyze, and Visualization. Developers will determine whether the program has a memory leak problem based on this information. These tools include Optimizeit Profiler, JProbe Profiler, Jinsight, Rational's Purify, etc. Below, we will briefly introduce the basic functions and working principles of Optimizeit. Optimizeit Profiler version 4.11 supports the application, Applet, Servlet, and Romote Application four-class applications, and supports most types of JVM, including Sun JDK series, IBM JDK series, and JBuilder JVM. Also, the software is written by Java, so it supports a variety of operating systems. The OptimizeIt family also includes two tools for THREAD DEBUGER and CODE COVERAGEs, respectively, which are used to monitor running status and code coverage at runtime.
When all parameters are set, we can run the test program in the Optimizeit environment. During the program running, Optimizeit can monitor the usage curve of the memory (such as the following figure), including the size of the JVM application, and The actually used memory size. In addition, during operation, we can suspend the operation of the program at any time, even forcibly calling the GC, allowing the GC to recycle. With the memory usage curve, we can understand the case where the program is used. This monitoring is necessary for long-term operational applications, it is also easy to find memory leaks. During the operation, we can also check the usage of memory from different perspectives, Optimizeit provides four ways:
Pile angle. This is a comprehensive perspective, we can understand all object information (quantity and type) in the heap, and perform statistics, sort, filtering. Understand the changes of related objects. Method view. Through the way of view, we can learn which methods are allocated in which methods are allocated, and their number. Object perspective. Given an object, through an object perspective, we can display all of its outfit and reference objects, we can understand all references to this object. Quote. Given a root, by reference diagram, we can display all the references from the vertex. During operation, we can observe the usage of memory at any time. In this way, we can quickly find objects that are not released for a long time, and no longer use. By checking whether it is a memory leak by checking the survival cycle of these objects. In practice, looking for memory leaks is a very troublesome thing, it requires programmers to be clearer for the entire program, and requires rich debugging experience, but this process is very important for many key Java programs. In summary, Java also has memory leak issues, whose reason is mainly some objects that are no longer used, but they are still referenced. In order to solve these problems, we can check the memory leaks through software tools. The main principle of check is to expose objects in all stacks, so that programmers find objects that are useless but still quote.