Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Linux OOM killer mechanism     - Modify grub solve computer startup error: ERROR 17 (Linux)

- Depth understanding of the TCP protocol (Database)

- Python regular expressions: how to use regular expressions (Programming)

- Struts2 Result Types (Programming)

- Oracle 12C modify spfile path (Database)

- How to implement Linux host Telnet SSH password Free (Server)

- Linux Firewall Basics (Linux)

- Linux operating system Start Tutorial: Xmanager Remote Access Linux graphical interface (Linux)

- Java NIO in the world (Programming)

- Zabbix monitoring of the switch (Server)

- Oracle 11g maintenance partitions (Seven) - Modifying Real Attributes of Partitions (Database)

- 11G ASM disk group does not automatically handle MOUNT (Database)

- File SUID, SGID, Sticky property (Linux)

- EChart simple and practical control on chart (Programming)

- Use the DBMS_SCHEDULER package to manage scheduled tasks (Database)

- The correct method to modify the maximum number of concurrent connections Tomcat 7 (Server)

- Snort build a secure Linux server (Linux)

- MySQL Server Time Synchronization Problem (Database)

- The Zabbix2.4.5 source compiler installation under Ubuntu 14.04 (Server)

- Ubuntu users to install Gnome-Pie 0.6.5 (Linux)

  Linux OOM killer mechanism
  Add Date : 2016-09-08      
  Linux in the Out Of Memory (OOM) Killer is a function of system memory sufficient means to ensure the final, you can exhaust system memory or swap area, the process according to some algorithm judging system occupy most resources, sends a signal to the process, forced to terminate the process.

The monitoring mechanism will simply take up memory that is too large, especially the moment quickly consume a lot of memory processes, in order to prevent out of memory and the kernel will kill the process.

This function can not be released even in the case of memory, it can be repeated to ensure that the process of memory, to prevent stagnation of the system, you can also find out the process consumes excessive memory.

A typical situation is: one day a machine can not suddenly ssh remote login, but can ping each other, not a network failure or the machine is down is very likely sshd process is OOM killer killed.

After the restart the machine to view the system log / var / log / messages will find Out of Memory: Kill process 247 (sshd) error message similar.

There is another situation can cause the ping can not ssh, too many connections to the system is the network file descriptor resource exhaustion, here not to consider this situation.

The availability and use vip program, this situation is also very prone to split-brain phenomenon.

Prevent important system processes trigger (OOM) mechanisms are killed: you can set the parameter / proc / PID / oom_adj -17, temporarily close the OOM mechanism linux kernel. The kernel is calculated by a specific algorithm to each process a fraction to determine which process to kill, oom score for each process can be / proc / PID / oom_score found.

We consider it important process has sshd, or some monitoring daemon, you can choose according to their need to protect the process of the actual situation.

Protection of a process being killed by the kernel can do this:

echo -17> / proc / $ PID / oom_adj

You can write a simple script to prevent the deployment process is important in the crontab oom

pgrep -f "/ usr / sbin / sshd" | while read PID; do echo -17> / proc / $ PID / oom_adj; done

One of the "/ usr / sbin / sshd" can replace you think that the important process, but in the match when the match was wrong not to pay attention

- The method of selection process

OOM Killer when memory is exhausted, will look at all processes, and scores were calculated for each process. Sends a signal to the highest score of the process.

The method of calculation of scores

When OOM Killer scores are calculated to consider many aspects. First make sure the following items 1 to 9 for each process and then calculate scores.

1. First, the calculation of scores based on the process virtual memory size as a reference, the virtual memory size can use the ps command VSZ or / proc / / status of VmSize to confirm. For virtual memory are consuming process, its initial score higher, the unit is 1KB as a score, 1GB memory consumption process, scoring about 1024 * 1024.

2. If the process is being executed swapoff system call, the score is set to maximum (the maximum value of unsigned long). This is because disabling swap behavior and eliminate the lack of memory is the opposite, it will immediately process as an object of OOM Killer.

3. If the parent process, then half of all child processes memory size as the score.

4. Adjust the score based on CPU usage time and process process start time, because here believe more in the long run or work process more and more important, the need to maintain a lower score.

5. For by the priority set lower process nice commands, to double the score. nice-n command set to 1 to 19 scoring double.

6. privileged process generally more important, so its score to 1/4.

7. Set by capset (3) and other functional (capability) CAP_SYS_RAWIO Note 3 process, which is a score of 1/4, will report directly to the hardware process operation is judged to be an important process.

8. With regard to Cgroup, if the process is allowed only with prompting OOM Killer running processes allowed memory node completely different memory nodes, its score of 1/8.

9. The final score value adjustment through the proc filesystem oom_adj.

According to the above rule, for all the scoring process, sends a signal SIGKILL to the highest scoring process (up to Linux 2.6.10, set up under the circumstances CAP_SYS_RAWIO function sends SIGTERM, in the absence of settings, send SIGKILL).

The score of each process can use the / proc / / oom_score to confirm.

However, init (PID of 1) the process can not be the object of OOM Killer. When the object of the process contains child process, child processes Xianxiang send signals.

After sending a signal to the object of the process, the full reference for the thread system, even if the thread group (TGID) different if processes share the same memory space and the target process exists, but also send a signal to those processes.

As to why with -17 instead of other values ​​(default is 0), which is defined by the linux kernel, the kernel source code view shows:

In linux- 3.3.6 version of the kernel source code for example, the path for the linux-3.6.6 / include / linux / oom.h, reading the kernel source known adjustable oom_adj is 15 -16, 15 -16 Minimum Maximum , -17 to prohibit the use of OOM. oom_score is 2 ^ n calculated, where n is the process of oom_adj value, the higher the score the more to oom_score the priority will be to kill the kernel.

Of course, you can also prohibit OOM mechanism by modifying the kernel parameters

# Sysctl -w vm.panic_on_oom = 1
vm.panic_on_oom = 1 // 1 means closed, default 0 indicates turning OOM
# Sysctl -p

test program

Command line parameters input occupied memory size N, according to their experimental environment to set the size of physical memory, for example, my test environment for 4G memory, it is sufficient to 4G

Code named mem.c, methods of compiling gcc -o mem mem.c

#include < stdio.h>
#include < stdlib.h>
#include < string.h>
#define PAGE_SZ (1 << 12)
int main (int argc, char * argv []) {
    int i;
    if (argc = 2!) return 0;
    int gb = atoi (argv [1]);

    for (i = 0; i <((unsigned long) gb << 30) / PAGE_SZ; ++ i) {
        void * m = malloc (PAGE_SZ);
        if (! m)
        memset (m, 0, 1);
    printf ( "allocated% lu MB \ n", ((unsigned long) i * PAGE_SZ) >> 20);
    getchar ();
    return 0;

Then perform ./mem 4

If you do not do anything, then run directly will be found out of this process automatically oom

If we do the following, the process priority setting -17

pgrep -f "mem" | while read PID; do echo -17> / proc / $ PID / oom_adj; done

You will find that this system does not occupy large memory processes oom out, but this time you will find that the system response is slow or crash!

- Set any process triggered oom

One of the most simple test method triggered OOM, you can put oom_adj a process is set to 15 (maximum), the most likely trigger. Then execute the following command:

echo f> / proc / sysrq-trigger

If you want to experiment in the test environment, speed, operating environment for online direct cause any adverse effects that do not blame bloggers.
- Tab set to four spaces in Vim (Linux)
- Glibc support encryption by modifying the DNS (Programming)
- Find details block device with Linux blkid command (Linux)
- Install NetBeans IDE 8.0 on Ubuntu, Linux Mint, Elementary OS, and Debian (Linux)
- LMMS 1.03 install on Ubuntu 14.04 (Linux)
- RabbitMQ Getting Started Tutorial (Linux)
- Compression decompression command under Linux (Linux)
- Getting Started with Linux system to learn: how to check in a package is installed on Ubuntu (Linux)
- Java in the final qualifier (Programming)
- Flask deploy applications using Nginx on Ubuntu (Server)
- MySQL multi-instance configuration (Database)
- C # asynchronous delegates (Programming)
- Under CentOS using yum command to install the Task Scheduler crontab (Linux)
- Use Bosh deploy CloudFoundry problems encountered on OpenStack (Server)
- installation of Vim plugin YouCompleteMe under Ubuntu 15.04 (Linux)
- Use Docker containers (Linux)
- Linux file permissions to modify the command: chmod (Linux)
- Use Visual Studio 2015 to develop Android program (Programming)
- Setting Squid successful anti-hotlinking (Linux)
- Configuring VMWare FreeBSD9.2 remote debugging kernel source code (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.