<p><strong>What happened</strong>:
I've configured a hpa with these details:</p>
<pre><code>apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: api-horizontalautoscaler
  namespace: develop
spec:
  scaleTargetRef:
    apiVersion: extensions/v1beta1
    kind: Deployment
    name: api-deployment
  minReplicas: 1
  maxReplicas: 4
  metrics:
  - type: Resource
    resource:

      name: memory
      targetAverageValue: 400Mib
</code></pre>
<p><strong>What I expected to happen</strong>:
The pods scaled up to 3 when we put some load and the average memory exceeded 400 which was expected. Now the average memory has gone back down to roughly 300 and still the pods haven't scaled down even though they have been below the target for a couple of hours now.
<img src="https://user-images.githubusercontent.com/15933995/97123211-95885980-1722-11eb-9b70-b47b1838372d.png" alt="image" /></p>
<p>A day later:
<img src="https://user-images.githubusercontent.com/15933995/97123226-a8029300-1722-11eb-9a15-53a3d645465d.png" alt="image" /></p>
<p>I expected the pods to scale down when the memory fell below 400</p>
<p><strong>Environment</strong>:</p>
<ul>
<li>Kubernetes version (using <code>kubectl version</code>):</li>
</ul>
<pre><code>Client Version: version.Info{Major:&quot;1&quot;, Minor:&quot;13&quot;, GitVersion:&quot;v1.13.9&quot;, GitCommit:&quot;3e4f6a92de5f259ef313ad876bb008897f6a98f0&quot;, GitTreeState:&quot;clean&quot;, BuildDate:&quot;2019-08-05T09:22:00Z&quot;, GoVersion:&quot;go1.11.5&quot;, Compiler:&quot;gc&quot;, Platform:&quot;linux/amd64&quot;}
Server Version: version.Info{Major:&quot;1&quot;, Minor:&quot;13&quot;, GitVersion:&quot;v1.13.10&quot;, GitCommit:&quot;37d169313237cb4ceb2cc4bef300f2ae3053c1a2&quot;, GitTreeState:&quot;clean&quot;, BuildDate:&quot;2019-08-19T10:44:49Z&quot;, GoVersion:&quot;go1.11.13&quot;, Compiler:&quot;gc&quot;, Platform:&quot;linux/amd64&quot;}re configuration:
</code></pre>
<ul>
<li>OS (e.g: <code>cat /etc/os-release</code>):</li>
</ul>
<pre><code>&gt; cat /etc/os-release
NAME=&quot;Ubuntu&quot;
VERSION=&quot;18.04.3 LTS (Bionic Beaver)&quot;
</code></pre>
<ul>
<li>Kernel (e.g. <code>uname -a</code>):
x86_64 x86_64 x86_64 GNU/Linux</li>
</ul>
<p>I would really like to know why this is. Any information needed I will be happy to provide.</p>
<p>Thanks!</p>

Suemayah Eldursi
<p>The formula for how the HPA decides how many pods to run is in the <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#algorithm-details" rel="nofollow noreferrer">Horizontal Pod Autoscaler documentation</a>:</p>
<pre><code>desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]
</code></pre>
<p>With the numbers you give, <code>currentReplicas</code> is 3, <code>currentMetricValue</code> is 300 MiB, and <code>desiredMetricValue</code> is 400 MiB, so this reduces to</p>
<pre><code>desiredReplicas = ceil[3 * (300 / 400)]
desiredReplicas = ceil[3 * 0.75]
desiredReplicas = ceil[2.25]
desiredReplicas = 3
</code></pre>
<p>You need to decrease the load further (below 266 MiB average memory utilization) or increase the target memory utilization for this to scale down more.</p>
<p>(Simply being below the target won't trigger scale-down on its own, you must be enough below the target for this formula to produce a lower number.  This helps avoid thrashing if the load is right around a threshold that would trigger scaling in one direction or the other.)</p>
