Android Memory leaks

Source: Internet
Author: User

The reasons for the memory leaks are broadly divided into the following types of Android:

memory leaks due to 1.static variables
Because the life cycle of a static variable is ended when the class unload begins when the class loads, that is, the static variable is released when the program process dies, and if the activity is referenced in the static variable, the activity is referenced, Will not be released as the lifetime of the static variable, causing a memory leak.

Workaround:
When the activity is referenced by a static variable, use Getapplicationcontext because the application life cycle is the same as the static variable from the beginning of the program to the end.

2. Memory leaks caused by threads
Similar to the case in the example above, thread execution takes a long time and activity jumps out of the box because the thread or runnable is the acticvity inner class, so there is an instance of activity (because creating an inner class must depend on an external class), As a result, activity cannot be released.
Asynctask thread pool, more serious problem

Workaround:
1. Arrange the thread execution time reasonably, and the control thread ends before the activity ends.
2. Change the inner class to a static inner class and use weak reference weakreference to save the activity instance because the weak reference recycles it as soon as the GC finds it, so it can be recycled as quickly as possible.

3.BitMap consumes too much memory
Bitmap Resolution requires memory, but memory only provides 8M of space to bitmap, if the picture is too much, and does not recycle bitmap in time, it will cause memory overflow.

Workaround:
Load pictures after recycle compressed pictures in time

4. Memory leaks due to resource not being shut down in time
For example, some cursor not in time close will save the activity reference, causing a memory leak

Workaround:
Close in time in the Ondestory method

memory leaks due to the use of 5.Handler
Because of the use of handler, Handler sends a message object into MessageQueue and then looper polls the MessageQueue and then takes out the message execution, but if a message is not removed for a long time to execute, So because there is a Handler reference in the message, and Handler is generally an inner class object, the message references Handler, and Handler references the activity so that the activity cannot be recycled.

Workaround:
Static inner class + weak reference is still used to resolve

There are also some about the collection object is not removed, the registered object is not anti-registration, the code pressure problem may also produce a memory leak, but the use of several of the above solutions can generally be resolved.

Android 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.