What happens to an Android memory leak

Source: Internet
Author: User

      • Static instances of non-static inner classes
        Because the inner class holds a reference to an external class by default, the static instance belongs to the class. Therefore, when an external class is destroyed, the inner class still holds a reference to the external class, causing the outer class to not be recycled by the GC. Thus causing a memory leak.
      • The static variables of the class hold big Data Objects
        Static variables maintain references to large data objects for a long time, preventing garbage collection.

      • Resource object not closed
        Resource objects such as cursor, Stream, Socket,bitmap, should be closed in time after use. Failure to close in finally results in an exception where the resource object is not released.

      • Registration object is not anti-registered
        We often write a lot of listener, not anti-registration will cause the Observer list to maintain the object's reference, prevent garbage collection.

      • Handler temporary memory leaks
        Handler interacts with the main thread by sending a message, and the message is stored in MessageQueue after it is sent, and some messages are not processed immediately.

      • Internal thread leaks new thread () {}.start (); one feature of thread in Java is that they are directly referenced by GC root, which means that the Dalvik virtual machine has strong references to all activated threads, causing the GC to never reclaim these thread objects. Unless the thread is manually stopped and set to null or the user directly killed the process operation. See this believe you should also have the answer in mind: I have created a thread in each mainactivity, this thread will hold the mainactivity reference, even if the current thread exits the activity because it is directly referenced by GC root so it will not be recycled. Causes Mainactivity to be recycled by GC as well. So when using a thread, be sure to consider stopping the thread and releasing it in time when the activity exits
      • Context leaks

What happens to an Android memory leak

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.