清除/proc/meminfo中的“缓存”内存而不使用drop_caches

10
我有一个嵌入式系统运行着2.6.10的Linux内核(我知道!),我注意到随着系统运行时间越长,“cached”内存使用量会线性增加,就像在/proc/meminfo中看到的那样。有时它会达到总RAM的70%,导致系统崩溃。
  1. 我不知道为什么在低内存条件下这个空间没有被内核自动回收。我认为“cached”内存虽然被使用,但仍被视为可用。
  2. 为了至少手动回收这个空间,我需要在/proc/sys/vm/中调整drop_caches设置。但是,此功能仅在2.6.16内核中引入。
由于“cached”中的很大一部分是页面缓存、文件和inode缓存,是否有任何方法可以在没有drop_caches功能的情况下强制回收它们?我尝试过同步,但完全没有帮助。
我已经搜索了很多关于此问题的资源,但找不到不依赖于drop_caches功能的资源。
编辑:

根据其他帖子的建议,我进行了一个测试,每秒写入一次到板载闪存分区,我看到/proc/slabinfo中的dentrysize-64字段都在上升(每个bash脚本循环增加1)。然而,我没有注意到Cached内存使用量有任何增加。

更多信息:

我必须提到,根目录本身位于ramfs中。我们确实有一个闪存芯片可以读写,但是访问太频繁。我已经粘贴了在崩溃之前一段时间拍摄的meminfo和slabinfo。请注意,活动和缓存字段总和大于总可用内存!

MemTotal:       256708 kB
MemFree:          4896 kB
Buffers:             0 kB
Cached:         181892 kB
SwapCached:          0 kB
Active:         178140 kB
Inactive:        46496 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:       256708 kB
LowFree:          4896 kB
SwapTotal:           0 kB
SwapFree:            0 kB
Dirty:               0 kB
Writeback:           0 kB
Mapped:          56656 kB
Slab:            11252 kB
CommitLimit:    128352 kB
Committed_AS:   175668 kB
PageTables:        908 kB
VmallocTotal:   737280 kB
VmallocUsed:    113084 kB
VmallocChunk:   624100 kB

slabinfo - version: 2.1
# name            <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <batchcount> <limit> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>
rwsem_lock_cache       0      0     64   59    1 : tunables  120   60    0 : slabdata      0      0      0
rwsem_owner_cache     16     59     64   59    1 : tunables  120   60    0 : slabdata      1      1      0
rwsem_sap_cache        0      0    128   30    1 : tunables  120   60    0 : slabdata      0      0      0
pss_node_cache         0      0     32  113    1 : tunables  120   60    0 : slabdata      0      0      0
mts-sapinfo-cache      0      0   1152    7    2 : tunables   24   12    0 : slabdata      0      0      0
mts-sap-desc-cache     34     36    448    9    1 : tunables   54   27    0 : slabdata      4      4      0
mts-qentry-cache       5     59     64   59    1 : tunables  120   60    0 : slabdata      1      1      0
mts-rcpnt-cache        1      1   5568    1    2 : tunables    8    4    0 : slabdata      1      1      0
rpc_buffers            8      8   2048    2    1 : tunables   24   12    0 : slabdata      4      4      0
rpc_tasks              8     24    160   24    1 : tunables  120   60    0 : slabdata      1      1      0
rpc_inode_cache        0      0    416    9    1 : tunables   54   27    0 : slabdata      0      0      0
unix_sock             70     70    384   10    1 : tunables   54   27    0 : slabdata      7      7      0
tcp_tw_bucket          0      0     96   40    1 : tunables  120   60    0 : slabdata      0      0      0
tcp_bind_bucket       25    203     16  203    1 : tunables  120   60    0 : slabdata      1      1      0
tcp_open_request       2     59     64   59    1 : tunables  120   60    0 : slabdata      1      1      0
inet_peer_cache        4     59     64   59    1 : tunables  120   60    0 : slabdata      1      1      0
ip_fib_alias          23    203     16  203    1 : tunables  120   60    0 : slabdata      1      1      0
ip_fib_hash           23    113     32  113    1 : tunables  120   60    0 : slabdata      1      1      0
ip_dst_cache          34     45    256   15    1 : tunables  120   60    0 : slabdata      3      3      0
arp_cache             13     24    160   24    1 : tunables  120   60    0 : slabdata      1      1      0
raw_sock               8      8    480    8    1 : tunables   54   27    0 : slabdata      1      1      0
udp_sock               7      8    480    8    1 : tunables   54   27    0 : slabdata      1      1      0
tcp_sock              29     36   1024    4    1 : tunables   54   27    0 : slabdata      9      9      0
flow_cache             0      0     96   40    1 : tunables  120   60    0 : slabdata      0      0      0
cfq_ioc_pool           0      0     24  145    1 : tunables  120   60    0 : slabdata      0      0      0
cfq_pool               0      0    104   37    1 : tunables  120   60    0 : slabdata      0      0      0
crq_pool               0      0     52   72    1 : tunables  120   60    0 : slabdata      0      0      0
deadline_drq           0      0     48   78    1 : tunables  120   60    0 : slabdata      0      0      0
as_arq                 4     63     60   63    1 : tunables  120   60    0 : slabdata      1      1      0
mqueue_inode_cache      1      8    480    8    1 : tunables   54   27    0 : slabdata      1      1      0
jffs2_inode_cache     24    145     24  145    1 : tunables  120   60    0 : slabdata      1      1      0
jffs2_node_frag    36493  36576     28  127    1 : tunables  120   60    0 : slabdata    288    288      0
jffs2_raw_node_ref  52018  52171     16  203    1 : tunables  120   60    0 : slabdata    257    257      0
jffs2_tmp_dnode        0      0     24  145    1 : tunables  120   60    0 : slabdata      0      0      0
jffs2_raw_inode        0      0     68   56    1 : tunables  120   60    0 : slabdata      0      0      0
jffs2_raw_dirent       0      0     40   92    1 : tunables  120   60    0 : slabdata      0      0      0
jffs2_full_dnode   36494  36540     16  203    1 : tunables  120   60    0 : slabdata    180    180      0
jffs2_i               13     24    328   12    1 : tunables   54   27    0 : slabdata      2      2      0
nfs_write_data        36     36    448    9    1 : tunables   54   27    0 : slabdata      4      4      0
nfs_read_data         32     36    448    9    1 : tunables   54   27    0 : slabdata      4      4      0
nfs_inode_cache        0      0    544    7    1 : tunables   54   27    0 : slabdata      0      0      0
nfs_page               0      0     64   59    1 : tunables  120   60    0 : slabdata      0      0      0
dnotify_cache          0      0     20  169    1 : tunables  120   60    0 : slabdata      0      0      0
kioctx                 0      0    192   20    1 : tunables  120   60    0 : slabdata      0      0      0
kiocb                  0      0    128   30    1 : tunables  120   60    0 : slabdata      0      0      0
fasync_cache           0      0     16  203    1 : tunables  120   60    0 : slabdata      0      0      0
shmem_inode_cache    169    170    384   10    1 : tunables   54   27    0 : slabdata     17     17      0
posix_timers_cache      0      0    100   39    1 : tunables  120   60    0 : slabdata      0      0      0
uid_cache              0      0     64   59    1 : tunables  120   60    0 : slabdata      0      0      0
blkdev_ioc             0      0     24  145    1 : tunables  120   60    0 : slabdata      0      0      0
blkdev_queue          25     30    368   10    1 : tunables   54   27    0 : slabdata      3      3      0
blkdev_requests        4     28    140   28    1 : tunables  120   60    0 : slabdata      1      1      0
biovec-(256)         256    256   3072    2    2 : tunables   24   12    0 : slabdata    128    128      0
biovec-128           256    260   1536    5    2 : tunables   24   12    0 : slabdata     52     52      0
biovec-64            256    260    768    5    1 : tunables   54   27    0 : slabdata     52     52      0
biovec-16            256    260    192   20    1 : tunables  120   60    0 : slabdata     13     13      0
biovec-4             256    295     64   59    1 : tunables  120   60    0 : slabdata      5      5      0
biovec-1             256    406     16  203    1 : tunables  120   60    0 : slabdata      2      2      0
bio                  256    295     64   59    1 : tunables  120   60    0 : slabdata      5      5      0
file_lock_cache       65     80     96   40    1 : tunables  120   60    0 : slabdata      2      2      0
sock_inode_cache     121    121    352   11    1 : tunables   54   27    0 : slabdata     11     11      0
skbuff_head_cache    560    560    192   20    1 : tunables  120   60    0 : slabdata     28     28      0
sock                   9     12    320   12    1 : tunables   54   27    0 : slabdata      1      1      0
proc_inode_cache    1196   1196    304   13    1 : tunables   54   27    0 : slabdata     92     92      0
sigqueue              26     26    148   26    1 : tunables  120   60    0 : slabdata      1      1      0
radix_tree_node     2030   2030    276   14    1 : tunables   54   27    0 : slabdata    145    145      0
bdev_cache             1      9    416    9    1 : tunables   54   27    0 : slabdata      1      1      0
mnt_cache             20     40     96   40    1 : tunables  120   60    0 : slabdata      1      1      0
inode_cache         2665   2665    288   13    1 : tunables   54   27    0 : slabdata    205    205      0
dentry_cache        4147   4147    136   29    1 : tunables  120   60    0 : slabdata    143    143      0
filp                1380   1440    160   24    1 : tunables  120   60    0 : slabdata     60     60      0
names_cache            4      4   4096    1    1 : tunables   24   12    0 : slabdata      4      4      0
idr_layer_cache       97    116    136   29    1 : tunables  120   60    0 : slabdata      4      4      0
buffer_head            0      0     48   78    1 : tunables  120   60    0 : slabdata      0      0      0
mm_struct             44     49    544    7    1 : tunables   54   27    0 : slabdata      7      7      0
vm_area_struct      3452   3680     84   46    1 : tunables  120   60    0 : slabdata     80     80      0
fs_cache              45    113     32  113    1 : tunables  120   60    0 : slabdata      1      1      0
files_cache           42     45    416    9    1 : tunables   54   27    0 : slabdata      5      5      0
signal_cache          57     60    256   15    1 : tunables  120   60    0 : slabdata      4      4      0
sighand_cache         55     60   1312    3    1 : tunables   24   12    0 : slabdata     20     20      0
task_struct           96    100    960    4    1 : tunables   54   27    0 : slabdata     25     25      0
anon_vma            1273   1356      8  339    1 : tunables  120   60    0 : slabdata      4      4      0
size-131072(DMA)       0      0 131072    1   32 : tunables    8    4    0 : slabdata      0      0      0
size-131072            1      1 131072    1   32 : tunables    8    4    0 : slabdata      1      1      0
size-65536(DMA)        0      0  65536    1   16 : tunables    8    4    0 : slabdata      0      0      0
size-65536             0      0  65536    1   16 : tunables    8    4    0 : slabdata      0      0      0
size-32768(DMA)        0      0  32768    1    8 : tunables    8    4    0 : slabdata      0      0      0
size-32768             1      1  32768    1    8 : tunables    8    4    0 : slabdata      1      1      0
size-16384(DMA)        0      0  16384    1    4 : tunables    8    4    0 : slabdata      0      0      0
size-16384             8      8  16384    1    4 : tunables    8    4    0 : slabdata      8      8      0
size-8192(DMA)         0      0   8192    1    2 : tunables    8    4    0 : slabdata      0      0      0
size-8192              3      3   8192    1    2 : tunables    8    4    0 : slabdata      3      3      0
size-4096(DMA)         0      0   4096    1    1 : tunables   24   12    0 : slabdata      0      0      0
size-4096            278    278   4096    1    1 : tunables   24   12    0 : slabdata    278    278      0
size-2048(DMA)         0      0   2048    2    1 : tunables   24   12    0 : slabdata      0      0      0
size-2048            292    292   2048    2    1 : tunables   24   12    0 : slabdata    146    146      0
size-1024(DMA)         0      0   1024    4    1 : tunables   54   27    0 : slabdata      0      0      0
size-1024             60     60   1024    4    1 : tunables   54   27    0 : slabdata     15     15      0
size-512(DMA)          0      0    512    8    1 : tunables   54   27    0 : slabdata      0      0      0
size-512             216    216    512    8    1 : tunables   54   27    0 : slabdata     27     27      0
size-256(DMA)          0      0    256   15    1 : tunables  120   60    0 : slabdata      0      0      0
size-256              76     90    256   15    1 : tunables  120   60    0 : slabdata      6      6      0
size-192(DMA)          0      0    192   20    1 : tunables  120   60    0 : slabdata      0      0      0
size-192             139    140    192   20    1 : tunables  120   60    0 : slabdata      7      7      0
size-128(DMA)          0      0    128   30    1 : tunables  120   60    0 : slabdata      0      0      0
size-128             857    870    128   30    1 : tunables  120   60    0 : slabdata     29     29      0
size-96(DMA)           0      0     96   40    1 : tunables  120   60    0 : slabdata      0      0      0
size-96             1049   1080     96   40    1 : tunables  120   60    0 : slabdata     27     27      0
size-64(DMA)           0      0     64   59    1 : tunables  120   60    0 : slabdata      0      0      0
size-64             4425   4425     64   59    1 : tunables  120   60    0 : slabdata     75     75      0
size-32(DMA)           0      0     32  113    1 : tunables  120   60    0 : slabdata      0      0      0
size-32             4633   4633     32  113    1 : tunables  120   60    0 : slabdata     41     41      0
kmem_cache           120    120    128   30    1 : tunables  120   60    0 : slabdata      4      4      0

2
你确定系统崩溃是因为内存不足的情况吗? - MK.
我怀疑它确实有问题。在只剩下4MB的空闲内存时,就出现了最后一个警告。 - Ram
2
我认为active + cached不是有意义的计算。https://access.redhat.com/solutions/406773说:“Active:最近使用过且通常未被交换或回收的内存”。因此,缓存内存可能被视为活动内存。只要还有空闲内存,就没有理由开始驱逐缓存,我认为长时间运行所有内存都被使用是正常的,只要你没有真正用完,并且你需要证明你确实用完了。你看到有任何进程因OOM而被杀死吗? - MK.
1
运行一个程序,在循环中分配大块内存,并进行一些睡眠,看看它在获取越来越多的内存时会发生什么。希望缓存会下降,然后 OOM 会终止它。 - MK.
引发了什么警告?此外,@MK.建议运行一个分配内存的程序实际上是对你的问题的答案,我想不到其他的方法来完成这个任务而不使用drop_caches。关于drop_caches,有很多"替代事实",我也不明白除非存在一些虚拟内存错误,否则缓存页面会造成问题。请参考这里中有关日志记录drop_caches的讨论。 - JimD.
显示剩余2条评论
1个回答

5

我认为你的某个地方有tmpfs/ramfs填满了。由于tmpfs/ramfs的工作方式,Linux内核将tmpfs上的文件占用空间计算为“Cached”。tmpfs/ramfs上的文件实际上只是未支持的缓存条目。

# echo 1 > /proc/sys/vm/drop_caches
# grep '^Cached' /proc/meminfo
Cached:          1341824 kB
# dd if=/dev/zero of=/tmp/testfile bs=1G count=4
4+0 records in
4+0 records out
4294967296 bytes (4.3 GB) copied, 1.33696 s, 3.2 GB/s
# grep '^Cached' /proc/meminfo
Cached:          5543480 kB
# echo 1 > /proc/sys/vm/drop_caches
# grep '^Cached' /proc/meminfo
Cached:          5494072 kB
# rm /tmp/testfile
# grep '^Cached' /proc/meminfo
Cached:          1306488 kB

这是“缓存”内存,Linux无法自动回收,因为它没有任何真正的持久性存储来支持。如您在示例中所见,即使使用drop_caches也无法帮助您。

因此,请查看/proc/mounts以查找已挂载的tmpfs/ramfs,并监视哪些正在填充。


好建议!楼主应该查看是否有东西在tmpfs上写入日志文件,因为tmpfs经常在嵌入式领域中用作读-写闪存镜像的可写层。 - pjz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接