Memory usage commands on Linux

If you have a website for your business which is accessed by multiple users and your sales depend on it, its very crucial to keep it running healthy always. Its just like doing regular medical checkups.

In the Linux operating system it is important to have enough free physical memory (RAM) on the market for your processes which require to run. If this memory runs out, swap file can begin to be used next, which will result in slower performance as I/O operations will need to be written to disk instead. If both main memory and swap become full your Linux system can fully freeze up.

Below commands can give you an idea of memory usage and overall load on server. Higher load means site is loading slow for end users in most of the cases.

1. top

The top command is a well known way of getting a fast overview of resource usage in Linux dynamically in real time and the top command is generally used to check memory and cpu usage per process. However it also reports total memory usage and can be used to monitor the total RAM usage. The header on output has the required information. Here is a sample output

top

Check the KiB Mem and KiB Swap lines on the header. They indicate total, used and free amounts of the memory. The buffer and cache information is present here too, like the free command.

2. htop

Similar to the top command, the htop command also shows memory usage along with various other details.
The header on top shows cpu usage along with RAM and swap usage with the corresponding figures.

3. df -h

df shows the amount of free space left on a file system. Space can have the following values:
Space Used
Total amount of space allocated to existing files in the file system.
Space Free
Total amount of space available in file system for the creation of new files by unprivileged users.
Space Reserved
Space reserved by the system which is not normally available to a user.
Total Space
Includes space used, space free, and space reserved.

df -h

df measures space in units of 512-byte disk sectors. You can specify a particular file system by naming any file name on that file system. If you do not give an argument, df reports space for all mounted file systems known to the system, in the following format:

  • File system root
  • File system name
  • Space available and total space

4. free -m

The free command is the most simple and easy to use command to check memory usage on linux. Here is a quick example

free -m

The m option displays all data in MBs. The total os 7976 MB is the total amount of RAM installed on the system, that is 8GB. The used column shows the amount of RAM that has been used by linux, in this case around 6.4 GB. The output is pretty self explanatory. The catch over here is the cached and buffers column. The second line tells that 4.6 GB is free. This is the free memory in first line added with the buffers and cached amount of memory.
Linux has the habit of caching lots of things for faster performance, so that memory can be freed and used if needed.
The last line is the swap memory, which in this case is lying entirely free.

5. du

The Linux operating system “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of disk usage of files and directories on a machine. The du command has several parameter choices that may be used to get the results in many formats. The du command also displays the files and directory sizes in a very recursively manner.

du -a -h

  1. If we want to print sizes in human readable format(K, M, G), use -h option  – e.g. : du -h /home/akash/test
  2. Use -a option for printing all files including directories. – e.g. du -a -h /home/akash/test
  3. Use -c option to print total size – e.g. du -c -h /home/akash/test
  4. To print sizes till particular level, use -d option with level no. – e.g. du -d 1 /home/mandeep/test
  5. Get the timestamp of last modified using –time option  – e.g. du –time -h /home/akash/test

6. cat /proc/meminfo

The next way to check memory usage is to read the /proc/meminfo file. Know that the /proc file system does not contain real files. They are rather virtual files that contain dynamic information about the kernel and the system.

cat -proc-meminfo

Check the values of MemTotal, MemFree, Buffers, Cached, SwapTotal, SwapFree.
They indicate same values of memory usage as the free command.

7. pmap

Pmap provide memory map of a process, The pmap command display the memory usage map of a process or multiple processes. Pmap reports information about the address space or memory usage map of a process. Pmap is actually a Sun OS command and Linux supports only very limited number of features. But it is very helpful for finding the complete address space of a process. To check memory usage of process we need PID or unique process ID of running process, we can get PID from /proc or regular commands like top or ps.

You can see more commands at our blog- Linux commands you should know about- Part 2.

 

Do you need to develop a web solution for your business on open source platform? Then you might need some help. We at Solace are known for building highly efficient linux solutions. Solace team is expert in Linux and is there to help you for development in linux. Contact us for any web development solution which lifts your business to the success it deserves.