Understand memory leaks in Java and examples of Solutions

This paper introduces the principle of JAVA memory management and the causes of memory leakage in detail, and provides some solutions to JAVA memory leakage, hoping to be helpful to Java developers.

JAVA memory management mechanism

In C + + language, if a piece of memory needs to be allocated dynamically, the programmer needs to be responsible for the whole life cycle of this memory. From application for distribution, to use, and then to the final release. This process is very flexible, but it is very cumbersome. Programmers can easily forget to release memory due to negligence, resulting in memory leakage. The Java language makes its own optimization for memory management, which is the garbage collection mechanism. Almost all memory objects in Java are allocated on heap memory (except basic data types), and then GC (garbage collection) is responsible for automatically reclaiming memory that is no longer used.

The above is the basic situation of JAVA memory management mechanism. However, if we only understand this, we will still encounter the problem of memory leakage in the actual project development. Some people may doubt that since Java's garbage collection mechanism can automatically recycle memory, how can memory leakage occur? To solve this problem, we need to know when GC reclaims memory objects and what memory objects will be considered "no longer used" by GC.

Access to memory objects in Java uses the method of reference. In Java code, we maintain a reference variable of a memory object. Through the value of this reference variable, we can access the memory object space in the corresponding memory address. In Java programs, the reference variable itself can be stored in heap memory, It can also be placed in the memory of the code stack (the same as the basic data type). The GC thread will track from the reference variables in the code stack to determine which memory is being used. If the GC thread cannot track a heap memory in this way, the GC will think that this memory will no longer be used (because the code can no longer access this memory.).

Through this directed graph memory management method, when a memory object loses all references, GC can recycle it. Conversely, if this object still has a reference, it will not be recycled by GC, even if the Java virtual machine throws outofmemoryerror.

JAVA memory leak

Generally speaking, there are two kinds of memory leaks. For example, in C / C + + language, the allocated memory in the heap is not released, Delete all the ways that can access this memory (such as pointer re assignment); in another case, when the memory object is clearly no longer needed, it still retains this memory and its access method (Reference). The first case has been well solved in Java due to the introduction of garbage collection mechanism. Therefore, memory leakage in Java mainly refers to the second case. Maybe the concept is too abstract. You can take a look at such examples:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>