Linux performance optimization 11: Memory Allocation Management

Source: Internet
Author: User
Linux performance optimization 11: Memory Allocation Management 10: resource allocation planning http://www.2cto.com/os/201307/228396.html In linux, memory allocation management is mainly controlled by kernel parameters: 1. capacity-related memory adjustable parameters... linux performance optimization 11: Memory Allocation Management 10: resource allocation planning http://www.2cto.com/os/201307/228396.html In linux, memory allocation management is mainly controlled by kernel parameters: 1. capacity-related memory adjustable parameters the following parameters are located in the/proc/sys/vm/directory of the proc file system. Overcommit_memory: Specifies whether to accept conditions for large memory requests. This parameter has three possible values: * 0-default value. The kernel executes heuristic memory for excessive processing by estimating the amount of available memory and rejecting obviously invalid requests. Unfortunately, the memory is deployed using heuristic rather than accurate algorithm computing. this setting may sometimes overload the available memory in the system. If you do not want to over-use it, an error is reported. * 1-The Kernel runs without excessive memory usage. Using this setting will increase the possibility of memory overload, but it can also enhance the performance of a large number of memory tasks. When needed, the application allows excessive use of * 2-memory to reject memory requests that are equal to or greater than the total available swap size and the physical RAM ratio specified by overcommit_ratio. If you want to reduce the risk of memory overuse, this setting is the best. Direct use of swap, memory used = swap + ram * 50% note: When overcommit_ratio is set to 2, it is recommended only for systems with the swap region greater than its physical memory, specify the physical RAM ratio. The default value is 50. Test procedure: ######################################## ######################################## include "stdio. h "# include" stdlib. h "# include" errno. h "# define maxtimes 1000*1000 # define unit 10485760int main () {char * q [1000000]; int I = 1; char * pc; for (I = 1; I <= maxtimes; I ++) {q [I] = (char *) malloc (unit); if (q [I] = NULL) {printf ("malloc error \ n");} printf ("% o \ n", q [I]); pc = (char *) (q [I]); for (int j = 0; j <10485760; j ++) {pc [j] = 'a';} printf ("% s \ n", pc ); printf ("have been malloc % dM \ n", I);} return 0 ;} ######################################## ####################################### test 1: overcommit_memory = 0 is also the default situation. I have 2 GB of physical memory and check that around MB is used after the instance is started. I disabled the swap partition here and compiled it for testing. result: use top to view. out of RES (use of physical memory) reached 1.3g process was Killed (the reason is described later) 1.3g + 600 multi M = 2G View Log: Jul 16 14:24:48 localhost kernel: out of memory: kill process 19066 (A. out) score 131753 or a childJul 16 14:24:48 localhost kernel: Killed process 19066 (. out) vsz: 8432216kB, anon-rss: 137720.kb, file-rss: 92kB Test 2: overcommit_memory = 1, the situation is consistent with Test 1, [root @ localhost Desktop] # free total used free shared buffers cachedMem: 1978696 1873644 105052 0 8268 75012-/+ buffers/cache: 1790364 188332 we can see: when there is about MB left,. out gets stuck for a while, and then starts execution. it will wait until available resources are executed (all resources released by other processes) and will not be killed because For this mode, there is no limit to the memory test. 3: overcommit_memory = 2. overcommit_ratio is used by default. the execution result of the malloc program cannot be obtained if the memory is left blank: have been malloc 266 M here is 2660 malloc error0Segmentation fault (core dumped) o: VIRT -- Virtual Image (kb) The total amount of virtual memory used by the task. it includes des all code, data and shared libraries plus pages that have been swapped out. (Note: you can define the STATSIZE = 1 environment vari-able and VIRT will be calculated from the/proc/#/state VmSize field .) VIRT = SWAP + RES. p: SWAP -- Swapped size (kb) The swapped out portion of a task's total virtual memory image. q: RES -- Resident size (kb) The non-swapped physical memory a task has used. 2. out-of-Memory Kill: Memory insufficiency (OOM) refers to all available Memory, including the computing status in which swap space has been allocated. By default, this status can cause the system panic to stop working as expected. However, setting the/proc/sys/vm/panic_on_oom parameter to 0 will enable the kernel to call the oom_killer function when OOM occurs. Usually oom_killer can kill the theft process and make the system work normally. You can set the following parameters in each process to improve control over processes killed by the oom_killer function. It is located in the/proc/pid/directory of the proc file system, where pid is the process ID. Oom_adj defines a value between-16 and 15 to help determine the oom_score of a process. The higher the oom_score value, the more processes killed by oom_killer. If oom_adj is set to-17, oom_killer is disabled for the process. Note: Any process derived from any adjusted process will inherit the oom_score of the process. For example, if the sshd process is not affected by the oom_killer function, all processes generated by the SSH session will not be affected. This may affect the capabilities of the oom_killer function rescue system when OOM occurs.
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: 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.