Memory leakage in Java

Source: Internet
Author: User

In Java, memory leakage is the existence of some allocated objects. These objects have the following two features: 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 isProgramThese objects will not be used in the future. If the objects meet these two conditions, these objects can be identified as memory leaks in Java. These objects are not recycled by GC, but occupy the memory.

In C ++, the memory leakage scope is larger. Some objects are allocated with memory space but cannot be accessed. Because c ++ does not have GC, these memories will never be recovered. In Java, GC is responsible for the collection of these inaccessible objects, so programmers do not need to consider the memory leakage.

Through analysis, we know that for C ++, programmers need to manage edges and vertices themselves, while Java programmers only need to manage edges (no need to manage the release of vertices ). In this way, Java improves programming efficiency.

Therefore, through the above analysis, we know that memory leakage also exists in Java, but the range is smaller than that in C ++. Because Java ensures that all objects are reachable, and all unreachable objects are managed by GC.

For programmers, GC is basically transparent and invisible. Although only a few functions can access GC, for example, the system. GC () function that runs GC, according to Java language specification definition, this function does not guarantee that the JVM garbage collector will certainly execute. Because different JVM implementers may use differentAlgorithmManage GC. Generally, the priority of GC threads is low. There are also many policies for JVM to call GC. Some of them start to work when the memory usage reaches a certain level, some are scheduled, some are gentle execution of GC, and some are interrupted execution of GC. But in general, we don't need to care about this. Unless in some specific scenarios, GC execution affects the performance of applications, such as web-based real-time systems, such as online games, if you do not want the GC to suddenly interrupt application execution and perform garbage collection, you need to adjust the GC parameters so that the GC can release the memory in a gentle manner, for example, to break down garbage collection into a series of small steps for execution, Sun provides the hotspot
JVM supports this feature.

 

The following is a simple example of Memory leakage. In this example, we cyclically apply for an object and put the applied object into a vector. If we only release the reference, the vector still references the object, therefore, this object cannot be recycled for GC. Therefore, if an object is added to a vector, it must be deleted from the vector. The simplest way is to set the vector object to null.

 

Vector <Object> V = new vector <Object> (10 );
For (INT I = 1; I <100; I ++ ){
Object o = new object ();
V. Add (O );
O = NULL;
}
// Procedure of using V
// V = NULL;
At this time, all object objects are not released because variable v references these objects.

Http://www.ibm.com/developerworks/cn/java/l-JavaMemoryLeak/index.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.