"Simple Java" Java Memory leak brief

Source: Internet
Author: User

One of the most significant advantages of Java is memory management. You simply need to create the object without being responsible for freeing the space, because the Java garbage collector is responsible for the recycling of the memory. However, the situation is not so simple that memory leaks often occur in Java applications.

This article describes what memory leaks are, why they occur, and how to prevent memory leaks.

What is a memory leak

Memory leak definition: For applications, memory leaks occur when objects are no longer being used, but the Java garbage collector cannot reclaim them.

To understand this definition, we need to understand the state of the object in memory. As shown, shows which objects are useless objects and which are unreferenced objects;

Unreferenced objects and reference objects are included in the. Unreferenced objects are reclaimed by the garbage collector, but not by reference objects. Unreferenced objects are obviously useless objects. However, useless objects are not all unreferenced objects, and some useless objects may also be reference objects, and this part of the object is the source of memory leaks.

Why memory leaks can occur

Let's use the following example to see why a memory leak occurs. As shown, object a references the object B,a life cycle (T1-T4) longer than B's life cycle (T2-T3), and when B is no longer used in the program, a still references B. In this case, the garbage collector does not reclaim the B object, which can cause an out-of-memory problem, because a may not only refer to the B object, but may also refer to other objects with a shorter life cycle than a, resulting in a large number of useless objects that cannot be recycled and occupy expensive memory resources.

Similarly, the B object may refer to a large number of objects that are referenced by the B object and cannot be reclaimed by the garbage collector, and all of these useless objects consume a large amount of memory resources.

How to prevent memory leaks

Here are some experience tips to block memory leaks:

    1. Pay more attention to collection classes, such as hashmap,arraylist, which often occur with memory leaks. For example, when they are declared as static objects, their life cycles are as long as the application's life cycle, which can easily cause memory shortages.
    2. More attention to event monitoring (listeners) and callbacks (callbacks), such as registering a listener, when it is no longer in use, forget to log out of the listener, a memory leak may occur.
    3. "If a class manages its own memory space, the developer needs to be particularly wary of memory leaks," the member variables referencing other objects are remembered to be set to null? Not quite understood (original: Often times member variables of an object, the point to the other objects need to being null out);
quiz: Why JDK 6 's substring () method can cause memory leaks

For details, please see the following links:
http://www.programcreek.com/2013/09/the-substring-method-in-jdk-6-and-jdk-7/

Link: http://www.programcreek.com/2013/10/the-introduction-of-memory-leak-what-why-and-how/

"Simple Java" Java Memory leak brief

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.