1

How can I display processes that are using memory between an given interval in terminal? For exemple: processes that are using between 50 and 100 MB of Memory.

I tried:

ps aux | awk '{print $4}' | sort

but this only displays the memory for every process sorted and not in an interval.

jww
  • 97,681
  • 90
  • 411
  • 885
Ax M
  • 330
  • 3
  • 15
  • Also see [How to see top processes sorted by actual memory usage?](https://stackoverflow.com/q/4802481/608639) and [Check memory per processes and subprocesses](https://stackoverflow.com/q/25495619/608639), [How to get all process ids with memory usage greater than](https://stackoverflow.com/q/26862748/608639), [Script to get user that has process with most memory usage?](https://stackoverflow.com/q/41177409/608639), [A way to determine a process's “real” memory usage, i.e. private dirty RSS?](https://stackoverflow.com/q/118307/608639), etc – jww Apr 29 '18 at 21:16

2 Answers2

1

This will list processes as expected. Remember that ps shows memory size in kilobytes.

ps -u 1000 -o pid,user,stime,rss \
  | awk '{if($4 > 50000 && $4 < 100000){ print $0 }}' \
  | sort -n -k 4,4

Command output:

 3407 luis.mu+ 10:30 51824
 3523 luis.mu+ 10:30 66108
 3410 luis.mu+ 10:30 71060
 3595 luis.mu+ 10:30 74340
 3609 luis.mu+ 10:30 77772
18550 luis.mu+ 16:47 93616

In that case it's showing only 4 fields for user id 1000. To show all processes use

ps -e -o pid,user,stime,rss

From the ps(3) man page under STANDARD FORMAT SPECIFIERS:

rss
resident set size, the non-swapped physical memory that a task has used (inkiloBytes)

If you want to show more fields, check the man page and add fields to -o option.

jww
  • 97,681
  • 90
  • 411
  • 885
LMC
  • 10,453
  • 2
  • 27
  • 52
  • when I type `ps aux` the memory used by every proccess is displayed in procentage (%) and not if Kilobyts as you said. So I tried: `ps aux -u | awk '{if($4 >= 0.05 && $4 <= 0.1){ print $0 }}'` and it worked. But how can I display the memory in kilobytes as you said? – Ax M Apr 26 '18 at 05:27
  • Added clarification to the answer, hope it helps. Try the ps command alone to check the output. – LMC Apr 26 '18 at 13:50
0

For more complex testing, including comparison, inequality, and numerical tests, awk is very useful:

ps aux | awk '{print $4}' | sort | awk '$1 >= 1 && $1 <=2'| cat

Here I am checking the memory usage between 1MB and 2MB using awk and printing them using cat.

Abhishek Keshri
  • 3,074
  • 14
  • 31