Home > Java > javaTutorial > What is a memory leak, the causes of memory leaks and how to prevent them?

What is a memory leak, the causes of memory leaks and how to prevent them?

伊谢尔伦
Release: 2016-11-25 15:34:35
Original
1997 people have browsed it

One of the most significant advantages of Java is its memory management mechanism. You simply create the object, and the Java garbage collection mechanism carefully allocates and frees the memory. However, it is not that simple as memory leaks often occur in Java applications.

This tutorial explains what memory leaks are, why they happen, and how to prevent them.

1. What is a memory leak?

Definition of memory leak: Objects are no longer used by the application, but the garbage collector cannot remove them because they are being referenced.

To understand this definition, we need to understand the state of objects in memory. Unreferenced objects will be recycled by the garbage collector, while referenced objects will not be recycled. An unreferenced object is unused because no other objects refer to it. However, unused objects are not necessarily unreferenced, some of them are. This is the cause of the memory leak.

2. Why does a memory leak occur?

Let’s take a look at the following example to see why memory leaks occur. In the example, object A refers to object B. A's life cycle (t1-t4) is much longer than B's life cycle (t2-t3). When B is no longer used in the application, A still holds a reference to it. In this way, the garbage collector cannot remove B from memory. This may lead to insufficient memory problems, because if A does the same thing to more objects, there will be many objects in the memory that cannot be recycled, which will extremely consume memory space.

It is also possible that B holds a large number of references to other objects, and these objects referenced by B cannot be recycled. All these unused objects consume valuable memory space.

3. How to prevent memory leaks?

Here are some quick hands-on tips for stopping memory leaks.

(1) Pay attention to collection classes, such as HashMap, ArrayList, etc. Because they are where memory leaks often occur. When they are declared static, their lifetime is as long as the application's lifetime.

(2) Pay attention to event listeners and callbacks. If a listener is registered but is not unregistered when the class is no longer used, a memory leak will occur.

(3) "If a class manages its own memory, programmers should be wary of memory leaks." [1] Many times when a member variable of an object points to another object, it needs to be set to null when it is no longer used. .

4. A short quiz: Why does the substring() method cause memory leaks in JDK6?

To answer this question, you may want to read about substring() in JDK6 and 7.

References:
[1]Bloch, Joshua.Effective Java.Addison-Wesley Professional, 2008
[2]IBM Developer Work.http://www.ibm.com/developerworks/library/j-leaks/


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template