overcomit_memory :
0 : When the user requests the memory, the system will determine how much memory is left, and if not enough, it will fail.
1 : When the user requests the memory, the system does not make any check of the task memory enough to use the memory more than the available memory.
2 : The amount of memory requested by the user at one time does not allow more than Available Memory the size.
Overcommit_ratio :
when overcomot_memory = 2 This parameter is valid, this parameter determines the system Available Memory the size.
Calculation formula: ( physical-ram-size ) *ratio/100 + (swap-size) ;
Current memory usage of the machine
[[email protected]/]# free-m (m)
Total used free shared buffers Cached
mem:5792 513 5279 0 40 158
-/+ buffers/cache:314 5478
swap:1983 0 1983
[Email protected]/]#
Test code:
#include <stdio.h> #include <stdlib.h>size_t max_num = 0;int main (int argc, char **argv) {void *block; void *tmp_block; size_t block_size[] = {1024*1024, 1024, 1}; int i=0; int count; For (i=0, i<3; i++) {for (count = 1;; count + +) {block = Ma Lloc (Max_num + block_size[i]*count); if (block) {Tmp_block = block; Max_num + = Block_size[i]*count; printf ("Max_num current malloc size =%lf gb\n", max_num*1.0/1024.0/1024.0/1024.0); Free (block); } else {break; }}} printf ("max_num malloc size =%lf gb\n", max_num*1.0/1024.0/1024.0/1024.0); printf ("The address Is%x \ n ", Tmp_block); printf ("The address End is%x\n", Tmp_block + max_num); while (1);}
Situation ①
overcomot_memory = 0 , Vm.overcommit_ratio = 50 = " max_num malloc size = 7.137890GB
Situation ②
overcomot_memory = 2 , Vm.overcommit_ratio = 50 = " max_num malloc size = 3.958923GB
Situation ③
overcomot_memory = 2 , vm.overcommit_ratio = 99 = "Max_nummalloc size = 6.722099 GB
About system parameters: Vm.overcommit_memory and Vm.overcommit_ratio;