<p>I am using 'crictl' tool to work with containerd runtime containers (under kubernetes) in a managed cluster.</p>
<p>I'm trying to set the memory limit (in bytes) to 16MB with the command:
<code>crictl -r unix:///run/containerd/containerd.sock update --memory 16777216 c60df9ef3381e</code></p>
<p>And get the following error:</p>
<pre><code>E1219 11:10:11.616194    1241 remote_runtime.go:640] &quot;UpdateContainerResources from runtime service failed&quot; err=&lt;
    rpc error: code = Unknown desc = failed to update resources: failed to update resources: /usr/bin/runc did not terminate successfully: exit status 1: unable to set memory limit to 16777216 (current usage: 97058816, peak usage: 126517248)
    : unknown
 &gt; containerID=&quot;c60df9ef3381e&quot;
FATA[0000] updating container resources for &quot;c60df9ef3381e&quot;: rpc error: code = Unknown desc = failed to update resources: failed to update resources: /usr/bin/runc did not terminate successfully: exit status 1: unable to set memory limit to 16777216 (current usage: 97058816, peak usage: 126517248)
: unknown
</code></pre>
<p>At first I thought that maybe I cannot set a memory limit directly to a running container lower than the limit that appears in the kubernetes yaml.
Here Are the limits from K8s:
<code>Requests:{&quot;cpu&quot;:&quot;100m&quot;,&quot;memory&quot;:&quot;64Mi&quot;} Limits:{&quot;cpu&quot;:&quot;200m&quot;,&quot;memory&quot;:&quot;128Mi&quot;} </code>
But not, even setting a memory limit above the K8S request (e.g. 65MB) gives this same error!</p>
<p><strong>This works on Docker runtime</strong> - I'm able to limit the memory of the container. Yes, it might crash, but the operation works..</p>
<p>Then, I tried to give a memory limit higher than the current usage, and it succeeded...</p>
<p>Can anyone help understanding this error and what might be causing it on containerd runtime?? Is this indeed a limitation that I cannot limit to a lower memory currently used by the container? Is there a way to overcome that?</p>
<p>Thanks a lot for your time!!!</p>

<p>The error suggests that the container is already using more memory than the limit you're trying to impose.  If the limit were to be imposed in this case, then any further attempts to allocate memory would fail.  That would usually cause processes to abort, which is bad.  Also, the container would already be violating the limit you just set.</p>
<p>The limit isn't allowed to be imposed in this case because data loss is highly likely to occur, and the choice of which data to lose should not be left to chance.</p>
<p>I suggest if you want to lower the limit, you need to lower the current usage to below that limit before you do so, to retain control of what memory is freed.</p>
