1. cat /proc/sys/vm/swappiness
  2. cat /proc/sys/vm/overcommit_memory
  3. cat /proc/sys/vm/overcommit_ratio
  4. # LOWER SWAPS LESS
  5. echo 0 > /proc/sys/vm/swappiness
  6. # DISABLES OVERCOMMITING MEMORY
  7. # overcommit_memory — Configures the conditions under which a large memory request is accepted or denied.
  8. # 2 — The kernel fails requests for memory that add up to all of swap plus the percent of physical RAM specified in /proc/sys/vm/overcommit_ratio. This setting is best for those who desire less risk of memory overcommitment.
  9. echo 2 > /proc/sys/vm/overcommit_memory
  10. # USE UP ALL PHYSICAL RAM
  11. # Specifies the percentage of physical RAM considered when /proc/sys/vm/overcommit_memory is set to 2. The default value is 50.
  12. echo 80 > /proc/sys/vm/overcommit_ratio
  13. ### TO SYSCTL.CONF
  14. vm.swapiness = 0
  15. vm.overcommit_memory = 2
  16. vm.overcommit_ratio = 80
  17. ### C code to use all memory
  18. ### https://stackoverflow.com/questions/1865501/c-program-on-linux-to-exhaust-memory/6617363#6617363
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #define PAGE_SZ (1<<12)
  23. int main() {
  24. int i;
  25. int gb = 2; // memory to consume in GB
  26. for (i = 0; i < ((unsigned long)gb<<30)/PAGE_SZ ; ++i) {
  27. void *m = malloc(PAGE_SZ);
  28. if (!m)
  29. break;
  30. memset(m, 0, 1);
  31. }
  32. printf("allocated %lu MB\n", ((unsigned long)i*PAGE_SZ)>>20);
  33. getchar();
  34. return 0;
  35. }