Introduction to using cgroups to control memory resources in Linux

Source: Internet
Author: User
This article mainly introduces how to use cgroups to control memory resources in Linux. For more information, see

Cgroups has a memory subsystem used to restrict and report the memory usage of processes.

Obviously, there are two groups of corresponding files, one with memsw and the other

Copy codeThe code is as follows:
Memory. failcnt
Memory. limit_in_bytes
Memory. max_usage_in_bytes
Memory. usage_in_bytes

Memory. memsw. failcnt
Memory. memsw. limit_in_bytes
Memory. memsw. max_usage_in_bytes
Memory. memsw. usage_in_bytes
Memsw indicates virtual memory, that is, physical memory plus swap zone. The group without memsw only includes physical memory. Here, limit_in_bytes is used to limit memory usage, while others are statistical reports.

Copy codeThe code is as follows:
# Echo 10485760>/sys/fs/cgroup/memory/foo/memory. limit_in_bytes

The total physical memory used by processes in this group cannot exceed 10 MB. For memory. memsw. limit_in_bytes, the virtual memory usage is restricted. Memory. memsw. limit_in_bytes must be greater than or equal to memory. limit_in_byte. These values can also be set in a more convenient 100 M, 20G format. To lift the limit, set this value to-1.

This method limits the memory usage of processes. When the memory occupied by a process exceeds the limit, page loss occurs when accessing the memory, and there are not enough non-active memory pages to be swapped out, oom is triggered, resulting in the process being killed directly, resulting in availability problems. Even if the oom killer in the control group is disabled, the process will not be killed when the memory is insufficient, but it will enter the D state for a long time (wait for the system call to be interrupted and sleep) and cannot continue execution, as a result, the service is still unavailable. Therefore, I believe that using memory. limit_in_bytes or memory. memsw. limit_in_bytes to limit the memory usage of a process should only be used as an insurance to avoid exhausting system resources when the process is abnormal. For example, if a group of processes is expected to consume at most 1 GB of memory, you can set it to 1.4 GB. This can avoid more serious problems in the case of memory leaks and other exceptions.

In the memory subsystem, there is also a memory. soft_limit_in_bytes. And memory. the difference between limit_in_bytes is that this limit does not prevent the process from using the memory that exceeds the limit, but when the system memory is insufficient, the memory occupied by the process that exceeds the limit will be preferentially reclaimed to bring it closer to the limit value.

Previously, oom killer in the control group can be disabled, which is implemented through memory. oom_control. Cat memory. oom_control: You can view the current settings and whether or not oom is triggered. Echo 1> memory. oom_control can disable oom killer.

Usage_in_bytes, max_usage_in_bytes, and failcnt correspond to the current usage, maximum usage, and the number of page defects.

Another important setting in the memory subsystem is memory. use_hierarchy, which is a Boolean switch and defaults to 0. In this case, resource restrictions and usage values at different levels are independent. When it is set to 1, the memory usage of the sub-control group process is also included in the parent control group and traced back to all the ancestor control groups of memory. use_hierarchy = 1. In this way, the resource usage of all child control group processes cannot exceed the resource limit set by the parent control group. At the same time, when the memory usage of the process in the entire tree reaches this limit, memory recycling will also affect the processes of all child control groups. This value can be set only when no sub-control group is available. Then, the default memory. use_hierarchy of the created sub-control group inherits the settings of the parent control group.

Memory. swappiness controls the kernel's tendency to use swap areas. The value range is 0-100. The smaller the value, the more likely it is to use physical memory. When it is set to 0, the swap zone is used only when the physical memory is insufficient. The default value is system Global Settings:/proc/sys/vm/swappiness.

Memory. stat is the memory usage report. This includes the total current resources, usage, page feed times, and number of active pages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.