Understanding how GNU/Linux manages memory is not quite that black and white, but it's really straightforward if you can get the concept.
free command is one of the useful utility for monitoring memory usage on our server, it displays the amount of free and used memory in the system.
Let's See Some Variation of The Command:
Displaying Total Memory, and Its Usage
user@server:~$ free total used free shared buff/cache available Mem: 1008724 695164 89696 131036 223864 64788 Swap: 0 0 0
The free command in the above result is shown in kilobytes.
Displaying Total Memory, and Its Usage in MB (Megabyte)
user@server:~$ free -m total used free shared buff/cache available Mem: 985 683 61 127 241 47 Swap: 0 0 0
This is much more readable, so, let's interpret the results of the above command (I would only be explaining the major ones):
- The total shows the total installed memory on your server, in my case, it is 985mb.
- The used show how much of that memory is currently used (by anything)
- The free column shows how much of memory that is completely free (not used at all, cache or otherwise)
- The available shows how much memory is free for your application to use, the thing is, this memory is actually be used in the form of a cache but would be released in the event that another application needs it.
Understanding of all the data in a table below:
|total||The total shows the total installed memory on your server|
|used||The used show how much of that memory is currently used (by anything)|
|free||The free column shows how much of memory that is completely free (not used at all, cache or otherwise)|
|shared||Memory used (mostly) by tmpfs (Shmem in /proc/meminfo|
|buff/cache||Sum of buffers and cache|
|available||Estimation of how much memory is available for starting new applications, without swapping.|
You might wonder what the Swap does, in the event that the memory gets full, it would utilize the swap if you created one during installation.
To control at which point your server will begin utilizing Swap, which is referred to as its swappiness, you change it in the following file
It's by default set to 60, you can verify by running the following command
The higher the swappiness value, the more likely your server will utilize swap, say you set its value to 100, it will use swap as much as possible. If it is set to 0, it won't use it at all.
To set the swappiness value, you can execute the following command:
sudo sysctl vm.swappiness=25
The swap will be used when RAM becomes 85 percent full, but the above command won't set the value permanently, it would revert back to the default once you reboot the system.
To make the change permanent, you need to open the sysctl.conf file as follows:
sudo nano /etc/sysctl.conf
By default the swappiness line won't be include, so, you need to add it manually, I'll add it to the end of the file:
vm.swappiness = 25
Utilizing swappiness is one of the useful nugget when performing tuning, there might be a situation where you don't want your application to swap more. Try out different values, and see if it works for you.