6

Suppose that I want to terminate a process on Linux machine with swap (which is actually a zram). The swap is a half of RAM's size. There is only 10% of free space in RAM and the swap is almost full too.

The process is using only 2% of RAM but about 90% of swap.

Doing a soft close (SIGTERM) and allowing the process to catch the signal and close by itself would cause unswapping all swapped mappings, but there is no enough free RAM to fit the whole process.

Because of that it may be better to kill the process with SIGKILL but I'm still afraid that OOM-killer would kill other processes or even whole X session or init due to run out of memory.

So is sending a kill signal make kernel to move swapped parts of process into physical memory? (What should I expect? Does it depend on kernel's version?)

If so, what to do in such case? The goal is to terminate the process without touching the rest (there are other important processes running).

Moreover how to kill it correctly when it's not a one process but a processes' tree and I can't let the application terminate by itself?

  • 1
    It should just terminate the process and free it's memory, without un-swapping it. – Golar Ramblar Apr 13 '17 at 14:59
  • 2
    Unix doesn't swap in entire processes, it uses paging, and only swaps in the pages that are actually needed. – Barmar Apr 13 '17 at 22:27
  • 1
    And it uses virtual memory, so a process can be bigger than available RAM. – Barmar Apr 13 '17 at 22:28
0

regarding killing the whole process tree, you may try this:

# in pid is saved pid of the parent process
CPIDS=`pgrep -P $pid` # gets pids of child processes
for cpid in $CPIDS ; do kill -9 $cpid ; done # first kill children
kill -9 $pid # then the parent (yeah, that sound kinda bad)
0

Generally-speaking, pages are not swapped back into RAM unless they are needed. That is, the page would only be loaded back into RAM if the page is actually accessed by something (causing a page fault exception in the MMU which the OS would handle by loading the page back into RAM before allowing the accessing thread to continue). Hard-killing the process (with SIGKILL) does not run any of the process's threads, so the killed process can't try to access any of its pages, and those pages should not get loaded into RAM.

In fact, even if you don't kill the process and it does resume running, it will still swap in (to RAM) only the pages it actually accesses. Furthermore, if all RAM is full and it needs to access another page that is not in RAM, the OS will select some other page of RAM to swap out (to disk) to make room in RAM for the page your program needs to access. As long as there's some space in the swap partition on disk, this swapping of pages can continue indefinitely, keeping everything alive, without triggering the OOM killer. Of course, if this happens a lot, it can really slow down your machine because it's spending most of its time moving memory between RAM and disk instead of executing program instructions. This is called "thrashing".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.