The Introduction of Java Memory Leaks

Source: Internet
Author: User

One of the most significant advantages of Java are its memory management. You simply create objects and Java garbage Collector takes care of allocating and freeing memory. However, the situation is not as simple as, because memory leaks frequently occur in Java applications.

This tutorial illustrates what are memory leak, why it happens, and how to prevent them.

1. What is Memory Leak?

Definition of Memory Leak: Objects is no longer being used by the application, but garbage Collector can not rem Ove them because they is being referenced.

To understand the this definition, we need to understand objects status in memory. The following diagram illustrates what are unused and what is unreferenced.

From the diagram, there is referenced objects and unreferenced objects. Unreferenced objects would be garbage collected and while referenced objects would not be garbage collected. Unreferenced objects is surely unused, because no other objects refer to it. However, unused objects is not all unreferenced. Some of them is being referenced! That's where the memory leaks come from.

2. Why Memory Leaks happen?

Let's take a look at the following example and see why memory leaks happen. In the example below, object A refers to object B. A ' s Lifetime (T1-T4) is much longer than B ' s (T2-T3). When B was no longer being used in the application, a still holds a reference to it. In the this-garbage Collector can not remove-B from memory. This would possibly cause out of memory problem, because if a does the same thing for more objects, then there would be A Lot of objects is uncollected and consume memory space.

It is also possible that B-hold a bunch of references of the other objects. Those objects referenced by B would not get collected either. All those unused objects would consume precious memory space.

3. How to Prevent Memory Leaks?

The following is some quick hands-on tips for preventing memory leaks.

    1. pay attention to Collection classes, such as HashMap, ArrayList, etc., as they is common places to find memory leaks. When they was declared  static , their life time was the same as the life time of the application.
    2. pay attention to event listeners and Callbacks. A memory leak may occur if a listener was registered but not unregistered when the class was not being used any LONGER.
    3. "If a class manages its own memory, The programer should is alert for memory leaks. " [1] Often times member variables of an object, the point to the other objects need to is null out.

4. A Little quiz:why substring () method in JDK 6 can cause memory leaks?

To answer this question, the want to read Substring () in JDK 6 and 7.

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

The Introduction of Java Memory Leaks

Related Article

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.