Does Kubernetes PODs provide memory back, after acquiring more than the requested amount

8/23/2021

I am trying to understand the behavior of K8S POD memory allocation and so far no luck on the materials I read on the internet.

My question is, If I have a POD template defined with the below values for the memory

Limits:
  cpu:     2
  memory:  8Gi
Requests:
  cpu:     500m
  memory:  2Gi

And say my application suddenly requires more memory and the POD allocates 4Gi ( from 2Gi initial memory ) to get the task done. Would the POD give back the extra 2Gi it acquired back to the underlying OS and become a 2Gi POD again after the task is complete or would it function as a POD with 4Gi memory afterward.

My application is a Java application running on Apache Tomcat having the max heap defined for 6Gi.

-- Klaus
kubernetes
kubernetes-pod

1 Answer

8/23/2021

The Kubernetes resource requests come into effect at basically three times:

  1. When new pods are being initially scheduled, the resource requests (only) are used to find a node with enough space. The sum of requests must be less than the physical size of the node. Limits and actual utilization aren't considered.

  2. If the process allocates memory, and this would bring its total utilization above the pod's limit, the allocation will fail.

  3. If the node runs out of memory, Kubernetes will look through the pods on that node and evict the pods whose actual usage most exceeds their requests.

Say you have a node with 16 GiB of memory. You run this specific pod in a Deployment with replicas: 8; they would all fit on the node, and for the sake of argument let's say Kubernetes puts them all there. Regardless of what the pods are doing, a 9th pod wouldn't fit on the node because the memory requests would exceed the physical memory.

If your pod goes ahead and allocates a total of 4 GiB of memory, that's fine so long as the physical system has the memory for it. If the node runs out of memory, though, Kubernetes will see this pod has used 2 GiB more than its request; that could result in the pod getting evicted (destroyed and recreated, probably on a different node).

If the process did return the memory back to the OS, that would show up in the "actual utilization" part of the metric; since its usage would now be less than its requests, it would be in less danger of getting evicted if the node did run out of memory. (Many garbage-collected systems will hold on to OS memory as long as they can and reuse it, though; see e.g. Does GC release back memory to OS?.)

-- David Maze
Source: StackOverflow