Stability is an important index to measure the quality of software system, and memory leakage is an important factor to destroy system stability. Because of the garbage collection mechanism, the pattern of memory leaks in the Java language differs greatly from that of C + +. The full text compares the memory leak problem in C + +, tells the basic principle of Java memory leak, and how to test memory leak and analyze the reason of memory leak with the help of Optimizeit Profiler tool, prove this is a set of effective method in practice.
Keywords Java; Memory leaks; GC (garbage collector) reference; Optimizeit
The question of the proposed
I have been involved in the development of network management systems, the system is large, involving millions of lines of code. The system mainly uses the Java language development, basically divides into the client, the server and the database three levels. In the version into the test and trial process, the field personnel and Test department personnel have reflected: the stability of the system is relatively poor, often appear server-side running day and night on the phenomenon of death, the client run dead phenomenon is also more frequent occurrence. For the network management system, the frequent server panic is a relatively serious problem, because the frequent panic not only may lead to inconsistent data, error, more will cause users dissatisfaction, reduce customer trust degree. Therefore, server-side stability issues must be resolved as soon as possible.
Solving ideas
By looking at the server-side log, it is found that the server side frequently throws OutOfMemoryException memory overflow error before the crash, so the cause of the crash is initially positioned as a memory leak that causes memory shortages, which in turn leads to memory overflow errors. How do I find out what caused the memory leak? There are two kinds of ideas: the first one is to arrange the experienced programmers to check and analyze the code to find out the location of the memory leak, and the second, use the Special memory leak test tool Optimizeit to test. These two methods are the effective means to solve the system stability problem, and the memory test tool is very effective for locating and solving the memory leak problem that has been exposed, but the theory of software testing also tells us that there are always some problems in the system that are not exposed, and The stability problem of the system is not only the problem of memory leakage, but the code checking is an effective means to improve the whole code quality of the system and even solve the potential problems. Based on this consideration, our memory stability work decided to use the code to check the combination of testing tools, two-pronged approach, to achieve a more thorough solution to the stability of the system.
In the work of code walking, the developers who are familiar with the system business and the development language tools are arranged to cross-check the application code, find out the database connection declaration and result set, code redundancy and inefficiency, etc, and get good results. This paper mainly describes the method of locating the memory leak problem with the use of the tool.
Fundamentals of memory leaks
In C + + language programs, objects created using the new operator should be released by the delete operator after use, otherwise, these objects will occupy the heap space, never be able to get recycled, resulting in memory space leakage. A simple code like this can cause memory leaks:
void function(){
Int[] vec = new int[5];
}
After the function () method completes, the VEC array is already unreachable, and in the C + + language, such an object is never released, calling it a memory leak.
Java, which manages memory recycling automatically through the garbage collector (garbage COLLECTION,GC), does not need to invoke functions to free up memory, but it can only reclaim space occupied by objects that are useless and are no longer referenced by other objects. In the following code, the object is iterated, and the requested object is placed in a vector, and if only the object itself is released, but because the vector still references the object, it is not recyclable for the GC. Therefore, if the object is added to the vector, it must also be removed from the vector, and the simplest 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;
}//此时,所有的Object对象都没有被释放,因为变量v引用这些对象。