Linux top memory usage

3 Ways to make Top Command sort by Memory

Top is a very powerful command to periodically display a sorted list of system processes. The default sorting key is %CPU on Linux. Below we collect 3 ways to sort processes by memory.

Understanding top command in Linux

The top command in Linux is used to display information about the current running processes on your system. The top command can be used to see the CPU usage, memory usage, and swap usage for each process.

It can also be used to see the PID, username, and command line for each process. The top command is a useful tool for understanding what processes are running on your system and how they’re using resources.

3 Ways to Sort Top Command by memory usage

  1. press shift+m after running the top command
  2. sort mem usage per process in the interactive menu. More details are below.
  3. run command top -o +%MEM

The best way to sort the top command by memory usage is by pressing shift+m after running the top command. Open a terminal window. Type in top command. This will display the information about the current running processes on your system. Press shift+m. This will sort the process by memory usage.

Tip: A leading ‘+’ will force sorting high to low, whereas a ‘-” will ensure a low to high ordering.

Sort By memory Usage per-process in the top command interactive menu

  • press Shift+f to enter the interactive menu
  • press the up or down arrow until the %MEM choice is highlighted
  • press s to select %MEM choice
  • press enter to save your selection
  • press q to exit the interactive menu

Sort By Memory in top batch mode

We can use top batch mode to capture the process info. The following example will output the highest memory process in batch mode.

for i in ;do date; top -b -o +%MEM | head -n 17|tail -11;sleep 5;done

Sort By VIRT RES Memory Metrics in top command

We have the following metrics to check memory resources in the top command.

  • %MEM: The percentage of the system’s physical memory in use by this process
  • VIRT: Virtual memory size
  • RES: Resident set size: the total physical memory in use by this process

Here is more info about VIRT and RES.

  • VIRT: Called VSZ in the ps command and VIRT in top, this is the total amount of memory mapped by a process. It is the sum of all the regions shown in
    /proc//map.
  • RES: Called RSS in ps and RES in top, this is the sum of memory that is mapped to physical pages of memory. This gets closer to the actual memory budget of the process.
Читайте также:  Узнать версию сборки линукс

So we can also use the following commands to sort these two memory metrics.

  • for i in ;do date; top -b -o +VIRT | head -n 17|tail -11;sleep 5;done
  • for i in ;do date; top -b -o +RES | head -n 17|tail -11;sleep 5;done

Linux Troubleshooting Guide:

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

howtouselinux.com is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative.

Источник

How to Make Top Command Sort by Memory Usage

Top is a Linux process and resource usage monitoring utility. It allows users to view real-time information about the running processes and threads managed by the system’s kernel. Because of its interactivity, top enables users to perform tasks, such as filtering for specific processes, filter processes by users, PID, and kill processes.

This guide will walk you through the basics of using the ps command to locate specific information about the system.

Basic Top Utility Usage

To launch the top utility, use the top command in the terminal. Using this command will spawn an interactive session showing system resource usage and running processes:

The upper part shows the resource usage. This output is similar to that of uptime and the free command in Linux.

To turn these values off, press “m” to hide the memory usage information and “l” to hide the uptime information.

To scroll through the running processes, use the up and down arrow key. To quit, press “Q”.

Top Output

The lower part of top command contains information about running processes. Let us focus on this.

The output uses a column-based organization, with an identifier on each:

  • PID: This column shows the unique ID of each process.
  • PR: This column shows the priority of the task.
  • NI: This column shows the nice value of the process. A positive value indicates low priority, while a negative value indicates high priority.
  • VIRT: This column represents the total virtual memory used by the process.
  • RES: This column shows the total actual memory used by the process.
  • SHR: This column shows the total amount of shared memory used by the process.
  • S: This column shows the process state in a single letter.
  • %CPU: This column shows percentage CPU usage per process.
  • %MEM: This column shows percentage Memory usage.
  • TIME+: This column shows CPU time used by the process calculated to hundredths of a second.
  • COMMAND: This column shows the process name.

How to Make Top Filter Processes by Memory Usage

You can also filter processes by memory usage in top. To do this, press SHIFT + m as shown:

Top will filter the processes by memory usage in descending order. Doing this can help identify the process using the most memory, giving you a chance to take action.

Читайте также:  Kali linux генератор паролей

To filter by actual memory usage, use the command:

Similarly, the command will filter the memory usage in descending order.

You can also interactively choose the filter parameter. To do this, press SHIFT + F and select MEM as:

How to Filter Process by User

To show processes from a specific user, use the command:

For example, to show processes from the ubuntu user; enter the command:

How to Show the Parent and Child Processes

To show the parent and child processes while top is running, press V. This will give you an output similar to the one shown below:

How to Kill All Processes

To kill a process in top, press k and enter the PID of the process.

Press enter to execute the kill command. This will terminate the process with the specified PID.

Conclusion

Top is a handy utility that makes it possible to understand and manage the Linux System processes. Besides what we have discussed in this tutorial, top has tons of other functionalities.

To understand how you can customize and use top to its full potential, consider the manuals.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

How to display `top` results sorted by memory usage in real time?

How can I display the top results in my terminal in real time so that the list is sorted by memory usage?

On Linux, > and < move the sort column right and left. Since the %MEM column is just right of the %CPU column, which is also the default sort column, it takes only one keystroke to switch between the two. I know, your question has the macintosh tag, that's why I'm writing this answer as a comment.

If using htop , in addition to shift + M , you will likely want to turn off the display of threads and just show the main process memory consumption with shift + H . See unix.stackexchange.com/a/10403/27902.

9 Answers 9

Use the top command in Linux/Unix:

  • press shift + m after running the top command
  • or you can interactively choose which column to sort on
    • press Shift + f to enter the interactive menu
    • press the up or down arrow until the %MEM choice is highlighted
    • press s to select %MEM choice
    • press enter to save your selection
    • press q to exit the interactive menu

    Or specify the sort order on the command line

    # on OS-X top -o MEM # other distros top -o %MEM 

    References

    @GabrielHautclocq It must depend upon your distribution of Linux, and the package bundled with it. Debian 7 uses procps-ng and there is no -o option at all in that version. SHIFT-M works for me once top is launched.

    The command line option -o (o standing for «Override-sort-field») also works on my Xubuntu machine and according to the Mac man page of top it should work on a Macintosh too. If I want to short by memory usage I usually use

    which sorts by the column %MEM . But I can use VIRT , RES or SHR too. On a Macintosh I would probably use mem or vsize .

    I don’t know why or how but this is pretty much different between Unix systems and even between Linux distributions. For example -o isn’t even available on my Raspberry running Wheezy. It may be worth give it a try though.

    Источник

    How to see top processes sorted by actual memory usage?

    If I understand correctly, the system has only 362 MB of available memory. My question is: How can I find out which process is consuming most of the memory? Just as background info, the system is running 64bit OpenSuse 12 .

    12 Answers 12

    use quick tip using top command in linux/unix

    and then hit Shift + m (i.e. write a capital M ).

    SORTING of task window For compatibility, this top supports most of the former top sort keys. Since this is primarily a service to former top users, these commands do not appear on any help screen. command sorted-field supported A start time (non-display) No M %MEM Yes N PID Yes P %CPU Yes T TIME+ Yes 

    Or alternatively: hit Shift + f , then choose the display to order by memory usage by hitting key n then press Enter . You will see active process ordered by memory usage

    hi codecowboy, perhaps you can look at commandlinefu.com/commands/view/3/… for more detailed memory used in my server i am using third party app like newrelic.com

    First, repeat this mantra for a little while: «unused memory is wasted memory». The Linux kernel keeps around huge amounts of file metadata and files that were requested, until something that looks more important pushes that data out. It’s why you can run:

    find /home -type f -name '*.mp3' find /home -type f -name '*.aac' 

    and have the second find instance run at ridiculous speed.

    Linux only leaves a little bit of memory ‘free’ to handle spikes in memory usage without too much effort.

    Second, you want to find the processes that are eating all your memory; in top use the M command to sort by memory use. Feel free to ignore the VIRT column, that just tells you how much virtual memory has been allocated, not how much memory the process is using. RES reports how much memory is resident, or currently in ram (as opposed to swapped to disk or never actually allocated in the first place, despite being requested).

    But, since RES will count e.g. /lib/libc.so.6 memory once for nearly every process, it isn’t exactly an awesome measure of how much memory a process is using. The SHR column reports how much memory is shared with other processes, but there is no guarantee that another process is actually sharing — it could be sharable, just no one else wants to share.

    The smem tool is designed to help users better gage just how much memory should really be blamed on each individual process. It does some clever work to figure out what is really unique, what is shared, and proportionally tallies the shared memory to the processes sharing it. smem may help you understand where your memory is going better than top will, but top is an excellent first tool.

    Источник

Оцените статью
Adblock
detector