In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use cgroups to control memory resources in Linux. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
There is a memory subsystem in cgroups that limits and reports on the memory usage of processes.
Among them, there are obviously two sets of corresponding files, one with memsw and the other without.
The 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 represents virtual memory, that is, physical memory plus swap. The group without memsw includes only physical memory. Among them, limit_in_bytes is used to limit memory usage, and the others are statistical reports.
The code is as follows:
# echo 10485760 > / sys/fs/cgroup/memory/foo/memory.limit_in_bytes
You can limit the total amount of physical memory used by processes in this group to no more than 10MB. For memory.memsw.limit_in_bytes, it is to limit the use of virtual memory. 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 form such as 100m focus 20g. To lift the restriction, set this value to-1.
There is a risk in limiting the process memory footprint in this way. When the process tries to occupy more memory than the limit, a page fault occurs when accessing memory, and there are not enough inactive memory pages to swap out, oom will be triggered, resulting in the process being directly killed, resulting in usability problems. Even if the oom killer of the control group is turned off, the process will not be killed when it runs out of memory, but it will enter D (uninterruptible hibernation waiting for system calls) for a long time and cannot continue to execute, resulting in still no service. Therefore, I think that using memory.limit_in_bytes or memory.memsw.limit_in_bytes to limit the process memory footprint should only be used as an insurance to avoid exhausting system resources when the process is abnormal. For example, if a set of processes is expected to consume at most 1 gigabyte of memory, it can be set to 1.4 gigabytes. In this way, more serious problems can be avoided in the event of anomalies such as memory leaks.
In the memory subsystem, there is also a memory.soft_limit_in_bytes. Unlike memory.limit_in_bytes, this limit does not prevent processes from using more than the limit, but when the system runs out of memory, it gives priority to reclaiming the memory occupied by processes that exceed the limit to the limit.
As mentioned earlier, the oom killer of the control group can be turned off, which is realized through memory.oom_control. Cat memory.oom_control can see the current settings and whether oom is currently triggered. Oom killer can be disabled if echo 1 > memory.oom_control.
Usage_in_bytes, max_usage_in_bytes and failcnt correspond to the current usage, the maximum usage and the number of page faults, respectively.
Another important setting in the memory subsystem is memory.use_hierarchy, which is a Boolean switch, which defaults to 0. At this point, the resource limits and usage values between different levels are independent. When set to 1, the memory footprint of the child control group process is also counted in the parent control group and traced back to all ancestral control groups with memory.use_hierarchy = 1. In this way, the resource consumption of the processes of all descendant control groups cannot exceed the resource limit set by the parent control group. At the same time, when the memory footprint of the processes in the whole tree reaches this limit, memory reclamation will also affect the processes of all descendant control groups. This value can only be set if there is no child control group. The default memory.use_hierarchy of the new child control group then inherits the settings of the parent control group.
Memory.swappiness controls the kernel's tendency to use swap zones. The range of values is 0-100. The smaller the value, the more likely you are to use physical memory. When set to 0, the swap area is used only if there is insufficient physical memory. The default value is the system global setting: / proc/sys/vm/swappiness.
Memory.stat is the memory usage report. Including the total amount of current resources, usage, number of page changes, number of active pages, and so on.
This is the end of this article on "how to use cgroups to control memory resources in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.