Made by rpmn0ise https://rpmn0ise.neocities.org/

Memory Management: Cgroups v2 - Reference Documentation on Resource Isolation and Hierarchy


Introduction

Cgroups (Control Groups) v2 is a crucial feature in modern Linux systems for isolating, managing, and monitoring resources allocated to processes. This system allows for the allocation of CPU time, memory, disk I/O, and more, ensuring that processes within a cgroup do not interfere with each other, or with the overall system. The concept is deeply tied to resource management, which is essential for environments that need strict resource control, such as containers or high-performance systems.

This documentation provides an overview of memory management within Cgroups v2, focusing on the isolation and hierarchical nature of this feature. The goal is to provide readers with a solid understanding of how to utilize Cgroups v2 for efficient resource allocation and process isolation.


Table of Contents

  1. What is Cgroups v2?
  2. Memory Management in Cgroups v2
  3. Creating and Configuring Cgroups
  4. Memory Resource Control Parameters
  5. Hierarchical Structure
  6. Use Cases and Examples
  7. Common Issues and Troubleshooting

What is Cgroups v2?

Cgroups v2 is a Linux kernel feature introduced to provide resource management and isolation for groups of processes. Unlike its predecessor (Cgroups v1), v2 introduces a more unified and simplified interface to control and manage resources, with a focus on providing a consistent and scalable model for process isolation.

Cgroups allow system administrators and developers to:

  • Limit the amount of resources (e.g., CPU, memory) a group of processes can consume.
  • Isolate groups of processes to prevent interference or resource contention.
  • Monitor resource usage for specific groups of processes.

Cgroups v2 is implemented in a hierarchical manner, meaning that each cgroup can have child cgroups, with resource limits propagating down the hierarchy. This allows for fine-grained control of resources across multiple processes.


Memory Management in Cgroups v2

Memory management is one of the most important aspects of Cgroups v2, particularly in environments that require strict resource isolation, such as containers, virtual machines, or multi-tenant systems. Cgroups v2 offers tools to limit, monitor, and control the memory usage of processes in a cgroup.

Memory management in Cgroups v2 is achieved through several key parameters:

  1. Memory Limit (memory.max): This parameter sets the maximum amount of memory that processes in a cgroup can use. Once this limit is reached, the kernel will start to kill processes in the cgroup to prevent the system from running out of memory (OOM).
  2. Memory Soft Limit (memory.soft_limit): The soft limit is a hint to the kernel to keep processes within a certain memory range but allows processes to exceed this limit if the system has enough memory available. This is more of a preference than a hard limit.
  3. Memory Swap (memory.swap.max): This parameter controls the maximum amount of swap space that can be used by processes in the cgroup. Swap space is used when physical RAM is exhausted.
  4. Memory Kernel (memory.kmem.max): This defines the limit on memory usage by the kernel for processes within the cgroup.
  5. OOM Killer Behavior (memory.oom.group): This flag allows you to control whether the OOM killer will kill processes in the entire cgroup or only individual processes when memory limits are exceeded.
  6. Memory Usage Statistics: Cgroups v2 provides various statistics related to memory usage, including the current memory usage, kernel memory usage, and swap usage. These statistics can be accessed from files in the cgroup's memory controller directory.

Creating and Configuring Cgroups

Creating and configuring Cgroups v2 is done through the /sys/fs/cgroup/ filesystem. To get started, you must ensure that your system supports Cgroups v2 (most modern Linux distributions do). The following steps illustrate how to create a basic Cgroup and apply memory limits.

  1. Check Cgroups v2 Support

    To check if your system uses Cgroups v2, run the following command:

    ls /sys/fs/cgroup/
    

    If you see a directory named unified, your system is using Cgroups v2.

  2. Create a Cgroup Directory

    To create a new cgroup, navigate to the Cgroups v2 directory and create a new directory:

    sudo mkdir /sys/fs/cgroup/my_cgroup
    
  3. Set Memory Limits

    To set a memory limit for this cgroup, write the desired value to the memory.max file:

    echo "500M" | sudo tee /sys/fs/cgroup/my_cgroup/memory.max
    
  4. Add Processes to the Cgroup

    To add a process to the cgroup, write the process ID (PID) to the cgroup.procs file:

    echo "1234" | sudo tee /sys/fs/cgroup/my_cgroup/cgroup.procs
    

    Replace 1234 with the actual PID of the process you want to add.


Memory Resource Control Parameters

The following table outlines the key memory-related control parameters available in Cgroups v2 for fine-tuning resource allocation:

Parameter Description
memory.max The maximum amount of memory that the cgroup can use.
memory.soft_limit A soft limit to guide the kernel's memory allocation but not enforce it strictly.
memory.swap.max The maximum amount of swap space that the cgroup can use.
memory.kmem.max The maximum amount of memory used by the kernel within the cgroup.
memory.oom.group Controls whether the OOM killer targets the entire cgroup or individual processes.
memory.stat Provides a snapshot of memory usage statistics for the cgroup.

These parameters allow for a variety of configurations, enabling system administrators to fine-tune the memory behavior of processes under the cgroup’s control.


Hierarchical Structure

Cgroups v2 introduces a hierarchical structure, allowing resources to be allocated across different levels. This enables resource control to be applied to a group of processes, with subgroups inheriting the limits set by their parent cgroup, but also capable of having their own specific limits.

For instance, a cgroup tree might look like this:

1
2
3
4
5
6
7
8
/sys/fs/cgroup/
├── system.slice/
│   ├── user.slice/
│   │   ├── cgroup1/
│   │   └── cgroup2/
│   ├── docker.slice/
│   └── ...
└── my_custom_cgroup/

In this example, my_custom_cgroup could have its own memory limit set, while inheriting from its parent cgroup (system.slice), which may also have its own settings.


Use Cases and Examples

  1. Containerized Environments:
    Cgroups v2 is heavily used in container technologies like Docker and Kubernetes. These systems rely on Cgroups for memory, CPU, and I/O isolation. You can use Cgroups v2 to restrict the amount of memory allocated to a container, ensuring that no container exceeds a defined memory limit.
  2. High-Performance Computing:
    In HPC (High-Performance Computing), Cgroups v2 can be used to ensure that different tasks or workloads receive the appropriate share of memory, avoiding resource starvation.
  3. Multitenancy:
    For systems hosting multiple users or processes, Cgroups v2 can prevent any single process or user from consuming all available memory, which could otherwise destabilize the system.

Common Issues and Troubleshooting

  • Out of Memory (OOM) Killer:
    When memory limits are exceeded, the kernel may invoke the OOM killer to terminate processes. Ensure that the memory.oom.group setting is configured correctly to avoid killing critical processes across the cgroup.
  • Inconsistent Memory Usage:
    If a cgroup consistently exceeds memory limits, monitor individual processes within the cgroup to identify which processes are using more memory than expected.
  • Hierarchical Inheritance:
    Cgroups v2 allows resources to be inherited from parent cgroups. Ensure that the correct inheritance model is in place, or child cgroups might end up with unexpected limits.

Conclusion

Cgroups v2 offers powerful and flexible tools for managing memory and other resources in a Linux-based system. Its hierarchical structure provides an efficient way to allocate and isolate resources among processes, preventing resource contention and ensuring stability. Understanding the various memory parameters and how to configure them can help achieve fine-grained control over system resources, which is crucial in environments like containers, cloud computing, and performance-critical applications.

Made by rpmn0ise https://rpmn0ise.neocities.org/

Edit

Pub: 26 Jan 2026 08:33 UTC

Views: 3