V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
youthdou
V2EX  ›  Linux

为什么清除缓存后,读文件的程序依然运行很快?

  •  
  •   youthdou · May 21, 2025 · 2610 views
    This topic created in 354 days ago, the information mentioned may be changed or developed.

    自己写的程序,通过 mmap 将一个大文件映射到内存,随机读取部分数据。

    测试环境:虚拟机,ubuntu 22.04 ,机械盘。

    第一运行时间大概在 20s 左右。

    第二次运行前,运行“echo 3 > /proc/sys/vm/drop_caches ”,清除缓存。再次运行,运行时间 1s 左右。清了缓存,为什么仍然会快很多?

    6 replies    2025-05-22 09:34:33 +08:00
    seers
        1
    seers  
       May 21, 2025 via Android
    mmap 内存不会清理,显示 munmap 才行
    youthdou
        2
    youthdou  
    OP
       May 21, 2025
    @seers 结束的时候会调用 unmap 。
    iBugOne
        3
    iBugOne  
       May 21, 2025
    既然你说了是虚拟机,那么有没有一种可能,你的宿主机把虚拟磁盘文件对应的块*也*给缓存起来了(狗头
    timewarp
        4
    timewarp  
       May 21, 2025
    看下/proc/meminfo 里 file active/inactive 就知道缓存在不在了
    youthdou
        5
    youthdou  
    OP
       May 22, 2025
    @iBugOne 👍 实体机上试了下,清除缓存后运行,时间是一致的。
    youthdou
        6
    youthdou  
    OP
       May 22, 2025
    @timewarp 看过了,缓存是被清掉了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   873 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 21:39 · PVG 05:39 · LAX 14:39 · JFK 17:39
    ♥ Do have faith in what you're doing.