How To Check Memory Usage
Running the command "htop" will give you the usage of your cores, and will provide a little bit of overview for how much memory is being used. You will see "memory" and also "swap":
You also are provided information as to how long your server has been up (Uptime) and what the current load average is. But we are focused on the memory for this section.
In the example above, we can see that this particular server has 31.2GB of Memory, and 2GB of Swap.
A quick online definition for what Swap is (from https://www.geeksforgeeks.org/swap-space-in-operating-system/#):
A computer has a sufficient amount of physical memory but most of the time we need more so we swap some memory on disk. Swap space is a space on a hard disk that is a substitute for physical memory. It is used as virtual memory which contains process memory images. Whenever our computer runs short of physical memory it uses its virtual memory and stores information in memory on disk. Swap space helps the computer’s operating system in pretending that it has more RAM than it actually has. It is also called a swap file. This interchange of data between virtual memory and real memory is called swapping and space on disk as “swap space”.
From that same article, these are some benefits of swap memory:
- It can be used as a single contiguous memory which reduces I/O operations to read or write a file.
- Applications that are not used or are used less can be kept in a swap file.
- Having sufficient swap files helps the system keep some physical memory free all the time.
- The space in physical memory which has been freed due to swap space can be used by OS for some other important tasks.
The main focus here is that if your memory, and/or swap memory, is full (or close to full), this can cause performance issues as there will be more I/O (input/output) processes as well as new information not being able to be written if the memory is full.
A more in depth way to check memory is to run:
This will provide memory information in a format that is readable in "megabytes" (rather than kilobytes):
We can see that the swap memory is a bit used up here and it would benefit us to go ahead and clear that out. (This will not be the case for every situation)
To purge the memory, and provide more available memory to the server and its processes, we can run:
sudo sync && echo 1 | sudo tee /proc/sys/vm/drop_caches***
**This will clear out the used memory, however will not affect the swap memory**
To clear out the swap memory, there are a couple of things you can do. You can do a full server restart, this will purge out the swap memory.
Or, if you would like to keep the server running, you can disable swap, wait about 30 seconds, and enable swap again:
sudo swapoff -a
(once completed, wait about 30 seconds, and reenable)
sudo swapon -a
**Depending on how much swap is used, the "swapoff -a" command can take a few minutes to complete**
You can run the "free -m" command after its disabled to verify that swap now shows "0", and you can run this again once re-enabled to be sure swap is being used again.