[Android Memory] malloc function and Oom Killer under Linux

Source: Internet
Author: User

Http://www.linuxidc.com/Linux/2010-09/28364.htmLinux the malloc function is primarily used to request memory from the heap in user space, the request successfully returns a pointer to the allocated memory, and the request fails to return NULL. By default, the Linux kernel uses an "optimistic" allocation memory strategy, first roughly estimating the amount of memory the system can use, and then allocating memory, but only when it is used to actually give you the allocated memory. In this way, using malloc to request memory does not return NULL, and you may not necessarily be able to fully use this memory, especially if you request a lot of memory in one or more successive times.   If you are continuously requesting memory with malloc instead of actually using it, the total amount of memory requested can exceed the amount of memory that can actually be used. But when you really use this memory, such as using the memset or bzero function once to "use" the chunk of memory that you requested, the Linux system will be out of memories, this time the Oom killer will kill other processes in the user space to free up more memory.  oom Killer According to the Oom score to decide which process to kill, OOM score can be seen/proc/<pid>/oom_score,score by the badness function calculated, depending on the duration of the process run, The process priority, the amount of memory used by the process, and so on. /proc/<pid>/oom_adj can be used to intervene to calculate socre, the value of the value range is -17~15, if 17 the process will never be killed (this may also be related to the kernel version, not all kernel versions are supported, you have to actually try).   "Default" Linux is done this, "default" refers to the time/proc/sys/vm/overcommit_memory is 0. This parameter can also be adjusted, if the 1 means "no rejection", as long as you come to malloc to apply, I do not do anything, immediately allocate memory to you, so that the performance will be greatly improved; If 2 means that Linux will accurately calculate all available memory and the requested memory, Returns null if the requested amount of memory is exceeded. Memory value calculation method that can be used, virtual memory (Swap) +/proc/sys/vm/overcommit_memory (percent) x physical memory. The default value of/proc/sys/vm/overcommit_memory is 50, which is calculated as the number of physical memory 50%.  linux own kernel will take up part of the memory, as well as the memory occupied by Buffer/cache, so the memory that can actually be used after the malloc application is not the physical memory size, the output of DEMSG contains the relevant information (if not seen, may be the other information washed out, restart the system, after the system up to see immediately): memory:2071220k/2097152k Available (2122k kernel code, 24584k reserved, 884k data, 228k init, 1179584k highmem)   about Oom killer proc File system   http://book.2cto.com/201302/16321.html  

The proc file system associated with Oom killer is now described below.
/proc/<pid>/oom_adj

Set a value for/proc/<pid>/oom_adj to adjust the score. The range of adjustment values is –16~15. The positive value is easily selected by the Oom killer. Negative values are less likely. For example, when you specify 3 o'clock, the score becomes 23 times times, and when you specify – 5 o'clock, the score becomes 1/25.

"–17" is a special value. If set to –17, the signal from Oom Killer is disabled (starting with the Linux 2.6.12 support setting –17).

In the case of Oom killer running, you can execute the following commands in order to implement remote logins and want to exclude sshd from the object.
# cat/proc/' Cat/var/run/sshd.pid '/oom_score
15
# echo-17 >/proc/' cat/var/run/sshd.pid '/oom_adj
# tail/proc/' Cat/var/run/sshd.pid '/oom_*
==>/proc/2278/oom_adj <==
-17
==>/proc/2278/oom_score <==
0/* Score becomes 0*/

You can use/proc/<pid>/oom_adj starting with Linux 2.6.18. The contents are recorded in the Documentation/filesystems/proc.txt.
/proc/sys/vm/panic_on_oom

When/proc/sys/vm/panic_on_oom is set to 1 o'clock, the Oom killer runs without sending a process signal, causing a major failure of the kernel.
# echo 1 >/proc/sys/vm/panic_on_oom
/proc/sys/vm/oom_kill_allocating_task

Starting with Linux 2.6.24 the proc file system has oom_kill_allocating_task. If you set a value other than 0 for this setting, the process that makes the Oom killer run itself will receive a signal. The scoring calculation process for all processes is omitted here.
# echo 1 >/proc/sys/vm/oom_kill_allocating_task

This does not require reference to all processes, but does not take into account the priority and root privileges of the process, and sends only the signal.
/proc/sys/vm/oom_dump_tasks

Starting with Linux 2.6.25, setting Oom_dump_tasks to a value other than 0 increases the list information for the process in the output of the Oom killer runtime.

The following is an example setup.
# echo 1 >/proc/sys/vm/oom_dump_tasks

The list information is shown below and can be confirmed using DMESG or syslog.
[PID] UID tgid total_vm RSS CPU Oom_adj Name
[1] 0 1 2580 1 0 0 Init
[0] 3231 0 1-17 UDEVD
[2736] 0 2736 1470 1 0 0 syslogd
[2741] 0 2741 944 0 0 0 klogd
[2765] Bayi 2765 5307 0 0 0 Dbus-daemon
[2861] 0 2861 944 0 0 0 acpid
...
[3320] 0 3320 525842 241215 1 0 Stress
/proc/<pid>/oom_score_adj

/proc/<pid>/oom_score_adj has been installed from Linux 2.6.36 and will be replaced with/proc/<pid>/oom_adj thereafter. For more information, please refer to documentation/feature-removal-schedules.txt. Even if the/proc/<pid>/oom_adj is currently set, the values that are transformed inside the kernel are set for/proc/<pid>/oom_score_adj.

/proc/<pid>/oom_score_adj can set a value between –1000~1000. When set to –1000, the process is excluded from the object that the Oom killer forces to terminate.

After the kernel 2.6.36 is written to Oom_adj, only the following information is output.
# DMESG
.....
UDEVD:/proc/60/oom_adj is deprecated, please use/proc/60/oom_score_adj instead.

[Android Memory] malloc function and Oom Killer under Linux

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.