Memory Management in Linux – How to Manage Linux Memory

Memory Management in Linux

In this article, we’ll cover the basic commands for memory management in Linux. Memory management is a vast topic and covering it in one blog post will not do it justice. In this tutorial, we will mainly go over the commands that you can use to observe your memory statistics.

Physical vs Virtual Memory in Linux

Before we get into the nitty-gritty, it’s important to know that there are two types of memories in Linux.

  • Physical Memory
  • Virtual Memory

Physical memory is the actual memory present in the machine. While Virtual memory is a layer of memory addresses that map to physical addresses.

Virtual memory is usually bigger than physical memory.

Linux kernel uses Virtual memory to allow programs to make a memory reservation.

While executing a program, the processor reads the instructions from the virtual memory. However, before executing the instructions, it converts the virtual addresses into physical addresses. Mapping information present in page tables is used for this job.

Commands for Memory Management in Linux

Let’s go over some of the commands for managing memory in Linux.

1. /proc/meminfo

The /proc/meminfo file contains all the information related to memory. To view this file use the cat command:

$ cat /proc/meminfo
Meminfo

This command outputs a lot of parameters related to memory. To get the physical memory from proc/meminfo file use:

$ grep MemTotal /proc/meminfo
Memtotal

To get the virtual memory from /proc/meminfo file use:

$ grep VmallocTotal /proc/meminfo
VmallocTotal

2. The top command

The top command lets you monitor processes and system resource usage on Linux. It gives a dynamic real-time view of the system. When you run the command, you’ll notice that the values in the output keep changing. This happens as it displays the values in real time.

$ top 
Top

The upper portion shows the current usage statistics of your system resources. The lower portion contains the information about the running processes. You can move up and down the list using the up/down arrow keys and use q to quit.

3. free command

The free command displays the amount of free and used memory in the system. It’s a simple and compact command to use. It tells you information such as how much free RAM you have on your system. It also tells you about the total amount of physical and swap memory on your system.

$ free
Free

 Values for each field are in Kibibyte (KiB). Kibibyte is not the same as Kilobyte. To get the output in a more human readable format use:

$ free -h
Free 0h

To know more about the free command, refer to our tutorial that discusses free command at length.

4. vmstat command

vmstat is a performance monitoring tool in Linux. It gives useful information about processes, memory, block IO, paging, disk, and CPU scheduling. It reports virtual memory statistics of your system.

$ vmstat 
Vmstat

To know more about vmstat command in Linux, refer to the tutorial on vmstat.

Conclusion

These were a few commands that you can use in Linux to manage your memory. These commands give key insights about your memory. You can play around with all the commands by following the individual tutorials to get an general idea of how you can play around with the processes in the task manager.