Java improvements--A brief introduction to Java's memory leaks

Source: Internet
Author: User

Read Catalogue

    • What is a memory leak?
    • Why is there a memory leak?
    • How do I prevent memory leaks?

One of the most obvious advantages of Java is its memory management mechanism. You simply create objects, and Java's garbage collection mechanism is responsible for allocating and freeing memory. However, the situation is not as simple as imagined, as 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.

Back to Top What is a memory leak?

Definition: If objects are no longer used in the app, but because they are referenced elsewhere, garbage collection cannot remove them (which can cause a lot of memory to be released, causing a memory overflow phenomenon.) ).

To understand this definition, we need to understand the state of an in-memory object. Illustrates those that are not used, those that are unreferenced.

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

Back to Top Why is there a memory leak?

Let's take a look at the following example to see why a memory leak occurs. 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. In this way, 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 a lot of objects in memory that cannot be recycled, 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.

Back to Top How do I prevent memory leaks?

Here 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 the place. When they are declared as static objects, their life cycle is as long as the life cycle of the application.

2. Be aware of 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 pointer to other object member variables need to be set to null (to be recycled).

A quiz: why does the substring () method in JDK6 cause a memory leak?

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

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

From:http://www.cnblogs.com/qian123/p/5706440.html

Java improvements--A brief introduction to Java's memory 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.