Optimized configuration of several kernel parameters in linux

1. Set the memory allocation strategy: vm.overcommit_memory

#View configuration

cat /proc/sys/vm/overcommit_memory

#explain

0, means that the kernel will check whether there is enough available memory for the application process; if there is enough available memory, the memory application is allowed; otherwise, the memory application fails and an error is returned to the application process.

1. Indicates that the kernel allows all physical memory to be allocated, regardless of the current memory state.

2. Indicates that the kernel is allowed to allocate more memory than the sum of all physical memory and swap space

#Modify the configuration value to 1, three ways (root privilege):

A. echo "vm.overcommit_memory=1" > /etc/sysctl.conf or vi /etc/sysctl.conf, then reboot to restart the machine

B. echo 1 > /proc/sys/vm/overcommit_memory does not need to start the machine to take effect

C、sysctl vm.overcommit_memory=1

#What is Overcommit and OOM

Linux replies "yes" to most requests for memory so that it can run more and larger programs. Because after applying for memory, the memory will not be used immediately. This technique is called Overcommit. OOM killer (OOM=out-of-memory) occurs when Linux finds that memory is insufficient. It will choose to kill some processes (userland processes, not kernel threads) in order to free memory.

When the oom-killer happens, which processes does linux choose to kill? The function that selects the process is the oom_badness function (in mm/oom_kill.c), which counts the number of points (0~1000) for each process. The higher the number of points, the more likely the process will be killed. The number of points per process is related to oom_score_adj, and oom_score_adj can be set (-1000 is the lowest, 1000 is the highest).

 

2、swapiness

#explain

Linux will use a part of the hard disk as a SWAP partition for process scheduling--a process is a running program--turn the currently unused process to 'standby' or even 'sleep'. Use, and then adjust it to 'active', the sleeping process will lie down on the SWAP partition to sleep, freeing up memory for the 'active' process.

If the memory is large enough, you should tell linux not to use the SWAP partition too much, you can modify the value of swappiness. When swappiness=0, it means that the physical memory is used to the maximum extent, and then the swap space is used. When swappiness=100, it means that the swap partition is actively used, and the data in the memory is moved to the swap space in time.

#Preparation before modification

cat /proc/version to view the linux kernel version

If the linux kernel version is < 3.5, then swapiness is set to 0, so the system would rather swap than oom killer (kill the process)

If the linux kernel version is >=3.5, then swapiness is set to 1, so the system would rather swap than oom killer

#Temporary modification

echo 0 > /proc/sys/vm/swappiness

#Permanent modification, requires reboot

echo vm.swapiness=0 >> /etc/sysctl.conf or vi /etc/sysctl.conf, then reboot to restart the machine

 

3、ulimit

#explain

Display (or set) the limit (limit) of the resources that the user can use. This limit is divided into soft limit (current limit) and hard limit (upper limit). The hard limit is the upper limit of the soft limit, and the application is running during the running process. The system resources used do not exceed the corresponding

Soft limit, any exceeding will result in the termination of the process.

#Command parameter explanation

-a list all current resource limits

-c Set the maximum value of the core file. Unit: blocks

-d Set the maximum value of the data segment of a process. Unit: kbytes

-f The maximum file size of the file created by Shell, unit: blocks

-h Specifies to set a hard limit for a given resource. If the user has root privileges, the hard limit can be increased. Any user can reduce the hard limit

-l Maximum amount of physical memory that can be locked

-m The maximum value of resident memory that can be used, unit: kbytes

-n Maximum number of files each process can open simultaneously

-p Set the maximum value of the pipeline, the unit is block, 1block=512bytes

-s specifies the maximum value of the stack: unit: kbytes

-S Specifies to set a soft limit for the given resource. The soft limit can be increased to the value of the hard limit. If neither the -H nor -S flags are specified, the limits apply to both

-t specifies the number of seconds used by each process, unit: seconds

-u Maximum number of concurrent processes that can run

-v The maximum virtual memory that can be used by Shell, unit: kbytes

#View the maximum number of resources opened by a single process (ie concurrent connections)

ulimit -n

#Change setting

ulimit -n 65536

 

4、somaxconn

#explain

For a TCP connection, Server and Client need to establish a network connection through three-way handshake. When the three-way handshake is successful, we can see that the state of the port changes from LISTEN to ESTABLISHED, and then data can be transmitted on this link.

Each port in the listening state has its own listening queue. The length of the listening queue

#View queue size

cat /proc/sys/net/core/somaxconn

#Modify queue size

echo 1024 > /proc/sys/net/core/somaxconn

or

Permanently effective: 

vim /etc/sysctl.conf net.core.somaxconn=32768 

sysctl -p 

Effective immediately:

sysctl -w net.core.somaxconn=32768

Remark

The sysctl command is used to dynamically modify the kernel's operating parameters while the kernel is running. The available kernel parameters are in the directory /proc/sys. It contains some advanced options for the TCP/ip stack and virtual memory system, which can make it noticeable to experienced administrators

system performance. More than five hundred system variables can be read and set with sysctl.

-n: do not print keywords when printing values;

-e: ignore unknown keyword errors;

-N: print name only;

-w: Use this when changing sysctl settings;

-p: load kernel parameter settings from the configuration file "/etc/sysctl.conf";

-a: print all currently available kernel parameter variables and values;

-A: Print all currently available kernel parameter variables and values ​​in tabular form.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326081285&siteId=291194637