Java Memory leak Simple description

Source: Internet
Author: User

One of the key advantages of the Java language is its memory management mechanism. You just create objects, and the Java garbage collector helps you allocate and reclaim memory. However, the actual situation is not as simple as memory leaks occur in Java applications.

The following explains what a memory leak is, why it happens, and how we can prevent it from happening.

1. What is a memory leak?

Memory leak definition: Objects have not been used by the application, but the garbage collector is not able to remove them because they are still being referenced.

To understand this definition, we need to look at the state of the object in memory first. The following diagram explains what a useless object is and what is unreferenced.

As can be seen in the above figure, there are referenced objects and unreferenced objects. Unreferenced objects are reclaimed by the garbage collector and the referenced objects do not. An object that is not referenced is, of course, an object that is no longer being used because no object references it again. However, useless objects are not all unreferenced objects. There are also references to them. This is the case that causes a memory leak.

2. Why is there a memory leak?

Let's take a look at the following example, why a memory leak occurs. In this example, thea object refers to the B object, and the life cycle of thea object (T1-T4) is much longer than the life cycle (T2-T3) of the B object. When the B object is not used by the application, theA object is still referencing the B object. In this way, the garbage collector is not able to remove the B object from memory, which can cause memory problems, because if a references more such objects, there will be more unreferenced objects and consume memory space.

The B object may also hold many other objects, and those objects will not be recycled by the garbage collector. All of these objects that are not in use will continue to consume the allocated memory space before they are consumed.

3. How do I prevent memory leaks from happening?

Here are a few easy tips to help you prevent memory leaks from happening.

    • Pay particular attention to some collection objects like HashMap, ArrayList , which often cause a memory leak. When they are declared as static , their lifetime is as long as the application.
    • Pay particular attention to event snooping and callback functions. When a listener is registered while it is in use, it is not unregistered after it is no longer in use.
    • "If a class manages its own memory, developers have to be careful about memory leaks." "Usually some member variables refer to other objects, which need to be empty when initializing.
4. A small question: why JDK6 in the substirng ()Method easily leads to memory leaks?

Reference Link: http://www.importnew.com/12961.html

Simple description 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.