Java improvements-A Brief Introduction to Java memory leaks and a brief introduction to java

Source: Internet
Author: User

Java improvements-A Brief Introduction to Java memory leaks and a brief introduction to java

The most obvious advantage of java is its memory management mechanism. You only need to create an object. java's garbage collection mechanism is responsible for allocating and releasing memory. However, the situation is not as simple as imagined, because memory leaks often occur in Java applications.

This tutorial demonstrates what is memory leakage, why is memory leakage, and how to prevent memory leakage.

What is memory leakage?

Definition: If objects are no longer used in applications, but they are referenced elsewhere, garbage collection cannot remove them (as a result, a lot of memory cannot be released, this causes memory overflow. ).

To understand this definition, we need to understand the status of objects in the memory. It indicates that those are not used and those are not referenced.

You can see the referenced object and the unreferenced object (range ). Unreferenced objects can be recycled by the garbage collection mechanism, but referenced objects cannot be recycled by the garbage collection mechanism. Objects that are not referenced are of course not used, because no other object references it. However, objects that are not used are not referenced. Some unused objects are still referenced elsewhere! This is the cause of Memory leakage.

Why is memory leakage?

Let's take the following example to see why Memory leakage occurs. In the following example, object A references object B. A's life cycle (t1-t4) is much longer than B's life cycle (T2-T3), and when B is no longer used in the application, A still holds A reference to B. In this way, the garbage collection mechanism cannot remove B from the memory. This may cause memory overflow, because if many other objects are like A, there will be A lot of objects in the memory that cannot be recycled, which will consume A lot of memory space.

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

How to Prevent Memory leakage?

The following are some quick tips to prevent memory leakage:

1. Pay attention to collection classes, such as HashMap and ArrayList, because they are frequent memory leaks. When they are declared as static objects, their lifecycle is as long as that of the application.

2. Pay attention to the event listener and callback. If a class registers a listener, but the listener is not logged out after the class is no longer used, memory leakage may occur.

3. "If a class manages its own memory, the programmer should guard against memory leaks ." [1]. In many cases, the variable pointing to other object members must be set to null (to be recycled ).

A quiz: Why does the substring () method in JDK 6 cause memory leakage?

To answer this question, you may need to check the source code of substring () in JDK 6 and 7.

References:
1. Bloch, Joshua. Valid tive java. Addison-Wesley Professional, 2008.
2. IBM Developer Work. http://www.ibm.com/developerworks/library/j-leaks/

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.