Sometimes for one reason or another it is necessary to limit the size of usable memory on Linux. Although not very frequent, it can be useful in complex environments. Let’s start.
Although it is not frequent to find ourselves in this situation, it is true that in complex environments where there are servers with many gigabytes or terabytes of memory, it can be useful to limit the resources they can use.
In this case, the process is much simpler than it seems.
Limit the size of usable memory on Linux
Usually, you already know how much memory you have on your system, but in case you don’t, there are many monitoring tools, but a quick and immediate way is with this command
cat /proc/meminfo | grep MemTotal
You will get an output expressed in KB. For example:
MemTotal: 7780876 kB
In this case, it is a system with only 8Gb.
The magic is done by the mem
directive that you have to add to the end of the GRUB_CMDLINE_LINUX
line of the /etc/default/grub
file
sudo vi /etc/default/grub
It will look like this
GRUB_CMDLINE_LINUX="mem=4G"
Of course, on a more real server, you will find more options for this.
Save the changes and to apply them then you have to generate a new configuration file for Grub
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Finally, reboot the system
sudo reboot
This will suffice
Conclusion
I hope this post will be useful to you at some point, and you will be able to take advantage of it in your work.