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