Rethinkdb 容器:rethinkdb 进程比整个容器占用更少的内存

Rethinkdb container: rethinkdb process takes less RAM than the whole container

我是 运行 我在 Kubernetes 集群中的 rethinkdb 容器。以下是我注意到的:

运行 top 在CoreOS主机中,rethinkdb进程占用大约3Gb:

$ top
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
  981 root      20   0   53.9m  34.5m  20.9m S  15.6  0.4   1153:34 hyperkube
51139 root      20   0 4109.3m 3.179g  22.5m S  15.0 41.8 217:43.56 rethinkdb
  579 root      20   0  707.5m  76.1m  19.3m S   2.3  1.0 268:33.55 kubelet

但是运行docker stats检查rethinkdb容器,大约需要7Gb!

$ docker ps | grep rethinkdb
eb9e6b83d6b8        rethinkdb:2.1.5                             "rethinkdb --bind al   3 days ago          Up 3 days                               k8s_rethinkdb-3.746aa_rethinkdb-rc-3-eiyt7_default_560121bb-82af-11e5-9c05-00155d070266_661dfae4

$ docker stats eb9e6b83d6b8
CONTAINER           CPU %               MEM USAGE/LIMIT     MEM %               NET I/O
eb9e6b83d6b8        4.96%               6.992 GB/8.169 GB   85.59%              0 B/0 B

$ free -m
         total       used       free     shared    buffers     cached
Mem:          7790       7709         81          0         71       3505
-/+ buffers/cache:       4132       3657
Swap:            0          0          0

有人可以解释为什么容器占用的内存比 rethinkdb 进程本身多得多吗?

我是 运行 docker v1.7.1,CoreOS v773.1.0,内核 4.1.5

top 命令中,您正在查看物理内存量。在 stats 命令中,这还包括磁盘缓存的 ram,因此它总是大于 ram 的物理量。当您确实需要更多 RAM 时,将释放缓存的磁盘供应用程序使用。

事实上,内存使用是通过 cgroup memory.usage_in_bytes 提取的,您可以在 /sys/fs/cgroup/memory/docker/long_container_id/memory.usage_in_bytes 中访问它。并且根据 linux 文档 https://www.kernel.org/doc/Documentation/cgroups/memory.txt 第 5.5 节:

5.5 usage_in_bytes

For efficiency, as other kernel components, memory cgroup uses some optimization to avoid unnecessary cacheline false sharing. usage_in_bytes is affected by the method and doesn't show 'exact' value of memory (and swap) usage, it's a fuzz value for efficient access. (Of course, when necessary, it's synchronized.) If you want to know more exact memory usage, you should use RSS+CACHE(+SWAP) value in memory.stat(see 5.2).