Great writeup on Linux memory management

…especially as it relates to committed memory…

http://www.etalabs.net/overcommit.html

int main() {
  void *m = malloc(6000*1024*1024);
  if (m)
    printf("Memory allocation succeeded!\n");
  else
    printf("Memory allocation failed!\n");
  //memset(m,0,2000*1024*1024);
  sleep(30);
  return 0;
}
[root@cmhlcarchapp01 ~]# sysctl -w vm.overcommit_memory=2
[root@cmhlcarchapp01 ~]# ./mem &
[1] 7890
[root@cmhlcarchapp01 ~]# Memory allocation succeeded!

[root@cmhlcarchapp01 ~]# ./mem &
[2] 7883
[root@cmhlcarchapp01 ~]# Memory allocation failed!


[root@cmhlcarchapp01 ~]# sysctl -w vm.overcommit_memory=0
[root@cmhlcarchapp01 ~]# ./mem &
[1] 7890
[root@cmhlcarchapp01 ~]# Memory allocation succeeded!

[root@cmhlcarchapp01 ~]# ./mem &
[2] 7891
[root@cmhlcarchapp01 ~]# Memory allocation succeeded!

You can also see an example of the JVM failing if overcommit is disabled…

[root@cmhlcarchapp01 ~]# sysctl -a | grep overcom
vm.overcommit_memory = 0
vm.overcommit_ratio = 50
vm.overcommit_kbytes = 0
vm.nr_overcommit_hugepages = 0
[root@cmhlcarchapp01 ~]# java -Xms6144m -version
java version "1.7.0_91"
Java(TM) SE Runtime Environment (build 1.7.0_91-b33)
Java HotSpot(TM) 64-Bit Server VM (build 24.91-b03, mixed mode)
[root@cmhlcarchapp01 ~]# sysctl -w vm.overcommit_memory=2
vm.overcommit_memory = 2
[root@cmhlcarchapp01 ~]# java -Xms6144m -version
Error occurred during initialization of VM
Could not reserve enough space for object heap
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
[root@cmhlcarchapp01 ~]#

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.