Analysis of memory leak _java in Java

Source: Internet
Author: User
Tags garbage collection

One of the most obvious advantages of AVA is its memory management mechanism. You simply create objects, and the Java garbage collection mechanism is responsible for allocating and freeing memory. However, the situation is not as simple as it seems, because memory leaks often occur in Java applications.

This tutorial demonstrates what a memory leak is, why a memory leak occurs, and how to prevent a memory leak.

What is a memory leak?

Definition: If objects are no longer used in applications, but because they are referenced elsewhere, garbage collection cannot remove them (this creates a lot of memory that cannot be freed, resulting in a memory overflow phenomenon.) .


To understand this definition, we need to understand the state of objects in memory. The following figure illustrates those that are unused and those that are unreferenced.

You can see the referenced and unreferenced objects (the range) from the diagram. Unreferenced objects can be reclaimed by the garbage collection mechanism, and referenced objects cannot be reclaimed by the garbage collection mechanism. An unreferenced object is of course not used, because no other object references it. Unused objects, however, are not all unreferenced. Some unused objects are still referenced elsewhere! This is the memory leak cause.


Why is there a memory leak?

Let's take this example below to see why a memory leak can occur. In the following example, a object references a B object. The life cycle of a (T1-T4) is much longer than the life cycle of B (T2-T3), and when B is no longer used in the application, a still holds a reference to B. As a result, the garbage collection mechanism cannot remove B from memory. This is likely to cause a memory overflow problem, because if many other objects are like a, there will be many objects that cannot be reclaimed, which consumes a lot of memory space.
It is also possible that B holds a large number of references to other objects. The objects referenced by B are also not recycled. All of these unused objects will consume valuable memory space.

How do I prevent a memory leak?

The following are some quick tips for preventing memory leaks:

1, pay attention to collection classes, such as hashmap,arraylist and so on, because they are memory leaks often occur in place. When they are declared as static objects, their lifecycle is as long as the life cycle of the application.

2, pay attention to event listeners and callbacks. A memory leak can occur if a class registers a listener, but does not unregister the listener after the class is no longer in use.

3. "If a class manages its own memory, programmers should be wary of memory leaks." "[1], many times the object's point to other object member variables needs to be set to null (to be reclaimed).

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.