In Linux there are certain configurations that will help the system and in general the performance to be as complete and optimal as possible, one of the best known configurations (and that we can configure from the installation) is the swap memory or swap memory. You have to know how to monitor Linux memory to be able to manage hardware and logical resources well to prevent our system from collapsing..
The exchange memory (swap), comes into action at the moment in which the amount of physical memory (RAM) has reached its limit, with it, in case Linux requires memory resources and as such the RAM memory is full, all inactive pages in memory will be moved to the swap space, this swap memory is available on hard drives since it has a slower access time than directly in the physical memory of the equipment.
Swap memory configuration
It should be noted that this swap memory can be configured in three ways, which are:
- As a dedicated swap partition (recommended)
- As a combination of swap partitions and swap files
To take into account that Btrfs does not allow swap space..
RAM for swap memory
TechnoWikis will advise you the space to use for the swap memory based on the physical RAM, these values are:
- Less than 2 GB 2 times the amount of RAM is recommended
- Between 2 GB and 8 GB we can use the same amount for swap
- Between 8 GB and 64 GB it will be possible to use at least 4 GB
- Greater than 64 GB we will use a minimum of 4 GB
There are some commands in Linux that are ideal for seeing the space available for swap memory.
To stay up to date, remember to subscribe to our YouTube channel!
SUBSCRIBE ON YOUTUBE
1 See Memory Swap or using swapon
The swapon command is used to indicate which devices will use the paging and racking tasks, these calls to swapon are executed in the system startup file /etc/rc, this command will give us the opportunity to see the swap space and if we want to see all the devices marked as swap in the /etc/fstab file we will use the -all parameter..
Step 1
To do this we open the terminal and execute:
swapon-all
Step 2
Then we will see the details by entering the --summary parameter. There we see the file, type, size, used space and priority level.
swapon-summary
2 View Swap Memory or using /proc/swaps
The /proc file system in Linux has been implemented as a virtual file system where we will see details about system information at run time about things like system memory, mounted devices, hardware configuration, etc.
Step 1
For its use we open the terminal and execute:
cat /proc/swaps
Step 2
We see the already mentioned details of swap.
3 See Memory Swap or using Free
Free is a command with which it is possible to see the amount of free and used system memory, we can add the -h parameter so that the result is a readable format.
Step 1
In this case we open the terminal and execute:
free -h
Step 2
We can see the indicated details of total, used and free memory.
4 See Memory Swap or using Top
The top command allows us to view Linux processes by giving a real-time dynamic view of the running system, we normally use it for a list of processes or threads that are managed by the Linux Kernel.
Step 1
To use this command we open the terminal and execute:
top
Step 2
We can see a series of columns with details but in this case at the top you will find the swap details.
5 See Memory Swap or using Atop
The atop command is a full-screen ASCII performance monitor in which the activity of all the processes on the computer is recorded and reported, atop will be active in the background so that the results are always available, some of its advantages are:
- Shows the resource usage of all processes
- Highlight critical resources through colors (red)
- Monitors threads within processes
- Makes use of the netatop kernel module to monitor TCP, UDP, and network bandwidth
- Integrates disk I/O and network usage
Step 1
We open the terminal and execute:
atop
Step 2
If it is not, we execute:
sudo apt install atop
Step 3
Enter the password and wait for it to be installed. We run "atop" again to see the swap memory at the top:
Note
we must look at the SWP line associated with the swap memory.
6 See Memory Swap or using Htop
With the htop command we will have the possibility to see processes in an interactive mode as well as see information about the use of memory, for its use we open the terminal and execute:
htop
At the top you will see the swap details.
7 See Memory Swap or using Glances
Glances has been developed as a system monitoring utility where we will have access to information about running processes, CPU load, storage space usage, memory usage and many more details, we access the terminal and we execute:
glances
We will see the swap details in the respective line indicating its total, used and free.
8 See Memory Swap or from using Vmstat
Finally, we can make use of the vmstat command which generates information about virtual memory statistics, running processes, memory usage and much more, if this command is not available (in most cases it comes by default).
Step 1
You can install it with the following commands:
sudo apt install vmstat (Debian, Ubuntu and Mint) sudo yum install vmstat (RHEL/CentOS/Fedora)
Step 2
Now run:
vmstat
Step 3
We will see the swap statistics:
So each of these commands is ideal for knowing everything related to the swap space in Linux.