Theory:
In OS with below kernel parameters:1 2 | vm.overcommit_memory = 2 vm.overcommit_ratio = 50 |
For example, if OS has 48G RAM with 48G SWAP as shown below:
1 2 3 4 5 | [root] # free total used free shared buffers cached Mem: 49316552 841556 48474996 0 150432 466384 -/+ buffers /cache : 224740 49091812 Swap: 50307508 0 50307508 |
Experiment:
Let's do a simple test to prove that.1. Prepare "mem.c" C program which can allocate memory using function malloc:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> #include <stdlib.h> int main(){ int *ptr; unsigned long i,n; printf ( "Enter number of int(4 byte) you want to allocate:" ); scanf ( "%lu" ,&n); printf ( "Allocating %lu bytes......\n" ,n* sizeof ( int )); ptr=( int *) malloc (n* sizeof ( int )); if (ptr==NULL){ printf ( "ERROR!Memory not allocated!" ); exit (0); } printf ( "Filling int into memory.....\n" ); for (i = 0; i < n; i++){ ptr[i] = 1; } printf ( "Sleep 10 seconds......\n" ); sleep(10); printf ( "Free memory.\n" ); free (ptr); return 0; } |
1 | gcc mem.c -o mem |
1 2 3 4 5 6 | [root] # ./mem Enter number of int(4 byte) you want to allocate:10000000000 Allocating 40000000000 bytes...... Filling int into memory..... Sleep 10 seconds...... Free memory. |
1 2 3 4 5 | [root] # free total used free shared buffers cached Mem: 49316552 841556 48474996 0 150432 466384 -/+ buffers /cache : 224740 49091812 Swap: 50307508 0 50307508 |
1 2 3 4 5 | [root] # free total used free shared buffers cached Mem: 49316552 39978952 9337600 0 150432 466408 -/+ buffers /cache : 39362112 9954440 Swap: 50307508 0 50307508 |
Note, previously we mentioned that the total virtual memory size is SWAP size +50% RAM size, that is only for calculating the size. In fact, RAM is firstly used before starting to use SWAP.
1 2 3 4 5 6 | [root] # ./mem Enter number of int(4 byte) you want to allocate:18000000000 Allocating 72000000000 bytes...... Filling int into memory..... Sleep 10 seconds...... Free memory. |
1 2 3 4 5 | [root] # free total used free shared buffers cached Mem: 49316552 49179688 136864 0 700 21856 -/+ buffers /cache : 49157132 159420 Swap: 50307508 21593608 28713900 |
1 2 3 4 | [root] # ./mem Enter number of int(4 byte) you want to allocate:20000000000 Allocating 80000000000 bytes...... ERROR!Memory not allocated! |
So one suggestion after adding more physical memory, is to add more SWAP also.
thanks for the scripts
ReplyDelete