-2

I am running a memory intensive c++ application and it is being killed by the kernel for excessively high memory usage. I would have thought that the os will automatically use swap when ram gets full. However, I don't think my swap space is getting utilised.

I have read the following two questions, but I can't relate it to my problem.

"How to avoid running out of memory in high memory usage application? C / C++"

Who "Killed" my process and why?

I will be grateful if someone can give me some hints/pointers to how I may solve this problem. Thanks.

Edit: I am running my application on a 64 bit machine linux machine. My ram and swap is 6gb and 12gb respectively.

Community
  • 1
  • 1
  • Is your app 32 or 64 bit? – Component 10 May 05 '15 at 10:42
  • sorry, forgot to mention...it is 64 bit – Musabbir Majeed May 05 '15 at 10:45
  • Have you checked how much memory your program uses when it got killed before saying "I don't think swap is used" or even posting this question? – user3528438 May 05 '15 at 10:53
  • Saying _I will be grateful if someone can give me some hints/pointers to how I may solve this problem._ is very unclear: Are you trying to figure out the code that causes the application to be closed? Are you trying to change/manage the way swap space is used by the system? Are you asking how to write your application differently? It is possible that the application does something ridiculous with memory (like attempting to allocate `std::numeric_limits::max()` bytes) in which case, it won't matter how you change swap space usage. What direction are you looking to, for a solution? – utnapistim May 05 '15 at 11:03
  • 1
    Learn how to use [valgrind](http://valgrind.org/) -after compiling your application with debug info `g++ -g -Wall -Wextra` – Basile Starynkevitch May 05 '15 at 11:04

2 Answers2

2

I suspect your process is asking for more memory than is available. In situations where you know you're going to use the memory you ask for, you need to disable memory overcommit:

echo 2 > /proc/sys/vm/overcommit_memory

and/or put

vm.overcommit_memory=2

in /etc/sysctl.conf so the setting survives reboots.

If your process asks for 32 GB of RAM on a machine with 16 GB of RAM + swap, your malloc() (or new...) calls might very well succeed, but once you try to use that memory your process is going to get killed.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
0

Perhaps you have (virtual) memory framgentation and are trying to allocate a large block of memory which the OS cannot find as a contiguous block? For instance an array would require this, but if you create a large linked list on the heap you should be able to allocate non-contiguous memory.

How much memory are you trying to allocate and how, and do you have suffiecient amount of free resources? If you debug your application what happens when the process is getting killed?

10100111001
  • 735
  • 15
  • 31