Top 6 commands to check the memory usage on Linux

System administrators controlling remote Linux systems may need to use the command-line approach to check and troubleshoot high memory usage on a network. Here are the best commands that you can fire on the Linux Terminal and start analyzing the results.

Do you find that your Linux installation has been running slowly lately? If you do, then you need to test which programs or processes are using the system resources. There are, of course, some processes that go rouge and run in the background without your knowledge.

In this article, we shall list top commands to check the memory usage on Linux. The commands should help you understand the memory consumption of the programs and processes. System administrators controlling remote Linux systems may need to use the command-line approach. You should then be able to take necessary action like disabling the process and switching the program with another one that’s compatible with your system.

Commands to check the memory usage on Linux

For the tutorial, we are going to use Ubuntu 19.10. If you are using a different version or another distro, the guide should still work as these are not distro dependent, except the smem and htop tool whose installation is distro dependent.

1. Top

top is an easy way to check the memory usage on your system. It is also one of the most used commands because it offers an interactive, dynamic, and real-time showcase of the running process.

Launch the terminal and use the command:

top

Let’s see it in action below.

top-command-memory
top listing the processes dynamically

As you can see that it is ample information about each process, including its PID, User, RES, Command, Time+, and so on.

If you want only to show only ten processes, then use the command below.

top -b | head - 10

2. Free

free is yet another useful and popular way to check memory consumption. It gives you an eagle-eye view of the memory on your machine. It shows used and free swap and physical memory. Moreover, it also indicates caches and buffers by the kernel.

The free command extracts the information from the “/proc/meminfo” file.

To run the command, type:

free - m

free-command-memory
Showing system’s memory using free

As you can, it has multiple columns, including total, used, free, shared, buff/cache, and available. These columns are filled with information corresponding to Mem and Swap.

3. /PROC/MEMINFO file

If you want more detail information about your system memory consumption and utilization, then you can check out the /proc/meminfo file, which contains various real-time memory usage information. It is a virtual file, as well.

To check its contents and learn about memory usage, you need to use the command below.

cat /proc/meminfo

proc-memory-file
Learning about system memory using /proc/meminfo file

4. VMSTAT

If you want to learn about virtual memory, then you need to use the vmstat command. It is widely used to report virtual memory statistics. By using the command, you can know about memory, processes, block IO, paging, CPU, and disks.

The command for it is as below.

vmstat

vmstat-command-memory-virtual
Using vmstat to learn about virtual memory

As you can see, the output can be a little confusing. The six significant parts of the output include the following, procs, memory, swap, io, system, and CPU. This means that it offers information about the six aspects of your machine.

In proc, it lists two parameters:

  • r: It shows the runnable processes number including waiting or running for run time
  • b: It shows the processes that are in the uninterruptible setup

In the case of memory, it lists the following parameters:

  • swpd: virtual memory used
  • free: idle memory
  • buff: memory used as buffers
  • cache: memory used as a cache
  • inact: inactive memory
  • active: active memory

If you look at the swap, you will find the two parameters:

  • si: memory swapped from disk
  • so: memory swapped to disk

Next, we have IO.

  • bi: blocks from block devices(received)
  • bo: blocks to block devices (sent)

The second last section consists of the following:

  • in: total number of interrupt
  • cs: total number of context switches/second

Lastly, CPU list the following:

  • us: non-kernel core time spent
  • sy: kernel code time spent
  • id: idle time spent
  • wa: time spent waiting for IO
  • st: time used from the virtual machine

5. SMEM

The smem command is useful for generating numerous reports on Linux systems. It doesn’t come pre-installed with the Ubuntu distro version we are using. To install it, you need to use the following:

sudo apt-get install smem

So, what exactly smem offers?

It is a tool that provides various memory-based reports. With it, you can generate Unique Set Size(USS), Resident Set Size(RSS), and Proportional Set Size(RSS) reports.

Each report has its value and offers something unique. Let’s discuss them below.

  • USS: List all the unshared memory
  • RSS: List a standard physical memory measure
  • PSS: List memory used by applications and libraries in the virtual memory system.

Note: Only use it if you have Python 2.x version installed. It should throw an error if you use a Python 3.x version or above.

6. HTOP

The last one we are going to discuss is htop. Unlike other commands, it offers an interactive view of the processes. It is kind of similar to top but provides a better view and control over the information. For instance, it lets you scroll horizontally and vertically.

To install it, you need to use the following command:

sudo apt install htop

Once installed, you need to type htop in command prompt to run it.

htop-memory
Interactive htop view

Conclusion

This leads us to the end of our article on top commands to check the memory usage on Linux. We hope you found out the one best suited for you. Do you find any other commands that are important and not mentioned here? What are the benefits that it has? Use the form below to let FOSSLinux readers know.

Divya Kiran Kumar
I'm the Editor of FOSS Linux. I worked as a Software Engineer before taking up blogging as my full-time job. I enjoy using Linux, and can't imagine anything else for my PC. Apart from writing for FOSS Linux, I enjoy reading non-fictional books. Sapiens was my favorite last read. I hope you enjoy reading and using this blog to enhance your Linux experience! Have a great day ahead!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,419FansLike
377FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

Basics of Working with the SQLite Database in Python

A database is one of the most useful and popular files for storing data; they can be used to store any kind of data, including text, numbers, images, binary data, files, etc. SQLite is a relational database management system based on the SQL language. It is a C library, and it provides an API to work with other programming languages, including Python. It does not require a separate server process to be run as needed in large database engines like MySQL and Postgresql.

5 Ways to Check the Linux Version

When most people talk of Linux, they are always referring to a Linux distribution. However, this is not the case. Linux itself is a kernel which acts as a bridge between user applications and the hardware. When we talk of a Linux distribution, we refer to an operating system developed from the Linux kernel. A distribution comes with a package manager, pre-installed applications, a Desktop Environment, and several more features.

Getting Started with Linux Operating System

The Linux operating system brings forth a vibrant mix of features and security, making it the best alternative to macOS or Windows operating systems. In this post, we will give you a master guide on Getting started with Linux systems - taking you from a complete beginner to a level where you can begin testing the various Linux distributions available with much ease.

How to Create a Comprehensive Mail Server on Ubuntu

Postal is a free and open-source mail server used to send and receive emails. It comes loaded with tons of excellent features and functionalities, making it extremely popular among large organizations as well as in enterprise settings.

The 10 Best Linux Performance Monitoring Tools

Do you want to monitor the performance of your Linux system? Are you looking for some powerful performance monitoring tools to help you out? If you agree, it's your day as we have put together a detailed list of the ten best Linux performance monitoring tools.

How to Boot your Windows or Linux PC from a USB Drive

Sometime back, the process of installing an operating system required users to pop a bootable media disk into their DVD or CD drive and use it to boot the PC. But times have changed. Nowadays, the most common way of installing an OS is booting from a USB drive. The use of USB drives is further propelled by the current production of slim and lightweight laptops with no support for DVD/CD drives.

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

10 Best PDF Editors for Linux

In this article, we will take a look at 10 of the best PDF editors and tools out there in 2019 that are available for Linux platforms. The editors are going to be judged on the basis of their functionalities, portability, ease of installation, price, and convenience.

Enabling GameMode on Linux for best gaming performance

GameMode is a combination of various libraries and daemons that allows all the users to improve the gaming performance on the Linux system. Developed by games publisher Feral Interactive, it improves gaming performance by requesting a group of options that will be applied temporarily to the Linux system.

6 Essential Command-Line Utilities Every Linux User Should Know

Last week, we shared with you several "cool and fun" commands to get comfortable and confident with the Linux command-line. In our quest to further aid Linux users with mastery of the command line, or CLI, we present you with a variety of command-line utilities essential for all Linux users, regardless of proficiency level.

Linux Mint vs. Ubuntu – which is best for you?

The conundrum is that which one among them? The best solution is to try both of them out, and then choose whatever seems to work the best for your needs. But here, we're giving you some distinctions between the both that might make it easier for you to make a choice.