Linux prevents processes from using swap and prevents the oom mechanism from causing the process to be killed __linux

Source: Internet
Author: User


First explains two concepts:
Swap: In Linux, when there is not enough physical memory and a new program requests to allocate memory, Linux chooses to exchange data that is temporarily unused by other programs to the physical disk (swap), and then read it when it is used ( Swap in). The disadvantage of this is obvious, swap in/swap out here the cost is relatively large, compared to the data has been placed in memory, read more disk operations, and disk IO costs. We all know that.
Oom:out of memory, which means that in Linux, because of system memory pressure, the system chooses to protect some system processes and kill some other processes to free up memory.

So how can you do this in Linux two points.
First, prohibit use of Swap
1. You can set/proc/sys/vm/swappiness=0, but this disables all processes using Swap
2. When allocating memory to a process, let it use hugepage memory. Set method:
1>vi/etc/sysctl.conf; add vm.nr_hugepages=hugepage_num (how many pages of large page memory, this must be greater than the maximum memory required by the process, such as mysqld buffer pool=5g, So hugepage_num*page_size>5g); Sysctl-p effective.
2> This prevents the process from using Hugepage: When the process uses Hugepage to allocate memory, it is one-time-allocated and exclusive, and since the one-time is already allocated there is no reason to use swap. You can see from the top command that the process is starting to take up the size of the memory you've set for it.
3. You can set/proc/sys/vm/overcommit_memory=2 (what other values mean, you can yourself, and if you find that this value is greater than 2 in your own test, then the effect is the same as 2) in the case of a value of 2, The maximum amount of memory that can be allocated is in swap size + ram* ((/proc/sys/vm/overcommit_ratio)/100), if it is greater than this direct return error, this is similar to Hugepage allocating memory, also one-time, exclusive (not 100 % sure), but with the top command view, it shows that the current memory size is not the value we set for him. Then why do I think it is exclusive and one-time distribution? Because when you start another process, you have a direct error:
Error occurred during initialization of VM
could not reserve enough spaces for object heap
4. Use MySQL with its own parameters –memlock it locks the memory, the same principle as hugepage, but the flaw is that it must run by root mysqld

Two, how to avoid the process because the oom mechanism was killed.
1. Several documents related to Oom are/proc//oom_adj,/proc//oom_score. The former is a weight value of 17 to 15. A setting of 0 means that you will never be killed, and the greater the value, the easier it is to kill. The latter is a value that is computed to select which processes are killed based on this value.
2. The above position uses the third method in swap to overcommit the parameter, because it can not allocate memory will return an error, so can never reach the memory is exhausted. Oom will have no effect.

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: 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.