Ax M
Ax M

Reputation: 370

How to display processes that are using memory in an given range

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.

Upvotes: 1

Views: 551

Answers (2)

LMC
LMC

Reputation: 12662

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.

Upvotes: 1

Abhishek Keshri
Abhishek Keshri

Reputation: 3234

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.

Upvotes: 0

Related Questions