Also say Java Memory leak

Source: Internet
Author: User
Tags int size

Is there a memory leak in Java? Yes. While some people say that this is inaccurate, in C + + programs, the memory that is opened up by the current process but not logically managed by the current process is called memory that is leaked by the process. In fact, Java will also have this situation.

When we first touched Java it was like it because it automatically manages memory without the convenience of programmer manual intervention, but this automatic is not omnipotent. For some hidden references caused by memory leaks, sometimes for a long time or even months, several years we are very difficult to find, unless the very experienced person to carefully check the source code, borrow heap analysis tools to analyze carefully to discover.

The simplest Java memory leak is an implementation of a data structure. Like what:

Class Innerstack<e> {

    private int size;
    private int initialcapacity =;
    Private e[] OS;

    @SuppressWarnings ("unchecked") public
    Innerstack () {
        OS = (e[]) new object[initialcapacity];

    public void push (E o) {
        checkcapacity ();
        this.os[size++] = o;
    }

    Public E pop () {
        if (size <= 0) throw new Emptystackexception ();
        E o = (e) this.os[--size];
        This.os[size] = null; 1 return
        o;
    }

    Public E Peek () {
        if (size <= 0) throw new Emptystackexception ();
        E o = (e) this.os[this.size-1];
        return o;
    }

    @SuppressWarnings ("unchecked")
    private void Checkcapacity () {
        if (size = = os.length) {
            OS = (e[)) New Obje Ct[size * 2 + 1];}}


In the case of Java 99.9%, we do not need to write type var = null; This syntax, but in the data structure of our own implementation, if the object pointed to by the example in note 1 above is os[10 by pop out, the caller is used up the idea is to hope it can be recycled by the system, However, because there is also an internal array in the Innerstack that has a reference to it, the object is not immediately marked as recyclable after the caller has finished using it.

If the same method implements the loop queue, the situation is somewhat improved, even if the reference is not interrupted, the original reference will be overwritten by the subsequent reference when the team element is more than the length of the data. But for the automatic expansion of the stack, if a moment capacity to a peak, such as the bottom number of the leader is extended to 65, in os[64] if the pop out after not set

OS[64] = null; then you will not be able to access this location (peak value) for a long time then the object it points to has been "used" by the outside, but cannot be recycled, so don't underestimate these objects, because they will also refer to other objects. Relationships are complex and can cause a large memory leak.

Similarly, if the underlying data structure, such as list objects support, if the object is acquired, the underlying object container is promptly removed, whether call clear, will cause the object is inadvertently referenced and cannot be recycled. When you need to cache objects first and then get used, it is best to choose a weak reference data structure such as Weakhashmap so that objects can be recycled as soon as they are acquired externally.

Effective Java mentions listeners and callbacks when it comes to other memory leaks, but none of this is important because after a listener is add, you are not sure when it should be erased, Because most listener are the same as the lifecycle of the application, they are not recycled unless you deal with them in the logic before exiting. But in actual applications that cause a lot of memory leaks, the use of NiO must be very, very careful.

In a lot of time, an IO channel of NIO cannot read all the transmitted data at one time, so we always have a risk of selectorkey the data in our own data structure, or attach it to the Selectorkey. The key is the corresponding client may be in a very bad network or simply disconnected, you register the event does not return the results, then save the original data can never be processed. There are also problems with write operations. So be sure to start the registration time and selector has handled the number of times attach to the key and then each processing time to judge, for after many processing and over a certain period of time did not read to complete the data you want to do the corresponding processing, Release your data structure that handles this link in time.

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.