<p>I have a kubernetes cluster with 16Gb RAM on each node</p>

<p>And a typical dotnet core webapi application</p>

<p>I tried to configure limits like here:</p>

<pre><code>apiVersion: v1
kind: LimitRange
metadata:
  name: mem-limit-range
spec:
  limits:
  - default:
      memory: 512Mi
    defaultRequest:
      memory: 256Mi
    type: Container
</code></pre>

<p>But my app believe that can use 16Gb</p>

<p>Because <code>cat /proc/meminfo | head -n 1</code> returns <code>MemTotal: 16635172 kB</code> (or may be something from cgroups, I'm not sure)</p>

<p>So.. may be limit does not work? </p>

<p>No! K8s successfully kills my pod when it's reaches memory limit</p>

<p>.net core have interesting mode of GC, <a href="https://blog.markvincze.com/troubleshooting-high-memory-usage-with-asp-net-core-on-kubernetes/" rel="noreferrer">more details here</a>. And it is a good mode, but looks like it's doesn't work solution for k8s, because application gets wrong info about memory. And unlimited pods could get all host memory. But with limits - they will die.</p>

<p>Now I see two ways:</p>

<ol>
<li>Use GC Workstation </li>
<li>Use limits and <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/#define-readiness-probes" rel="noreferrer">k8s readness probe</a>: handler will
checks current memory on each iteration and call GC.Collect() if
current used memory near 80% (I'll pass limit by env variable)</li>
</ol>

<p>How to limit memory size for .net core application in pod of kubernetes?</p>

<p>How to rightly set limits of memory for pods in kubernetes?</p>

<p>You should switch to Workstation GC for optimizing to lower memory usage. The readiness probe is not meant for checking memory </p>

<p>In order to properly configure the resource limits you should test your application on a single pod under heavy loads and monitor(e.g. Prometheus &amp; Grafana) the usage. For a more in-depth details see this <a href="https://opensource.com/article/18/12/optimizing-kubernetes-resource-allocation-production" rel="noreferrer">blog post</a>. If you haven't deployed a monitor stack you can at least use <code>kubectl top pods</code>.</p>

<p>If you have found out the breaking points of a single pod you can add the limits to the specific pod like this example below (see <a href="https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#resource-requests-and-limits-of-pod-and-container" rel="noreferrer">Kubernetes Documentation</a> for more examples and details)</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: exmple-pod
spec:
  containers:
  - name: net-core-app
    image: net-code-image
    resources:
      requests:
        memory: 64Mi
        cpu: 250m
      limits:
        memory: 128Mi
        cpu: 500m
</code></pre>

<p>The readiness probe is actually meant to be used to tell when a Pod is ready in first place. I guess you thought of the liveness probe but that wouldn't be adequate usage because Kubernetes will kill the Pod when it's exceeding it's resource limit and reschedule.</p>
