Reprinted from: http://blog.csdn.net/anlegor/article/details/23398785
Android Memory management mechanism:
Android memory management mainly includes: Lowmemory killer mechanism, ashmem,pmem/ion and native memory and Dalvik memory management and JVM garbage collection mechanism.
Lowmemory killer mechanism:
SOURCE Location Drivers/staging/android/lowmemorykiller.c
Android is a multitasking system, which means you can run multiple programs at the same time, which you should be familiar with. In general, there is a certain amount of time overhead to start running a program, so in order to run faster, Android does not kill it immediately when you exit a program so that it can be started quickly the next time you run the program. As more and more programs are kept in the system, memory will certainly be insufficient, and the killer is to clear the relevant programs when the system memory is below a certain value, and ensure that the system maintains a certain amount of free memory.
The low Memorykiller determines the release process based on two principles, the importance of the process, and the amount of free memory that the process can obtain.
The importance of the process, decided by Task_struct->signal_struct->oom_adj,
Android divides the importance of the program into the following categories, descending in order of importance, each program has a Oom_adj value, the smaller the value, the more important the program, the less likely you will be killed:
In addition to the above procedural importance classifications, the Android system maintains another table Minfree for maintaining memory alert values, which form a correspondence between the two tables that are saved under "/sys/module/lowmemorykiller/parameters/".
Adj file contents such as "0,1,2,4,9,15", minfree file content shape such as "8192,10240,12288,14336,1638,20480", the resulting table structure is as follows:
For example, when the system's available memory is less than the 12384*4k size, it starts to kill the oom_adj>=9 level of the process.
Process of memory, obtained through GET_MM_RSS, under the same Oom_adj, the memory is large, priority is killed.
Ashmem (anonymous memory sharing):
SOURCE Location: KERNEL/MM/ASHMEM.C
Provides large chunks of shared memory for inter-process, while providing a mechanism for the kernel to reclaim and manage this memory.
Compared to the traditional memory allocation mechanism such as malloc and Anonymous/namedmmap, the advantage is that the kernel driver provides the auxiliary kernel's memory recovery algorithm mechanism (pin/unpin). What is a pin and unpin? Specifically, when you use Ashmem to allocate a piece of memory, but some parts of it are not used, you can unpin the memory out.
After Unpin, the kernel can reclaim its corresponding physical page for his use. You don't have to worry about the process failing to access the Unpin memory again because the recovered memory can be retrieved again (via a handler) because the Unpin operation does not change the address space already mmap.
Androidpmem/ion Memory Manager:
SOURCE Location: DRIVERS/MISC/PMEM.C
Androidpmem is a mechanism developed to achieve shared large-size continuous physical memory, which is useful for components such as DSP,GPU. Pmem is the equivalent of dividing the system's memory into separate management, that is, not managed by Linux mm, in fact the Linux mm does not see this memory at all.
Pmem and Ashmem all share memory through mmap, the difference is that the shared area of Pmem is a contiguous amount of physical memory, while the Ashmem shared area is contiguous in virtual space and physical memory is not necessarily contiguous. DSP and some devices can only work on continuous physical memory, so the communication between the CPU and DSP needs to be realized through PMEM.
Ion is a common memory manager that Google introduced in Android4.0ics to address memory fragmentation management, and it will be more integrated kernel. Currently qcom MSM, Nvdia Tegra, TI OMAP, MRVL Pxa are replacing pmem with Ion.
ION defines four different heap types to implement different memory allocation strategies.
Androidnative Memory and Dalvik memory management:
The native process is implemented in C/s + + and does not contain Dalvik instances, and the program files under the/system/bin/directory are run in the form of native processes, such as/system/bin/surfaceflinger,/system /bin/rild, Procrank, etc. is the native process, the address space structure is as follows:
The Java process is a process that runs on top of the Dalvik virtual machine in Android. The host process of the Dalvik virtual machine is created by the fork () system call, so each Java process is present in a native process, so the Java process's memory allocation is more complex than the native process because a virtual machine instance exists in the process. Applications in Android are basically Java processes, such as desktops, phones, contacts, status bars, etc., and the process structure is as follows:
Stack space (stack and stack) is controlled by the operating system, which mainly stores function addresses, function parameters, local variables, and so on, so the stack space does not need to be large, typically a few megabytes in size.
The use of the heap space is controlled by the programmer, who can manipulate the address space using a function call such as malloc, new, free, and delete. Heap provides memory space for programs to complete a variety of complex tasks, so the space is larger, typically hundreds of MB to several gigabytes. It is because the heap space is managed by programmers that being prone to improper use leads to serious problems.
The heap space in process space is what we need to focus on. The heap space is fully controlled by the programmer, and the space we use for malloc, C + + new, and Java new is the heap space, the C + + request memory space in the native heap, and the Java Request memory space in the Dalvik heap.
The memory space of a process is only virtual memory (or logical memory), and the program's operation requires real memory, that is, physical memory (RAM). When necessary, the operating system maps the memory (virtual memory) requested in the program's run to ram, allowing the process to use physical memory.
RAM, as an indispensable resource for process operation, has a decisive impact on system performance and stability. In addition, a part of RAM is used by the operating system for his use, such as video memory and so on, and so on is controlled by the operating system, and we do not have to pay too much attention to it, the process is the space to operate the virtual address space, can not directly manipulate the RAM. As follows:
The Android system imposes a hard limit on the VM heapsize of Dalvik, which throws an Oom exception when the Java process requests a Java space exceeding the threshold, and there are three items in the/system/build.prop that can be configured to configure the relevant information.
Dalvik.vm.heapstartsize: The initial size of the heap allocation, adjusting this value will affect the smoothness of the application and the overall RAM consumption. The smaller the value, the slower the system's RAM consumption, but due to the small initial value, some larger applications need to expand the heap, triggering a policy of GC and heap tuning, and the response will be slower to apply. Instead, the larger the value, the faster the system RAM consumes, but the smoother the program.
Dalvik.vm.heapgrowthlimit: The DVM heap is an increase in the limit heap under controlled conditions (only for the Dalvik heap, not including the native heap), but the DVM is under normal conditions The size of the heap is no more than the value of dalvik.vm.heapgrowthlimit (not normally described below). This value controls the limit heap size of those controlled applications, and if the controlled application DVM heap size exceeds this value, an oom (out of memory) is raised.
Dalvik.vm.heapsize: The limit heap size is not controlled, this is the maximum value of the heap. Whether it is controlled or not. This value affects the dalvikheap size of the non-controlled application. Once the Dalvik heap size exceeds this value, Oom is raised directly.
JVM garbage collection mechanism:
The JVM's garbage principle is that it divides objects into younger generations (young), older generations (tenured), persistent generations (Perm), and uses different garbage collection algorithms for objects of different lifecycles.
Young generation: The young generation is divided into three districts, one Eden area and two survivor districts. Most of the new objects generated in the program are in the Eden area, and when the Eden area is full, the surviving objects are copied to one of the survivor areas, and when the objects in this survivor area are full, the surviving objects in this area are copied to another survivor area, When the survivor area is full, the objects that were copied from the first Survivor district and survived at this time will be copied to the old generation.
Older generation (tenured): Older generations are storing objects copied from the young generation, which are still alive in young generations, and are full of copies. In general, the life cycle of the objects in older generations is relatively long.
Persistent generation (Perm): used to store static classes and methods, and persistent generations have no significant impact on garbage collection.
Android Memory Analysis tool:
Memory Monitoring Tool DDMS:
DDMS can install the corresponding plug-ins in eclipse, as well as the Tools folder in Androidsdk, and is more complete than the Eclipse plugin.
After switching to Eclipse's DDMS view, select the process to monitor from the device (the phone needs to turn on USB debugging, the manifest in the monitored app should be true android:debuggable)
As an operation, we focus primarily on whether the DataObject type of total size data is abnormal during the use of data fluctuations.
Eclipse Mat Plugin:
The mat is a plug-in for Eclipse, and after the installation is complete, DDMS attempts to select the "Update Heap" and "Dump HPROF file" buttons on the left-hand side of the device to select "Leak Suspects report" in the popup window. And finish, the following results can appear to try:
After entering Leaksuspects, you will be able to list all possible leaks and code snippets.
Dumpsys meminfo command for Adb Shell:
Adbshell's cat command:
Apply memory allocation Trace Tool allocation tracker:
The same function is used for DDMS, only a sub-function provided inside, to know the allocation of all objects in the code of which class, which file of which line.
Common causes of Androidoom:
Static instances of non-static inner classes tend to cause memory leaks
Activity uses static members
Memory issues when using handler
No anti-registration after registering an object
Memory leaks due to objects not being cleaned in the collection
Memory leaks due to resource object not shutting down
Some bad code into memory pressure: bitmap improper use; When constructing adapter, no cached Convertview is used; objects are created in frequently invoked methods (such as loops)
Resources:
Androidmemory Fundamentals
Http://www.slideshare.net/tarasleskiv/android-memory-fundamentals?qid=c8462372-7470-4459-a76c-552c678724c0 &v=qf1&b=&from_search=2
Forensic Memory Analysis of the Android ' Sdalvik Virtual machine
Http://www.slideshare.net/SOURCEConference/forensic-memory-analysis-of-androids-dalvik-virtual-machine?qid= C8462372-7470-4459-a76c-552c678724c0&v=qf1&b=&from_search=6
Memory Management For_android_apps
Http://www.slideshare.net/shaobin0604/memory-management-forandroidapps?qid= c8462372-7470-4459-a76c-552c678724c0&v=qf1&b=&from_search=11
Android Memory management mechanism analysis
Http://www.doc88.com/p-992213371842.html
Memory recovery mechanism for Android operating system
http://blog.jobbole.com/25169/
Android System anonymous shared memory (anonymousshared Memories) C + + Call interface analysis
http://blog.csdn.net/luoshengyang/article/details/6939890
Android system Anonymous shared memory Ashmem (anonymousshared memory) brief introduction and Learning Plan-Lao Luo's Android Tour-blog channel-csdn.net
http://blog.csdn.net/luoshengyang/article/details/6651971
Program source Code Analysis-Lao Luo's Android Tour-blog channel-csdn.net
http://blog.csdn.net/luoshengyang/article/details/6664554
Android system Anonymous shared memory Ashmem (anonymousshared memory) analysis of the principle of sharing between processes-Lao Luo's Android Tour-blog channel-csdn.net
http://blog.csdn.net/luoshengyang/article/details/6666491
Windows memory and Process manager underlying analysis
Http://www.cnblogs.com/zudn/archive/2010/12/29/1920593.html
What is memory direct imaging technology? What is the characteristic of it?
Http://zhidao.baidu.com/link?url=e8NFxuOaY9UsOxKGklWmTR20-gpDGy48eE1SerGNCjAYbg3Xj-Fp0pqBSAKFy_ Aejykqowmx3qhgn4ldwgec0m1wxbowdot59bua2o0djzc
Mysteries of Windows Memory managementrevealed
Http://media.ch9.ms/teched/na/2011/ppt/WCL406.pptx
Comprehensive introduction to Windows memory management mechanisms and C + + memory allocation instances
http://blog.csdn.net/yeming81/article/details/2046193
Tech Insider: Android's enhancements to the Linux kernel
Http://tech.it168.com/a2011/0805/1228/000001228471_all.shtml
Android Memory Management Learn _ Baidu Library
Http://wenku.baidu.com/link?url=bGaPQbl7u1_JmKSJ5nVBv1dHtvGSXREyv_7PeSt9_FCx20I5oK1EaEgno9d6Ked4Kao92yoP_ 7x6goqtoylezmwen-inknbxb7saoj3jh6m
Android Low Memory killer detailed-tuyer-chinaunix Blog
Http://blog.chinaunix.net/uid-20321537-id-3228776.html
Introduction to Linux Device files
Http://www.jinbuguo.com/kernel/device_files.html
Explore Android Internals
Http://www.slideshare.net/jserv/android-internals-30176596?qid=a3be858d-e483-423c-be0e-a85e0a08fdd6&v=qf1 &b=&from_search=4
Inside Android ' s Dalvik vm-nejug Nov 2011
Http://www.slideshare.net/dougqh/inside-androids-dalvik-vm-nejug-nov-2011?qid= A3be858d-e483-423c-be0e-a85e0a08fdd6&v=qf1&b=&from_search=3
Android Ion Memory Manager (1)--Introduction
http://blog.csdn.net/crazyjiang/article/details/7927260
Memory management analysis for Android processes
http://blog.csdn.net/gemmem/article/details/8920039
Gc in Android
Http://www.slideshare.net/VikasBalikai/gc-in-android
Android Memory leak analysis and debugging
http://blog.csdn.net/gemmem/article/details/13017999
Android Oom Introduction and Analysis method
http://blog.csdn.net/gemmem/article/details/8964397
[Android Memory] Android memory management, monitoring profiling