2

I have a multi-user system, where active users should get a fair share of the available memory.

Can I set up Linux to kill a process of the user with the highest total memory consumption?

This will happen when the system is out of memory. I suppose the largest of this user's processes should be killed.

I don't want to set memory limits on users, since their number varies and because there would be wasted memory if most of the users are not working. Killing the largest process would work, but it should be per user. Otherwise one user might spawn many small processes and cause another user's medium process to be killed.

  • I suspect that's only possible if you make every user run in their own container. I think that wouldn't have any impact on performance, the difficulty would be the initial setup. – Gilles Mar 8 '16 at 22:46
  • I'm not an expert, but I can ask people to help. With containers, will a lone user be able to use the whole machine? Are containers hard to set up? I was thinking of making OOM score adjustments according to user memory usage. Does that make sense? – Gerenuk Mar 9 '16 at 9:24
  • I'm not an expert either. AFAIK the system keeps track of memory used by each container. You can set a limit per container, but you don't have to. The problem about “user memory usage” is that not only does Linux not keep track of it, but it isn't a well-defined notion, because memory can't be accounted per process: a lot of memory is used by multiple processes (in particular file cache). This is still true with containers but there is more separation so you get a more accurate picture (but not perfect). – Gilles Mar 9 '16 at 10:22
  • The details aside, hard limits on memory isn't the solution here, since it would mean there are hard limits for users. I want users to have 50% when there are two users online and 100% if they are alone. This would be solved "naturally", by killing a processes from the greediest user, when memory runs out. – Gerenuk Mar 9 '16 at 12:02
1

The below will kill the process using the most memory on the box. Is that what you were looking for, or does it have to be the process of the user who is using the most memory (which may not be the greatest offender and reason for the memory shortage)?

#!/bin/bash

free=`awk '/^Mem/ {print $3}' <(free -m)`

psout=`ps axo pid,args,pmem,rss,vsz --sort -pmem,-rss,-vsz | head -2`
pid=`echo $psout | cut -d " " -f 6`

if (( $free < 1 )); then

kill -9 $pid

fi;

Edit: I see your reasoning now, will attempt to modify the above code.

Edit 2: Since I don't have to complete this project today, let me put you on the right path. You can download smem (python script) from the below URL: https://www.selenic.com/smem/download/

Running as root with -u option will give you the total amount of memory used by each user. Grab that in an array, sort by mem used, then just add a -u to the ps command above to kill the worst offender of that user.

  • Yes, the victim should be the user who is using the most memory. And his biggest process should be killed. But also note it should be handled the usual automatic way of the Linux OOM killer or something similar. Only the OOM can detect memory overflow without running a script too often. – Gerenuk Mar 8 '16 at 16:11
  • Indeed. It's not for today :) But a solution that integrates with Linux OOM killer would be nicer. Since otherwise I have to probe the memory all the time. Letting it get full is not an option, since then OOM will just kill the largest process. It should be an automatic solution, too. – Gerenuk Mar 8 '16 at 16:31

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.