Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Linux OOM killer mechanism     - Ubuntu Tutorial: How to Upgrade a New Linux Kernel 3.12.7 on Ubuntu (Linux)

- Kali Linux 2.0 U disk installation errors Your installation cd-rom could not be mounted (Linux)

- MySQL5.7 implement virtual column expression index (Database)

- CentOS yum install LAMP (Server)

- shell script: MySQL startup script simple (Database)

- Python-- for anomalies and reflection of objects articles (Programming)

- Repair CentOS 6.4 Grub boot (Linux)

- To install the iNode client on UbuntuKylin 13.10 (Linux)

- Installation and configuration of phpMyAdmin under CentOS (Database)

- CentOS7 installed MySQL (Database)

- FileZilla install on Ubuntu 14.10 (Linux)

- Linux System Getting Started Tutorial: How do you know Shell which is currently being used (Linux)

- Linux System Getting Started Tutorial: How to Force Change your password at next logon Linux (Linux)

- Linux Getting Started tutorial: build your own Vim (Linux)

- MySQL completely uninstall and install Configuring Character Sets under Linux (Database)

- Learning and Practice (Linux)

- Linux Desktop allows exceptionally different launch applications (Linux)

- How to Install Android Studio on Ubuntu 15.04 / CentOS7 (Linux)

- Linux log management make the system more secure (Linux)

- Why do you need close contact Rust 1.0 (Programming)

  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.
- PPA on Ubuntu Linux installation Plank 0.8.0 (Linux)
- How to use the tab in Vim carried Python code completion (Linux)
- How to create an alternative Android / iOS connected wireless hotspot AP in Ubuntu 15.04 (Linux)
- Linux common network tools: Scan routing of mtr (Linux)
- CentOS 6.6 x64 Oracle Database 11gR2 RAC automated installation scripts (Database)
- Analysis examples: Intrusion Response Linux platform Case (Linux)
- Linux firewall settings instance (Linux)
- In-depth understanding of PHP ini configuration (Server)
- The basic principles for the protection of a good linux server security (Linux)
- Fedora10 use Git version Configuration Management (Linux)
- How to create a cloud encrypted file system in Linux systems (Linux)
- On the PC goes heavy security watch your startup items (Linux)
- C ++ Const breaking rules (Programming)
- To change CentOS7 runlevel (Linux)
- Getting Started with Linux: Nginx Web Server How to Block Specific User Agents (UA) (Server)
- How to properly set up a Linux swap partition (Linux)
- Android project using the command to create and install the package (Programming)
- Several start-up mode of Tomcat (Server)
- Create RAID 1 (mirroring) with two disks (Linux)
- ORA-38856: Unable instance UNNAMED_INSTANCE_2 (redo thread 2) marked enabled (Database)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.