87

I'm looking for somthing like top is to CPU usage. Is there a command line argument for top that does this? Currently, my memory is so full that even 'man top' fails with out of memory :)

107

From inside top you can try the following:

  • Press SHIFT+f
  • Press the Letter corresponding to %MEM
  • Press ENTER

You might also try:

$ ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5

This will give the top 5 processes by memory usage.

|improve this answer|||||
  • 18
    Under Linux, simply press M to sort by physical memory usage (RES column). Under *BSD, run top -o res or top -o size. But htop is a lot nicer and doesn't even consume more memory than top (however it's not part of the basic toolset so you might not have it installed). – Gilles 'SO- stop being evil' Dec 19 '10 at 13:38
  • 2
    I had to press s to sort by %MEM and, then hit escape for top. – Bryce Guinta Jun 26 '16 at 20:40
11

If you have it installed I like htop once launching it you can press f6, down arrow (to MEM%), enter to sort by memory.

|improve this answer|||||
7

Once top starts, press F to switch to the sort field screen. Choose one of the fields listed by pressing the key listed on the left; you probably want N for MEM%

|improve this answer|||||
  • 2
    If you want MEM%, pressing 'M' does the same stated above. 'c' adds command line parameters to the process list, may be informative for your problem. – wag Dec 19 '10 at 8:46
6

In Solaris the command you would need is:

prstat -a -s size

This will list all processes in order of descending process image size. Note that the latter is based on memory committed to the process by the OS, not its resident physical memory usage.

There are supposedly versions of "top" available for Solaris, but these are not part of the standard installation.

|improve this answer|||||
5

One nice alternative to top is htop. Check it, it is much more user friendly than regular top.

|improve this answer|||||
1

This command will identify the top memory consuming processes:

ps -A --sort -rss -o pid,pmem:40,cmd:500 | head -n 6 | tr -s " " ";z"
|improve this answer|||||
1

It can be achieved in multiple ways, My favourite one is:

The PS way:

  1. [arif@arif ~]$ ps -eo pid,cmd,%cpu,%mem --sort=-%mem

    Where,

    • -e: to select all process
    • -o: to apply to the output format
    • pid,cmd,%cpu,%mem: Output format in exact order. Here, pcpu and pmem can be used instead of %cpu and %mem.
    • But sadly (don't know why) it doesn't work on some machine (Oracle Linux) and some older machine. You can use the following similar alternatives.
  2. [arif@arif ~]$ ps -eo pid,cmd,%cpu,%mem --sort -rss

    Where,

    • -rss: resident set size, the non-swapped physical memory that a task has used
  3. [arif@arif ~]$ ps --cols 120 aux --sort -rss

    Where,

    • --cols 100: to specify column width of the output as sometimes cmd gets very long. This is not necessary if you don't want curtailed commands with arguments.
    • aux: to see every process on the system using BSD syntax

The top way:

[arif@arif ~]$ top -b -o +%MEM

Where,

  • -b: to use top as batch mode.
  • -o: to override sort fieldname followed by a fieldname %MEM

And you can always use head and/or tail to control the output.

|improve this answer|||||
0

You can try ps aux --sort -rss | head or ps aux | sort -nk +4 | tail

|improve this answer|||||
0

Globally: It's always recommended to use a log analyser tool for logging history logs such as Splunk, ELK etc. So that using query language you can easily get the PIDs and its usage by CPU & memory.

AT SERVER/OS LEVEL: From inside top you can try the following:

 Press SHIFT+M  ---> This will give you a process which takes more memory in descending order.

You might also try:

$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -10

This will give the top 10 processes by memory usage. Also you can use vmstat utility to find the RAM usage at same time not for history.

|improve this answer|||||

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.