<p>I am now doing experiments on using Kubernetes and Docker to provisioning services such as WordPress, Spark and Storm on 10 physical machines</p>

<p>But after lots times of launching and terminating Docker Containers, the used memory would increase even I kill all the Containers via Kubernetes delete or Docker kill commands.</p>

<p>I noticed that there were lots of Containers with status Exited, and after I remove all the Exited Containers, it frees lots of memories.</p>

<p>And I came up with a solution, which is writing a removing exited containers schedule into cron table on each Docker host.</p>

<p>But is this appropriate? if not, how can I release the memories?</p>

<p>It is not recommended to use external container garbage collection scripts. Kubernetes relies on exited containers as tombstones to reconstruct the pod status and/or serve logs. Even if you don't care about container logs, if you remove the exited containers before kubernetes examines them and properly records the status, it may cause inaccurate status and restart decisions. This reliance may be eliminated <a href="https://github.com/kubernetes/kubernetes/issues/489" rel="nofollow">in the future</a>.</p>

<p>For now, the best way to achieve more aggressive container garbage collection is through adjusting the parameters, as detailed in this <a href="https://github.com/kubernetes/kubernetes/blob/master/docs/admin/garbage-collection.md" rel="nofollow">guide</a>.</p>

<p>FYI, there are also open issues to improve the garbage collection behavior. <a href="https://github.com/kubernetes/kubernetes/issues/13287" rel="nofollow">#13287</a> is one example.</p>

<p>If you <em>really</em> want to clean up the containers yourself, it is safe to remove containers associated with deleted pods. Removing multiple exited containers that belong to the same pod/container while keeping the most recent few exited containers is also relatively low-risk.</p>
