linux - Linux 上无法清除缓存

标签 linux caching memory

我在 EC2(8GB 内存)、ArchLinux 上运行 Logstash 和 Elasticsearch。

我无法清除缓存内存(我了解内核应该能够在需要时收回该内存)。从meminfo,我了解到大部分缓存内存都是不活动的,这意味着没有进程正在使用它,所以我应该能够清除它,对吧?

我正在执行 sync && echo 3 >/proc/sys/vm/drop_caches 来清除内存。 只有系统重新启动才能清除该内存。 可能存在内存泄漏,我如何查看哪个进程正在使用此缓存内存。

cat /proc/meminfo 
MemTotal:        7661268 kB
MemFree:          613144 kB
MemAvailable:     561144 kB
Buffers:            8580 kB
Cached:          2271144 kB
SwapCached:            0 kB
Active:           165424 kB
Inactive:        2244356 kB
Active(anon):     152528 kB
Inactive(anon):  2226088 kB
Active(file):      12896 kB
Inactive(file):    18268 kB
Unevictable:     4429248 kB
Mlocked:         4429248 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:               164 kB
Writeback:             0 kB
AnonPages:       4559304 kB
Mapped:            33916 kB
Shmem:           2226220 kB
Slab:              54908 kB
SReclaimable:      33764 kB
SUnreclaim:        21144 kB
KernelStack:        1176 kB
PageTables:        11832 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     3830632 kB
Committed_AS:    6861504 kB
VmallocTotal:   34359738367 kB
VmallocUsed:       19132 kB
VmallocChunk:   34359707084 kB
HardwareCorrupted:     0 kB
AnonHugePages:   4329472 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       10240 kB
DirectMap2M:     7985152 kB

top的输出:

top - 06:04:57 up 16 days, 17:12,  1 user,  load average: 0.00, 0.04, 1.39
Tasks:  76 total,   1 running,  75 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.3 sy,  0.0 ni, 99.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   7661268 total,  7051140 used,   610128 free,     9772 buffers
KiB Swap:        0 total,        0 used,        0 free.  2272468 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                     
19958 elastic+  20   0 9746648 4.273g  22760 S   0.3 58.5   1:11.36 java                                                                        
19970 root      20   0 1008496  89912   3876 S   0.3  1.2   0:07.13 node                                                                        
19932 root      20   0   28256   6728   6496 S   0.0  0.1   0:01.29 systemd-journal                                                             
    1 root      20   0  110052   3316   1596 S   0.0  0.0   1:45.84 systemd                                                                     
19856 root      20   0   16136   2100   1460 S   0.0  0.0   0:02.05 bash                                                                        
19851 root      20   0   82472   2040   1136 S   0.0  0.0   0:01.39 sshd                                                                        
  245 haproxy   20   0   28872   1996    624 S   0.0  0.0   4:01.48 haproxy                                                                     
19854 root      20   0  157700   1980      0 S   0.0  0.0   0:00.00 (sd-pam)                                                                    
  244 haproxy   20   0   28596   1728    768 S   0.0  0.0   0:00.00 haproxy                                                                     
  221 systemd+  20   0   23496   1476   1056 S   0.0  0.0   0:04.99 systemd-network                                                             
20197 root      20   0   17544   1464   1064 R   0.0  0.0   0:00.02 top                                                                         
  149 root      20   0   31016   1456    768 S   0.0  0.0   0:00.10 systemd-udevd                                                               
  233 root      20   0   39980   1448    860 S   0.0  0.0   0:01.03 sshd                                                                        
  217 root      20   0   13204   1344    796 S   0.0  0.0   0:02.55 crond                                                                       
  219 dbus      20   0   24748   1236    900 S   0.0  0.0   0:18.18 dbus-daemon                                                                 
  229 systemd+  20   0   18164   1196    980 S   0.0  0.0   0:00.92 systemd-resolve                                                             
19853 root      20   0   26764   1168    700 S   0.0  0.0   0:00.01 systemd                                                                     
  218 root      20   0   15252   1144    876 S   0.0  0.0   0:07.26 systemd-logind                                                              
  194 root      16  -4  105484   1136    764 S   0.0  0.0   0:03.15 auditd                                                                      
  214 root      20   0   19240   1024    776 S   0.0  0.0   1:08.62 irqbalance                                                                  
  226 root      20   0    8124    820    700 S   0.0  0.0   0:00.00 agetty                                                                      
  225 root      20   0    8124    816    700 S   0.0  0.0   0:00.00 agetty                                                                      
  224 root      20   0    8124    792    680 S   0.0  0.0   0:00.01 agetty                                                                      
  242 root      20   0   23896    624    292 S   0.0  0.0   0:00.00 haproxy-systemd                                                             
  213 root      20   0    9324    612    420 S   0.0  0.0   9:44.04 rngd                                                                        
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.16 kthreadd                                                                    
    3 root      20   0       0      0      0 S   0.7  0.0  65:18.76 ksoftirqd/0                                                                 
    5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                                                                
    7 root      20   0       0      0      0 S   0.0  0.0  10:08.87 rcu_sched                                                                   
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh                                                                      
    9 root      rt   0       0      0      0 S   0.0  0.0   0:00.36 migration/0                                                                 
   10 root      rt   0       0      0      0 S   0.0  0.0   0:01.17 watchdog/0                                                                  
   11 root      rt   0       0      0      0 S   0.0  0.0   0:00.91 watchdog/1                                                                  
   12 root      rt   0       0      0      0 S   0.0  0.0   0:00.37 migration/1      

最佳答案

首先 - 您给出的 echo 命令应该清除页面缓存。这与给出的命令 here 相同。 .

页面缓存由内核管理。它不被任何进程使用(至少不直接使用)。如果内核决定这样做,则页面缓存使用的内存可以提供给任何进程。内存没有泄漏。

如果您想做一个实验,请编写一个程序,重复写入 4 或 6 GB 空间中的每个页面。您将看到页面缓存下降的大小。

关于linux - Linux 上无法清除缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30272412/

相关文章:

linux - Bash enter & ctrl-z 按键

linux - AWK 脚本自动从字符串中删除前导 0

c - 构建一个带有一些内核内存泄露的应用程序

c++ - Linux:有没有办法在不停止/暂停进程(SIGSTOP)的情况下使用 ptrace?

python - 在 Docker 容器中找不到共享库 libpython3.5(但覆盖工作正常)

linux - 如何在目录和子目录中的所有文件中用另一个字符串模式替换字符串模式

spring - 如何停止 org.springframework.web.client.RestTemplate 缓存响应?

symfony - 关闭翻译缓存

asp.net-mvc - 缓存的正确信息是什么?什么是好的页面加载时间?

windows - Hyper-V WMI 类的哪些属性可访问内存信息