In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to solve the memory leak problem of Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The raising of a question
One of the important advantages of Java is that it automatically manages memory collection through the garbage collector (Garbage Collection,GC). Programmers do not need to call functions to free memory. As a result, many programmers believe that there is no memory leak in Java, or that even if there is a memory leak, it is not the responsibility of the program, but the problem of GC or JVM. In fact, this idea is not true, because Java also has memory leaks, but its performance is different from that of C++.
As more and more server programs adopt Java technology, such as JSP,Servlet, EJB and so on, server programs often run for a long time. In addition, the total amount of memory is very limited in many embedded systems. The problem of memory leakage becomes very critical, even if there is a small amount of leakage in each run, the system is in danger of crashing after a long run.
How does Java manage memory?
To determine if there is a memory leak in Java, we must first understand 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.
In order to better understand the working principle of GC, we can consider the object as the vertex of the directed graph and the reference relation as the directed edge of the graph, and the directed edge points from the referrer to the cited object. In addition, each thread object can be used as the starting vertex of a graph. For example, most programs start from the main process, so the graph is a root tree that starts with the vertex of the main process. In this digraph, objects whose root vertices are reachable are valid objects, and GC will not recycle these objects. If an object (connected subgraph) and this root vertex are not reachable (note that the graph is a directed graph), then we think that this (these) objects are no longer referenced and can be recycled by GC.
Below, let's give an example of how to use a directed graph to represent memory management. For each moment of the program, we have a directed graph showing the memory allocation of the JVM. The right picture below is a schematic diagram of the program running to line 6 on the left.
Java uses directed graph for memory management, which can eliminate the problem of reference loop, for example, there are three objects that reference each other, as long as they and the root process are unreachable, then GC can also recycle them. 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.
What is a memory leak in Java
Below, we can describe what a memory leak is. 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.
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.
Through analysis, we know that programmers need to manage edges and vertices on their own for cantilever, while for Java programmers only need to manage edges (there is no need to manage vertex release). In this way, Java improves programming efficiency.
Therefore, from the above analysis, we know that there are memory leaks in Java, but the scope is smaller than C++. Because Java guarantees from the language that any object is reachable, all unreachable objects are managed by GC.
To programmers, GC is basically transparent and invisible. Although we only have a few functions that can access GC, such as the function System.gc () that runs GC, according to the Java language specification, this function does not guarantee that JVM's garbage collector will execute. Because different JVM implementers may use different algorithms to manage 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, which is supported by HotSpot JVM provided by Sun.
A simple example of a memory leak is given below. In this example, we looped through the Object object and put the requested object into a Vector. If we only release the reference itself, then the Vector still references the object, so the object is not recyclable to GC. Therefore, if an object has to be deleted from the Vector after it has been added to the Vector, the easiest way is to set the Vector object to null.
Vector v=new Vector (10)
For (int iTunes 1)
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.