NeoWang
NeoWang

Reputation: 18523

Under what condition does Linux Kernel kills a process leaking memory?

I detected my service process leaking memory on a Linux server, it takes 1.2G of physical memory and consumes more and more.

While I am looking at the code for the memory leak, I notice the process is restarted (This process if managed by supervisord, so it is restarted if killed). There is no error log or panic in the log of the process. So my guess is that it is killed by the kernel.

When does the kernel kill a process that is leaking memory? When it consumes too much memory? or it allocates memory too fast?

Upvotes: 0

Views: 805

Answers (1)

Karthik Balaguru
Karthik Balaguru

Reputation: 7842

Memory leaks can cause your system memory to get low. If the memory gets very low, the OOM(Out Of Memory) killer will be invoked to try to recover from low memory state. The OOM Killer will terminate one or more processes that consume more memory and are of least importance(low priority). Normally, the OOM killer will be invoked incase there is no user address space available or if there is no page available.

OOM killer uses select_bad_process(),badness() to determine and kill the process. These functions determine the process by assigning points/score for all the processes based on various factors such as VM size of process, VM size of its children, uptime, priority, whether it does any hardware access, whether it is swapper or init or kernel thread. The process with highest points/ score(badness) gets terminated/killed.

Also, checkout whether the overcommit behaviour of kernel (/proc/sys/vm/overcommit_memory, /proc/sys/vm/overcommit_ratio) and the limit on the address space for the processes are appropriate.

Valgrind is a very handy tool in such scenarios in identifying memory leaks.

Upvotes: 2

Related Questions