Yes, depending on what you mean by "allocated". You can reduce or increase the amount of physical memory that the process can use.
In-use memory consists of, among other things:
- Images from disk which are mapped to the process (e.g. shared libraries, executables)
- Pages from disk which are cached by the IO cache layer
- Anonymous memory which may or may not be backed by swap.
When reducing the cgroup memory.limit_in_bytes
the system will discard pages from disk cache and from disk images (e.g. executables), as these can always be reloaded if needed.
If you have swap enabled, it can also page out anonymous memory.
Create a cgroup for your process and set the limit
# Create a cgroup
mkdir /sys/fs/cgroup/memory/my_cgroup
# Add the process to it
echo $PID > /sys/fs/cgroup/memory/my_cgroup/cgroup.procs
Set the limit to 40MB
echo $((40 * 1024 * 1024)) > /sys/fs/cgroup/memory/my_cgroup/memory.limit_in_bytes
The system will immediately swap things out until it is under the limit, and it will keep doing this to keep the process under the limit.
If it is not possible for the kernel to put the process or group under the limit initially (because certain memory cannot be swapped e.g. if you don't have any swap, or certain kernel pages which might not be swappable), an error will occur, and the limit will not be changed.
If it is not possible to do this at some point in the future, the OOM killer will kill the process (or one of its subprocesses).
You can use the oom_control
key to instead suspend the processes in the group, but then you will have to resolve the issue yourself, either by raising the limit or killing one or more processes.
More information
There is also a memory.soft_limit_in_bytes
key. This does not invoke the oom killer, rather it is used when the system is under memory pressure to determine which processes get swapped out first (and possibly at other times).
From within the process or group
Processes can be made aware of memory limits by coding them to query the current usage, and the hard and soft limits. They can then take action to avoid breaching the limits, for example by refraining: from allocating memory, creating child processes or accepting new connection; or by reducing usage by e.g. terminating existing connections.
Processes can subscribe to notifications using the cgroups notification API.
See also
Kernel documentation at:
"cgexec -g
, isn't it? Can you clarify what you mean by that? – Bouncner Jul 06 '21 at 08:39