If you want to run some tests on a server with 32GB of RAM, but you only have access to servers with 64GB of RAM, you might wonder if there is a way to limit the amount of RAM that Red Hat Enterprise Linux (RHEL) 8 can use. This can be useful for simulating the production environment or testing the performance of your applications under different memory constraints.
In this blog post, we will show you how to limit the RAM available to RHEL8 by using a kernel parameter. We will also explain why this method is preferable to other alternatives, such as using cgroups or virtual machines. Finally, we will answer some frequently asked questions about this topic.
Table of Contents
What is a kernel parameter?
A kernel parameter is a setting that affects how the Linux kernel operates. The kernel is the core component of the operating system that manages the hardware resources and provides basic services to the user space applications. You can pass kernel parameters to the kernel at boot time, either manually or automatically, by editing the bootloader configuration file.
Some kernel parameters are built-in, meaning they are part of the kernel code and cannot be changed without recompiling the kernel. Others are module parameters, meaning they are associated with loadable kernel modules that can be inserted or removed dynamically. You can view the current kernel parameters by running the command cat /proc/cmdline or sysctl -a.
How to limit the RAM available to RHEL8 using a kernel parameter
The kernel parameter that we will use to limit the RAM available to RHEL8 is mem. This parameter allows you to specify the amount of memory that the kernel can use when it is not able to see the whole system memory or for testing purposes. The syntax of this parameter is mem=nn[KMG], where nn is a number and KMG are optional suffixes for kilobytes, megabytes, and gigabytes.
For example, if you want to limit the RAM available to RHEL8 to 32GB, you can use the parameter mem=32G. To pass this parameter to the kernel at boot time, you need to edit the /etc/default/grub file and append it to the GRUB_CMDLINE_LINUX variable. For example:
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet mem=32G"
Then, you need to regenerate the grub configuration file by running the command grub2-mkconfig -o /boot/grub2/grub.cfg. Finally, you need to reboot your system for the changes to take effect. You can verify that the kernel parameter has been applied by running the command free -h or cat /proc/meminfo and checking the total amount of memory.
Why use a kernel parameter instead of other methods?
There are other methods that you can use to limit the RAM available to RHEL8, such as using cgroups or virtual machines. However, these methods have some drawbacks compared to using a kernel parameter.
Cgroups are a feature of the Linux kernel that allow you to group processes and assign them resource limits, such as CPU, memory, disk I/O, and network bandwidth. You can use cgroups to create a memory cgroup and set a limit for it. Then, you can move your test processes into that cgroup and run them under that limit. However, this method only affects the processes that belong to that cgroup. The rest of the system will still see and use the full amount of RAM. Moreover, cgroups add some overhead and complexity to your system administration.
Virtual machines are software emulations of physical machines that run on top of a hypervisor, such as KVM or VMware. You can use virtual machines to create a virtual server with 32GB of RAM and install RHEL8 on it. Then, you can run your tests inside that virtual server. However, this method requires you to allocate disk space and CPU resources for the virtual machine. It also adds another layer of abstraction and performance degradation between your test processes and the hardware.
Using a kernel parameter is simpler and more effective than using cgroups or virtual machines. It directly tells the kernel how much memory it can use, regardless of how much physical memory is installed on your system. It also does not require any additional software or configuration. It is a low-level and lightweight solution that gives you more control and accuracy over your testing environment.
How to limit the RAM available to RHEL8 using Cgroups
Have you ever needed to allocate a fixed amount of RAM for testing purposes on a server with more physical RAM than required? Red Hat Enterprise Linux (RHEL) makes it possible through control groups (cgroups), allowing you to restrict the memory usage of particular processes or groups of processes.
Here’s a step-by-step guide to help you control RAM usage for specific processes:
Step 1: Install and Enable cgroup-tools
First, make sure you have the cgroup-tools package installed. If not, you can install it with the following command:
sudo yum install libcgroup-tools
Step 2: Create a Memory Cgroup
Now, create a new cgroup specifically for your test processes. You can name it whatever you like by replacing ‘/mytestgroup’ with your preferred name:
sudo cgcreate -g memory:/mytestgroup
Step 3: Set Memory Limits
Define the memory limit for the cgroup. For example, if you want to allocate 16GB of RAM, use the following command and adjust the limit as per your requirements:
sudo cgset -r memory.limit_in_bytes=16G mytestgroup
Step 4: Move Your Processes to the Cgroup
To limit the RAM usage of specific processes, you need to move them to the cgroup. This can be achieved by specifying the cgroup when starting a process or by using the ‘cgexec’ command. Here’s an example of how to run a command within the ‘mytestgroup’ cgroup:
sudo cgexec -g memory:mytestgroup your_test_command
Replace ‘your_test_command’ with the actual command you want to run for your tests.
Step 5: Monitor Memory Usage
To keep an eye on the memory usage within the cgroup and its associated processes, you can use the ‘cgget’ command:
sudo cgget -g memory:mytestgroup
This command provides information about memory usage, including the set limit and the current consumption.
Step 6: Cleanup
Once your testing is complete, you can remove the cgroup to release the allocated memory limits:
sudo cgdelete memory:mytestgroup
Following these steps enables you to efficiently manage the RAM usage of specific processes or process groups on a server with excess physical RAM, allowing you to control resource allocation for your tests without requiring hardware changes.
Frequently Asked Questions
Question: Can I use any value for the mem parameter?
Answer: No, you cannot use any value for the mem parameter. The value must be less than or equal to the amount of physical memory installed on your system. Otherwise, you will get an error message like “Memory too large” or “Bad mem option”. You also cannot use a value that is too small, as it may cause your system to crash or malfunction. The minimum value depends on your system configuration and workload, but it is generally recommended to use at least 1GB of RAM for RHEL8.
Question: Can I use the mem parameter with other kernel parameters?
Answer: Yes, you can use the mem parameter with other kernel parameters, as long as they are compatible and do not conflict with each other. For example, you can use the mem parameter with the memmap parameter to avoid physical address space collisions. The memmap parameter allows you to reserve or remove specific regions of memory from the kernel’s allocation pool. For example, if you want to limit the RAM available to RHEL8 to 32GB and reserve 4GB of memory for a PCI device, you can use the parameters mem=32G memmap=4G$32G.
Question: How can I undo the changes made by the mem parameter?
Answer: If you want to undo the changes made by the mem parameter and restore the full amount of RAM available to RHEL8, you need to remove the parameter from the /etc/default/grub file and regenerate the grub configuration file. For example:
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet"
grub2-mkconfig -o /boot/grub2/grub.cfg
Then, you need to reboot your system for the changes to take effect.
Conclusion
In this blog post, we have shown you how to limit the RAM available to RHEL8 by using a kernel parameter. This can be useful for testing purposes or simulating different memory scenarios. We have also explained why this method is preferable to other alternatives, such as using cgroups or virtual machines. Finally, we have answered some frequently asked questions about this topic.
We hope you have found this blog post helpful and informative. If you have any questions or feedback, please leave a comment below. Thank you for reading!