[Android performance optimization series] memory basics-how to manage memory for Android, android-android

Source: Internet
Author: User

[Android performance optimization series] memory basics-how to manage memory for Android, android-android

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.

The following is the body of this article:

################

Random Access to memory (Ram) is an extremely valuable resource in whatever software development environment, and extremely limited physical memory is more valuable on mobile development platforms. Therefore, although the Dalvik virtual opportunity of Android executes garbage collection, this does not mean that you can ignore the memory allocation and release of your application.

To enable the garbage collection period to recycle the memory in your application, you need to avoid Memory leakage (usually caused by holding an object in a global variable ), and release point reference objects (such as in the lifecycle) when appropriate ). For most applications, the garbage collector of the Dalvik virtual machine will help you reclaim the memory of objects that are no longer in scope.

This article describes how Android manages memory processes and memory allocation, and how to reduce memory usage during development. If you want to know more about the Java resource cleanup mechanism, you can read some books or online documents about resource reference. If you are looking for information on how to analyze the memory Usage of the current application, you can refer to this article (Investingating Your RAM Usage ).


How does Android manage memory?

Although Android does not provide swap space for memory, it uses paging and memory ing files to manage memory. This means that any memory you modified, whether it is a new object or an operation that changes the memory ing night, will be stored in the memory, so that it cannot be released. The only way to release these memories from your application is to release the used objects so that the garbage collection mechanism can recycle them. The only exception is where the system calls data that has not been changed, such as code, when the system wants to use it elsewhere.


Public memory

In order to store as many resources as possible in RAM, Android allows some common memory to be used across processes. The principle is as follows:

1.Every application process is taken responsibility by a process called Zygote.The Zygote process is automatically started when the system starts andMany public framework codes and resources (such as Activity topics) are loaded internally ).When starting a new application, the system copies these resources from the Zygote process and runs the application code in the new process. This allows a large number of memory resources in the framework and resources to be reused across processes.

2.Most static data is stored in the process through memory ing. This not only indicates that the same data can be shared across processes, but also indicates that the data can be called at any time when necessary.For example, static data includes the Dalvik code (precompiled. odex for direct calling), application resources (called by guotong resume resource ing table), and some default project resources, such as in. local Code in the so file

3.In many places, Android directly allocates memory areas to share the same dynamic RAM across processes.For example, if the window Surface uses a public area for application and screen sorting, the Cursor cache uses the same memory between the ContentProvider and the client.

Due to the use of public memory, we need to care about how much memory your application needs. For more information about how to analyze the memory Usage of an application, see Investingating Your RAM Usage.


Allocate and release application memory

The following section describes how Android allocates and releases memory:

1. The Dalvik heap of each process is limited to a virtual memory range. This indicates that the size of the logical heap can increase as needed (of course, the system will limit the maximum memory usage of each application)

2. The logical size of each heap is not the same as the physical memory used by the heap.When the app heap is checked, Android calculates a value called the ratio (PPS), which includes the dirty data and cleaning data shared with other apps, the total value of your PPS will be considered as the value of physical memory by the system. For more information, see Investingating Your RAM Usage.

3. The Dalvik heap does not compress the logical size of the heap,This means that Android does not reduce the heap size by clearing the space. Android only recycles the heap itself when there is no available space in the heap. However, this does not mean that the physical memory used by the heap can be released. After garbage collection, Dalvik scans the heap to find unused memory pages and releases these memory pages to the kernel space through madvise. Therefore, allocating or releasing objects in large batches will reclaim the used physical memory. However, it is not very good to reclaim memory space from those small resources because the pages that store these small resources are likely to be used by other objects and cannot be released.


Restrict application memory

Android,He sets heap hardware limits for each application.. This limit depends on how much RAM the device has. If your application reaches the upper limit of the heap and you want to allocate more memory space, OOM will occur.

Under certain conditions, you may want to know the maximum heap space allowed by the system on the current device, for example, to determine the cache size. Then you can query getMemoryClass () to obtain the system data. It will return the available memory of your application as an int value, which will be discussed below.


Switch Application

When switching between applications, Android uses the least recently used algorithm (LRU) to save the process in a front-end application component list, rather than in the swap space. For example, when a user starts an application for the first time, the process of the application is created, but the application is not completely exited when the user leaves the application. The system will save the process cache, so that when the user returns to the current application, the cache can be called as quickly as possible to speed up the entry into the program.

If your application has a cache process that maintains the memory required by the application, the overall performance of the system will be limited even if the user does not use it.Therefore, when the system memory resources are insufficient, it may kill the processes that have not been used recently.. So in order to keep your processes as much as possible, it is best to follow the chapters below to release your resources.

If you want to know

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.