Android: Memory Optimization Summary

Source: Internet
Author: User

Android memory optimization is important for large Android projects! Let's pick a few common examples today.


1. Context References

In addition to activity and fragment, when using context, try to use application context, in Androidannotations, that is:

@RootContextContext context;

Reason: The context of the application has a long life cycle, it coexists with our app, we try not to hold the context of the activity, because its life cycle is short, the person may appear, after the activity exits, Its context is still held by the Ebean object.


2.Bitmap

Bitmap is a big memory-intensive Android, we need to be aware of:

2.1 Do not need bitmap, call recycle () in time to destroy.

2.2 When using the bitmap, you can set a certain sampling rate.


3.Cursor

When we read the third-party application or the system's contentprovider, we usually get the cursor object by calling the Getcontentresolver () and adding or removing the change method, and then proceed with the operation. When you're done, be sure to turn it off!


4.ListView and GridView

The ListView and the GridView are to be reused for the view, the programmer who does not understand the reuse, is not the qualified Android programmer.


5. Custom Controls

We ourselves often customize our own controls through view or Surfaceview, usually using a thread, periodically drawing, view usually using the Postinvalidate () and OnDraw () methods, etc. Note: When we override its Ondetachwindow method, remember to stop the drawing of threads and the release of resources in time, especially some picture resources.


6.BroadcastReceiver Be sure to check that the register and unregister appear in pairs.


7.Handler when used, to declare as static


8. We have Java containers in singleton mode, such as List,queue,stack,map, to call the clear method in a timely manner, because if these containers retain references to objects that are no longer applicable, the JVM will assume that the object is still referenced, Will not be released, if used improperly, there will be a memory leak.


..............................

Analysis Tools for Memory:

1.Eclipse, open the Ddms view, check our app's package name, we can dump out the memory consumption of our application

2.Eclipse third Plug-in mat (Memory analysis tool)

3.ADB command

We can use the following steps:

a). ADB shell

ADB enters mobile phone

b). PS

To view all the process and package names, note that we need to review the package name and PID (process ID) that our app uses

c). Dumpsys Meminfo < application pid>

Use this command to view the memory usage.

As the memory footprint of the Chrome browser:


We can constantly view the memory footprint in real time by manipulating our application to locate the problem.


Not permitted for commercial purposes without permission

Welcome to join QQ Group discussion: Android Development Alliance: 272209595



Android: Memory Optimization Summary

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.