7

Java process is killing my computer (Intel i3, 8GB RAM). It takes over whole CPU and system starts to hang. I was trying to change niceness of java processes but i have to control it for all time and this is not always possible. So for beginning I tried to construct a command to change process niceness by name. Ended up with something like this:

ps ax -o pid,comm | grep java | awk '{print $1}' | tr "\n" " " | renice -n 5 -p

But it looks like it doesn't work. And I don't know where to go next. Bash script maybe? Run it by cron or by watch every x time? Or is there a better way?

4 Answers 4

11

If you have only one java instance running, simply:

renice -n 5 -p $(pgrep ^java$)
  • $(pgrep ^java$): command substitution; bash replaces this with the output of pgrep ^java$; pgrep ^java$ returns the list of PIDs of the processes whose name matches the regular expression ^java$, which matches all processes whose name is exactly java

If you have multiple java instances running:

for p in $(pgrep ^java$); do renice -n 5 -p $p; done
  • for p in $(pgrep ^java$); do renice -n 5 -p $p; done: almost the same as above; $(pgrep ^java$) is a command substitution; bash replaces this with the output of pgrep ^java$; pgrep ^java$ returns the list of PIDs of the processes whose name matches the regular expression ^java$, which matches all processes whose name is exactly java; this is expanded into the for loop, which assigns a new line of the output of pgrep ^java$ to the variable $p and runs renice -n 5 -p $p at each iteration until the output of pgrep ^java$ is consumed
2
  • That works pretty well. But to take next step, what is better to run this periodically. Some cron or watch command? Jul 2, 2015 at 9:57
  • The first command is also working, if you have one single program running multiple processes (like a parallel processing thread pool in MATLAB).
    – Corbie
    Jun 29, 2020 at 16:07
6

Try this:

pgrep java | xargs -n 1 echo renice -n 5 -p

If output is okay, remove echo.

0

After all I occured it was memory problem. I didn't set maximum memory for Jetty so it tried to allocate 1/4 of all memory. Also I have found final solution for such problems here:

OOM killer not working?

0

The easiest way I've found is using the pidof command:

renice <new niceness> -p $(pidof <process name>)

found from: http://www.commandlinefu.com/commands/view/4614/renice-by-name

For your situation, probably put that command into crontab -e (as root) with whatever period you want (say every 30 mins). It seems like you should find the root cause of your new and resource-hogging java processes and see if you can fix/prevent that.

You must log in to answer this question.

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