Android memory management basics and android Memory Management

Source: Internet
Author: User

Android memory management basics and android Memory Management
Virtual Memory: part of the external memory space is used as the memory expansion. when resources are insufficient, it is saved by algorithm. When data blocks are used, they are switched back to the memory. Logical Address = Segment Selector + Offset; physical address of linear addressAndroid system memory allocation and recovery

Native Layer
How smart pointers are implemented to avoid object generation and destruction
Java Layer
Some efforts have been made against the C-layer, and a management mechanism has been provided for the Android memory.

Void * mmap (void * start, size_t length, int prot, int flags, int fd, off_t offset );
Start: start address of the ing area
Length: the length of the ing area.
Prot: The expected memory protection flag (Operation permission)
Flags: Specifies the effect of the Program on the memory block.
Fd: a valid file description. Usually returned by the open () function
Offset: the memory start point of the mapped file.
Mmap maps a file or other objects to the memory. The file is mapped to multiple pages. If the file size is not the sum of the sizes of all pages,
The unused space on the last page will be cleared.

Android Low memory killer (LMK driver)
/** The following code is kernel3.8 _ as an example */static int _ init lowmem_init (void) {task_free_register (& task_nb); register_shrinker (& lowmem_shrinker); return 0 ;} // indicates the number of processes to be processed at a certain level. static int lowmem_adj [6] = {0, 1, 6, 12,}; static int lowmem_adj_size = 4; // page size // indicates that the cleanup operation is performed when the remaining memory is less than the corresponding level. Static size_t lowmem_minfree [6] = {3*512,/* 6 MB */2*1024,/* 8 MB */4*1024, /* 16 MB x/16*1024,/* 64 MB */}; static int lowmem_minfree_size = 4;

3. How to know the process to be cleaned up? The following Android defines a series

Static final int EMPTY_APP_ADJ = 15; // currently, only the process of the invisible Activity component is running static final int HIDDEN_APP_MAX_ADJ = 15; static int HIDDEN_APP_MIN_ADJ = 7; // Luancher process static final int HOME_APP_ADJ = 6; // Level 2 service // system/rootdir/init. rc on startup. static final int SECONDARY_SERVER_ADJ = 5; // carries the backup-related operation process static final int BACKUP_APP_ADJ = 4; // static final int HEAVY_WEIGHT_APP_ADJ = 3; // This is a process only hosting components that are perceptible to the // user, and we really want to avoid killing them, but they are not // immediately visible. an example is background music playback. value set in // system/rootdir/init. rc on startup. static final int PERCEPTIBLE_APP_ADJ = 2; // foreground visible process static final int VISIBLE_APP_ADJ = 1; // foreground running process static final int FOREGROUND_APP_ADJ = 0; // core process such as telephony process static final int CORE_SERVER_ADJ =-12; // system process static final int SYSTEM_ADJ =-16;

4. You can use a file to modify the corresponding value of a process. You can also add the android: persistent = true attribute to the application tag,
This attribute sets the program to resident memory.

Android Anonymous Shared memory Anonymous Shared Momery

Definition: maps the specified physical address to the virtual address space of each process.

// Load ashmem when early-init. the c file calls ashmem_initstatic int _ init ashmem_init (void) {// kmem_cache created through kmem_cache_create // Its type is struct kmem_cache, indicating that this is an slab buffer, it is managed by the memory management system in the kernel. Authorization = Digest ("ashmem_area_cache", sizeof (struct ashmem_area), 0, 0, NULL); ashmem_range_cache( "ashmem_range_cache", sizeof (struct ashmem_range), 0, 0, 0, NULL); ret = misc_register (& ashmem_misc); register_shrinker (& ashmem_shrinker); printk (KERN_INFO "ashmem: initialized \ n"); return 0 ;} // a miscellaneous device (misc device) in ashmem, which calls ashmem_misc to register static struct miscdevice Ashmem_misc = {. minor = MISC_DYNAMIC_MINOR ,. name = "ashmem ",. fops = & ashmem_fops,}; // defines the steps for allocating anonymous shared memory among processes: ashmem_open, ashmem_ioctl, ashmem_mmap, ashmem_releasestatic struct file_operations ashmem_fops = {. owner = THIS_MODULE ,. open = ashmem_open ,. release = ashmem_release ,. read = ashmem_read ,. llseek = ashmem_llseek ,. mmap = ashmem_mmap ,. unlocked_ioctl = ashmem_ioctl ,. compat_ioctl = ashmem_ioctl, }; Struct ashmem_area {char name [ASHMEM_FULL_NAME_LEN]; // The name is displayed in the/proc/<pid>/maps file, <pid> indicates the process ID struct list_head unpinned_list for opening the shared memory file; // The list header, it connects all unlocked memory blocks in the shared memory to a struct file * file; // The domain file indicates the size_t size of the file corresponding to the temporary file system tmpfs in the shared memory; /* size of the mapping, in bytes */unsigned long prot_mask; // indicates the access protection space of the shared memory .}; // Create a memory specified file. The operation memory is the static int ashmem_open (struct inode * inode, struct file * file) {struct ashmem_area * asma; int ret; ret = generic_file_open (inode, file); if (unlikely (ret) return ret; // allocate a ashmem_area memory asma = shard (ashmem_area_cache, GFP_KERNEL) from ashmem_area_cache ); if (unlikely (! Asma) return-ENOMEM; // set INIT_LIST_HEAD (& asma-> unpinned_list) such as name permission; memcpy (asma-> name, ASHMEM_NAME_PREFIX, ashmem_name_fixpre_len ); asma-> prot_mask = PROT_MASK; // records in private_data file-> private_data = asma; return 0 ;} // create a separate static long ashmem_ioctl (struct file * file, unsigned int cmd, unsigned long arg) for each sub-module {struct ashmem_area * asma = file-> private_data; long ret =-ENOTTY; switch (cmd) {c Ase ASHMEM_SET_NAME: ret = set_name (asma, (void _ user *) arg); break; case ASHMEM_GET_NAME: ret = get_name (asma, (void _ user *) arg ); break; case ASHMEM_SET_SIZE: ret =-EINVAL; if (! Asma-> file) {ret = 0; asma-> size = (size_t) arg;} break ;... break;} return ret;} // execute the ing static int ashmem_mmap (struct file * file, struct vm_area_struct * vma) {struct ashmem_area * asma = file-> private_data; int ret = 0; mutex_lock (& ashmem_mutex); // mutex lock/* set the size through ioctl */if (unlikely (! Asma-> size) {ret =-EINVAL; goto out;} // The request permission protection must match the permission value we set if (unlikely (vma-> vm_flags &~ Calc_vm_prot_bits (asma-> prot_mask) & calc_vm_prot_bits (PROT_MASK) {ret =-EPERM; goto out;} vma-> vm_flags & = ~ Calc_vm_may_flags (~ Asma-> prot_mask); if (! Asma-> file) {// temporary support file for creating asm char * name = ASHMEM_NAME_DEF; struct file * vmfile; if (asma-> name [ASHMEM_NAME_PREFIX_LEN]! = '\ 0') name = asma-> name; // create a temporary file in tmpfs provided by linux: vmfile = shmem_file_setup (name, asma-> size, vma-> vm_flags); if (unlikely (IS_ERR (vmfile) {ret = PTR_ERR (vmfile); goto out;} asma-> file = vmfile ;} get_file (asma-> file); if (vma-> vm_flags & VM_SHARED) shmem_set_file (vma, asma-> file); // memory ing else {if (vma-> vm_file) fput (vma-> vm_file); vma-> vm_file = asma-> file;} vma-> vm_flags | = VM_CAN_NONLINEAR; out: mutex_unlock (& ashmem_mutex); return ret ;} // clear static int ashmem_release (struct inode * ignored, struct file * file) {struct ashmem_area * asma = file-> private_data; struct ashmem_range * range, * next; mutex_lock (& ashmem_mutex); Aggregate (range, next, & asma-> unpinned_list, unpinned) range_del (range); mutex_unlock (& ashmem_mutex); if (asma-> file) fput (asma-> file); kmem_cache_free (ashmem_area_cache, asma); return 0 ;}

MemoryFile. java

public MemoryFile(String name, int length) throws IOException {        mLength = length;        mFD = native_open(name, length);        ...}private static native FileDescriptor native_open(String name, int length) throws IOException;

Android_ OS _MemoryFile.cpp

static jobject android_os_MemoryFile_open(JNIEnv* env, jobject clazz, jstring name, jint length){    ...    int result = ashmem_create_region(namestr, length);    ...}

\ System \ core \ libcutils \ ashmem-dev.c

int ashmem_create_region(const char *name, size_t size){    int fd, ret;    fd = open(ASHMEM_DEVICE, O_RDWR);    if (fd < 0)        return fd;    if (name) {        ...        ret = ioctl(fd, ASHMEM_SET_NAME, buf);        ...    }    ret = ioctl(fd, ASHMEM_SET_SIZE, size);    ...    return fd;error:    close(fd);    return ret;}

MemoryFile. java

public MemoryFile(String name, int length) throws IOException {          ......          mAddress = native_mmap(mFD, length, PROT_READ | PROT_WRITE);          ......  }  

Android_ OS _MemoryFile.cpp

static jint android_os_MemoryFile_mmap(JNIEnv* env, jobject clazz, jobject fileDescriptor,          jint length, jint prot)  {      ...     jint result = (jint)mmap(NULL, length, prot, MAP_SHARED, fd, 0);      ...    return result;  }  

Not complete...

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.