[Android performance optimization series] Memory Improvement-how should applications manage memory and android Performance Optimization
If you like my blog, please pay attention to my Weibo, please click here (http://weibo.com/kifile), thank you
Reprinted please indicate the source (http://blog.csdn.net/kifile), thank you again
Address: http://developer.android.com/training/articles/memory.html
In the next period of time, I will translate some official Android documents about performance improvement every day for you.
We recommend that you go to my blog before reading this article.
[Android performance optimization series] memory basics-how to manage memory in Android
[Android performance optimization series] ultimate article on memory-reducing your memory consumption
The following is the body of this article:
################
How should applications manage memory?
In each stage of software development, you should pay attention to your RAM consumption (even in the software design phase ). There are many ways to use them to help you design and write more efficient code,
You should use the following technologies to design and implement applications to reduce the memory consumption of applications.
Use as few services as possible
If your application requires services for background operations, try to make it work only when it does need work. Make sure that your service is terminated after your work is completed.
When you start a service, the system will keep its process while the service is running, which will make the process very large, because the resources consumed by the service cannot be used or recycled by other applications. This will reduce the number of processes stored in the LRU cache and reduce the efficiency when switching applications. When there are enough services running in the background, it may even cause the entire system to crash due to memory shortage.
The easiest way to manage your service is to use an IntentService, which closes the service after the event is handled. For more information, see use the front-end service.
If you do not need it, keep the service running in the background.One of the least mistakes in memory management. Therefore, do not greedy yourself to keep your services running in the background. This will not only lead to lower performance of your application, but also lead to the possibility of discovering such behavior and detaching your application.
When the user interface is hidden, the memory resources are released.
When a user switches to another application and your ui is invisible, you should release the resources used by your own interface. Releasing the ui resources can significantly increase the number of cache processes stored by the system, which greatly improves the user experience.
When the interfaces of all processes in your application are invisible to users, the onTrimMemory () callback interface is triggered with a TRIM_MEMORY_UI_HIDDEN parameter. This interface differs from the OnStop () callback interface in that the OnStop () interface will be called when the application jumps, the onTrimMemory interface is called only when all interfaces are invisible. Although you need to implement the onStop () method to release Activity resources, such as network access or logout of broadcast receivers, however, this is not enough. You should not release your ui resources before onTrimMemory. This ensures that the user returns to your application through the return key. Your ui resources also exist and can be quickly displayed.
Some memory is released when the memory is insufficient.
In the lifecycle of your application, the onTrimMemory () interface is also called when the memory of the entire device gets low. You should selectively release your resources based on the memory level passed from onTrimMemory ().
TRIM_MEMORY_RUNNING_MODERATE
Your application is running and cannot be killed. However, there is very little memory left on the device. The system needs to kill some processes from the LRU cache.
TRIM_MEMORY_RUNNING_LOW
Your application is running and cannot be killed. However, the remaining memory on the device is insufficient to reach a critical value. Therefore, you need to release unused memory to improve the system running efficiency.
TRIM_MEMORY_RUNNING_CRITICAL
Your application is running, but the system is ready to kill processes in most LRU, so you should release those resources that are not critical. If the system cannot obtain enough RAM through recycling, it clears all LRU caches and begins to kill the processes that want to be retained, such as processes that are running background services.
Similarly, when your application is in the cache, you may suffer from the following onTrimMemory () levels
TRIM_MEMORY_BACKGROUND
The system is running in low memory, and your process is in the beginning of the LRU list. Therefore, although your application process is unlikely to be killed, the system has started killing processes in LRU in a cup. You should release resources that are easy to restore to ensure that you are still in the list and can switch quickly when the user returns to the application.
TRIM_MEMORY_MODERATE
The system is running in the low memory stage, and your process is in the center of the LRU list. You cannot obtain enough memory resources when you join the system. Your application will be killed.
TRIM_MEMORY_COMPLETE
The system is running in the low memory stage. Your process will be one of the first processes killed by the system. You should release all resources unrelated to your application status.
Although the onTrimMemory () interface is not added until API14, you can still use the onLowMemory interface as the callback for the old version. It can be the same as TRIM_MEMORY_COMPLETE in onTrimMemory.
Note: When the system begins to kill processes in the LRU cache, although the process starts from bottom to top, it will also consider killing those with high memory consumption first, to reclaim more system resources. Therefore, if your application's memory is as low as possible, you may be kept in the memory and can switch back quickly.
Determine how much memory you should have
As mentioned earlier, each Android device will provide different heap size restrictions to the application based on its own RAM size. You can use getMemoryClass () to obtain the size of the heap that an application may own. Add your application to allocate more memory when the memory is insufficient, which will cause OOM
In a special case, you can set the attribute of largeHeap in the <application> label in the manifest file to true. If you do this, you can use getLargeMemoryClass () to obtain the ratings of a large heap.
However, it is recommended that only applications that really need more memory space request a larger heap space, such as a large image editing application.Remember not to apply for a large heap because of your memory overflow., You need to know that you only understand where your memory is allocated and why it has been retained. When you are sure that your application requires a large heap, you should avoid wasting it at will. Using Additional memory will greatly affect the user experience, because garbage collection consumes more events, and the efficiency of the system slows down application switching or other operations.
In addition, the size of a large heap varies with devices. When running on devices with limited memory, the size of a large heap may be the same as that of a normal heap. Therefore, even if you use a large heap, you should use getMemoryClass () to check the size of the real heap to ensure that the size exceeds the limit.
How does android manage memory?
Let's take a look at the following tutorial.
Google has developed Android in partnership with the Open Mobile Alliance, which is a leader in more than 30 technologies and wireless applications including China Mobile, Motorola, Qualcomm, HTC, and T-Mobile. Through in-depth partnerships with carriers, equipment manufacturers, developers, and other parties, Google hopes to build a standardized and open mobile phone software platform, an open ecosystem is formed in the mobile industry.
Take a look at the following android tutorial, very comprehensive and detailed: tutorial 1: Android development video training tutorial android smart System Development tutorial size: 3.5 GB
Tutorial 2: getting started with Google Android development and video tutorials + Source Code ebook size: 1 GB
Tutorial 3: Android development video tutorial size from scratch: 1.19 GB
Tutorial 4: Android 2.0 game development practice instance tutorial + source code size: 1.8 GB
Tutorial 5: Android-based Address Book Development Video size: 437 MB
Tutorial 6: 3G Mobile Phone development-Android Application Development size: 4.23 GB
Tutorial 7: Android project video tutorial + Source Code Tutorial: 1.85 GB
Tutorial 8: android video tutorial size: 117 MB
Tutorial 9: Android Application Development details (e-book) Format: PDF
Tutorial 10: how to increase the size of Android: 2.7 GB
Tutorial 11: 3G application development size for Android practice courses: 1.7 GB
Tutorial address: www.dwz.cn/98Foh
Android phones occupy more than 80% of the memory on the host. No matter how the optimization is always around 90%, how to deal with 2 GB of running memory?
I will teach you