Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Two questions:

  1. Is there some way to turn off swapping for a given process, i.e., is it possible to kill a certain process if it uses all the system's primary memory available?

  2. How do I deactivate memory overcommit in a Linux system?

Context:

I'm running a distributed application, and I'd like to have a process killed once the system ran out of primary memory. The process is quite heavy on hard disk communication, for what I find reasonable to check the memory usage programatically, like:

std::ifstream statm("/proc/self/statm");
size_t mem_virt, mem_rss, mem_shared;
statm >> mem_virt >> mem_rss >> mem_shared;
if (mem_virt == MAX_SYSTEM_PRIMARY_MEMORY) {
    // kills current process
}

But if there was some set of command lines capable of turning off the swap for a given process, that would be definitely better.

Yet, I still don't know how to turn off the overcommit. Any ideas?

share|improve this question
up vote 1 down vote accepted

You can disallow swapping in your application by calling mlockall(MCL_CURRENT|MCL_FUTURE), see the man page.

You can disable overcommit:

$ sysctl vm.overcommit_memory=2    # policy number 2
$ sysctl vm.overcommit_ratio=0     # ratio = 0%

Running without swap is generally a terrible idea. Now all anonymous mmaps will be be forced to be backed by RAM, even if they are not used. Unix needs swap, even if nothing is actually swapped.

share|improve this answer
    
This sounds to me like the best solution. Thanks very much! (: – Rubens Aug 20 '13 at 3:24
1  
If I read correctly (kernel.org/doc/Documentation/vm/overcommit-accounting) by setting vm.overcommit_ratio=0 like above, you limit total memory allocations to the size of only the swap. I tried the above, and got into a state where nothing can run as all allocations fail. I guess the system has already used more memory than the size of the swap. Even ls gives -bash: fork: Cannot allocate memory. I think you meant to set the percentage to 100, no? – Edd Barrett Jan 13 at 15:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .