Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clear cache memory inside docker container

I have a docker swarm CE setup in EC2. I have two nodes. 1 manager and 1 worker. The host instance type is Alpine Linux.

In the Manager host the Memory usage is:

~ $ free -m
                    total       used       free     shared    buffers     cached
Mem:                 7972       4996       2975        715        178       2857
-/+ buffers/cache:   1960       6011
Swap:                 0          0          0

~ $

Now when i login the container,

~ $ docker exec -it c7cc255aca1f /bin/bash
 [root@c7cc255aca1f /]# 

And see the memory utilization:

[root@c7cc255aca1f /]# free -m
              total        used        free      shared  buff/cache   available
Mem:           7972        1654        2970         715        3347        5269
Swap:             0           0           0
[root@c7cc255aca1f /]#

Kindly help me how to clear the buff/cache or cached ?

like image 427
Anandkumar Lakshmanan Avatar asked Sep 13 '25 10:09

Anandkumar Lakshmanan


2 Answers

To clear the cache please see https://unix.stackexchange.com/questions/17936/setting-proc-sys-vm-drop-caches-to-clear-cache.

By the way why do you want to drop the cache? I am certain you do not want to drop the cache (unless you are doing some read benchmark, like with dd).

Your cache will be freed up as soon as a process needs memory. It is designed to use all available memory. Under Linux it is completely OK to run with no "free" memory (the one free refers to as "free") as long as you have enough available (or total-- buffers/cache is enough).

It is also highly recommended to add SWAP (even if you do not want to swap) to prevent OOM situations. Just create a swapfile somewhere, and set the vm.swappiness sysctl parameter to 0 so it won't be used if there is still RAM memory available.

like image 116
scream314 Avatar answered Sep 15 '25 02:09

scream314


While the kernel may evict buffers/cache for containers as needed, it cannot evict tmpfs.

LXCFS reports tmpfs as buffers/cache.

Systemd-journald writing logfiles in /run (a tmpfs), will be charged to the buffers/cache - and no amount of dumping buffers will free ram up once journald eats all ram. When OOMkiller strikes, it might take out a process or the entire container due to tmpfs eating all ram.

You can clear your journal files to reduce the size:

journalctl --vacuum-time=2days

Or you can clear based on size:

journalctl --vacuum-size=100M

Note that older versions of journalctl do NOT offer --vacuum functions. I am not aware how to cleanly clear the logfiles (other than deleting them by hand?)

like image 35
math Avatar answered Sep 15 '25 03:09

math