buffer/cache内存优化_posix_fadvise_主动释放读缓存cache

ops/2024/10/15 8:50:09/

1.问题现象

1.htop free命令发现系统 buffer/cache 内存占用高

free -h
total used free shared buff/cache available
Mem: 61Gi 15Gi 569Mi 1.7Gi 45Gi 43Gi
Swap: 30Gi 0.0Ki 30Gi

cat /proc/meminfo or grep -E "Buff|Cache" /proc/meminfo
Buffers: 370568 kB
Cached: 45599784 kB
SwapCached: 0 kB

![[<a class=缓存cache高_htop展示.jpg]]" />

2.问题原因

原理

  • cache 读数据保存到内存,加速下次读速度
  • buffer 写缓冲区
    linux 系统为了提高下一次读取速度, 从内存cache中读取

cache_22">cache加速读实例

第一次读取

dd if=/dev/zeroo of=test.img bs=1M count=512
free -h # 查看内存
total used free shared buff/cache available
Mem: 15Gi 3.7Gi 10Gi 516Mi 993Mi 11Gi
Swap: 4.0Gi 0B 4.0Gi
time grep 123 test.img
real 0m0.480s
user 0m0.138s
sys 0m0.110s

第二次读取

free -h # 查看内存, 确认cache是否增加
total used free shared buff/cache available
Mem: 15Gi 3.7Gi 10Gi 516Mi 1.5Gi 11Gi
Swap: 4.0Gi 0B 4.0Gi
time grep 123 test.img
real 0m0.163s
user 0m0.102s
sys 0m0.061s

结论:

  1. buffer/cache 由 993Mi 变为1.5Gi
  2. 第二次读取, 匹配关键字速度提升 0.480s --> 0.163s

3.优化与解决方法

1.不解决: 当系统内存不足时, 主动释放cache, 提供给程序,系统需要的内存
2.需要优化的场景

  1. 确认文件只需要读取一次, 后续不会再次读取: 比如配置文件, 只读取一次的数据

cache_54">1.测试示例: posix_fadvise主动释放读缓存cache

在线gitee代码: 4_read_cache_读缓存优化_posix_fadvise.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>#define KB_SIZE 1024
#define MB_SIZE (1024 * KB_SIZE)
#define GB_SIZE (1024 * MB_SIZE)
#define BUF_4K (4 * KB_SIZE)void show_cache_buffer_info()
{system("free -m");system("grep -E 'Buffers|Cached' /proc/meminfo");
}void help()
{char *help_str = "\默认 正常读文件 \n\1 n NOREUSE \n\2 d DONTNEED\n";printf("%s", help_str);
}int main(int argc, char *argv[])
{int ret = 0;// 1.生产大文件if((ret=access("testfile.img", F_OK)) != 0)system("dd if=/dev/zero of=testfile.img bs=1M count=512");// 2.清空系统缓存system("sync; echo 3 > /proc/sys/vm/drop_caches");show_cache_buffer_info();// 3.打开文件int fd = open("testfile.img", O_RDONLY);if (fd < 0){perror("open");exit(1);}// // 4.读缓存优化 --> 释放缓存,要在读取完之后再释放// if (argc == 2)// {//     char opt = *argv[1];//     if (opt == '1')//         posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE);//     if (opt == '2')//         posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);// }// 5.读文件char buf[BUF_4K];while (1){ret = read(fd, buf, BUF_4K);if (ret < 0){perror("read");exit(1);}if (ret == 0){break;}}// 6.释放读缓存fsync(fd);      // fsync将写数据落盘,这样才能确保 page cache全部释放成功if (argc == 2){char opt = *argv[1];if (opt == '1' || opt == 'n')posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE);if (opt == '2' || opt == 'd')posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);}close(fd);show_cache_buffer_info();
}

1.默认读取文件–测试数据

time ./4_read_cache_读缓存 优化_posix_fadvise.out

           total        used        free      shared  buff/cache   available

Mem: 15998 3566 11440 516 991 11636
Swap: 4096 0 4096
Buffers: 2828 kB
Cached: 938928 kB
SwapCached: 0 kB

        total        used        free      shared  buff/cache   available

Mem: 15998 3555 10936 516 1506 11647
Swap: 4096 0 4096
Buffers: 3544 kB
Cached: 1464996 kB
SwapCached: 0 kB

real 0m0.491s
user 0m0.032s
sys 0m0.165s

2.posix_fadvise POSIX_FADV_NOREUSE预期对文件中的信息的访问不会重复 – 测试数据

time ./4_read_cache_读缓存 优化_posix_fadvise.out 1

           total        used        free      shared  buff/cache   available

Mem: 15998 3554 11452 516 990 11648
Swap: 4096 0 4096
Buffers: 2428 kB
Cached: 938988 kB
SwapCached: 0 kB

           total        used        free      shared  buff/cache   available

Mem: 15998 3549 10942 516 1505 11653
Swap: 4096 0 4096
Buffers: 3300 kB
Cached: 1465136 kB
SwapCached: 0 kB

real 0m0.513s
user 0m0.026s
sys 0m0.218s

3.posix_fadvise POSIX_FADV_DONTNEED丢弃任何与该区域相关的缓存 – 测试数据

time ./4_read_cache_读缓存 优化_posix_fadvise.out 2

           total        used        free      shared  buff/cache   available

Mem: 15998 3562 11444 516 991 11640
Swap: 4096 0 4096
Buffers: 3028 kB
Cached: 938740 kB
SwapCached: 0 kB
total used free shared buff/cache available
Mem: 15998 3560 11442 516 994 11642
Swap: 4096 0 4096
Buffers: 4012 kB
Cached: 941124 kB
SwapCached: 0 kB

real 0m0.538s
user 0m0.035s
sys 0m0.232s

4.测试小结:

通过系统API posix_fadvise POSIX_FADV_DONTNEED 可以主动释放文件读缓存cache

4.总结

  1. 缓存cache, 是系统为了提高下一次读取速度, 保存文件内存在内存中
  2. 确认是否优化减少读缓存cache
  3. 目前已知的读缓存cache优化点
    1. 文件只需要读取一次, 后续不会再次读取: 如配置文件, 或只读取一次的数据


http://www.ppmy.cn/ops/125855.html

相关文章

Postman最新V11版本关键更新一览

Postman作为接口测试中&#xff0c;被广泛应用的一款主流工具&#xff0c;以其丰富的功能&#xff0c;灵活方便的使用方式&#xff0c;广受欢迎。最新发布的V11版本则在向协作平台转型的过程中一路狂奔&#xff0c;增加大量全新的协作支持。下面我们就一起来看看都有哪些变化吧…

戴尔Windows10专业版系统镜像:免费获取!

今天&#xff0c;系统之家小编给大家分享2024年最新的戴尔Windows10专业版系统下载&#xff0c;该版本系统采用最新Windows10 22H2 19045.5011 64位专业版进行离线制作&#xff0c;确保系统安全无毒&#xff0c;系统的一些安全问题也得到解决&#xff0c;整体操作更放心。系统的…

adminPage-vue3依赖 v1.2.0新增组件 DetailsModule说明文档

adminPage-vue3依赖 v1.2.0新增组件 DetailsModule说明文档 引入思路介绍DetailsModuleAPI汇总属性插槽自定义对象config(array<object\>/object 类型)config.list(array<object\> 类型) 使用基础使用范例config-titleconfig-moduleKeyconfig-listconfig-list-slot…

【Oracle 数据库技术分享】BLOB 与 CLOB 的选择与应用

【Oracle 数据库技术分享】BLOB 与 CLOB 的选择与应用 文章目录 前言一、为什么选择 BLOB 或 CLOB&#xff1f;二、BLOB 与 CLOB 的区别1.数据类型2.性能3.功能4.兼容性5.存储限制6.查询和处理 三、选择 BLOB 或 CLOB 的建议1.根据数据类型选择2.考虑性能需求3.兼容性和集成4.综…

数据结构——递归思想

递归思想是一种在数学和计算机科学中广泛应用的解决问题的方法。它的核心思想是将一个复杂的问题分解为规模较小的相同类型的问题&#xff0c;直到达到一个可以直接解决的简单情况&#xff08;通常称为基准情况或递归终止条件&#xff09;。递归通过函数调用自身来实现这一分解…

Spring Cloud Sentinel配置

Spring Cloud Sentinel 文章目录 Spring Cloud Sentinel1. Sentinel Dashboard 启动2. Spring Cloud 客户端配置3. Sentinel Dashboard 限流配置直连关联链路 4. Sentinel Dashboard 熔断配置 1. Sentinel Dashboard 启动 下载sentinel dashboard jar包sentinel dashboard启动…

MES管理系统如何实现生产过程全流程追溯

在现代制造业的转型升级中&#xff0c;MES管理系统作为核心管理工具&#xff0c;正以其卓越的能力引领着生产过程的实时追踪与优化。MES管理系统通过一系列创新技术和精细化管理模块&#xff0c;构建了一个高度集成、高效运转的生产监控平台&#xff0c;为企业提供了前所未有的…

安卓14无法安装应用解决历程

客户手机基本情况&#xff1a; 安卓14&#xff0c;对应的 targetSdkVersion 34 前天遇到了安卓14适配问题&#xff0c;客户发来的截图是这样的 描述&#xff1a;无法安装我们公司的B应用。 型号&#xff1a;三星google美版 解决步骤&#xff1a; 1、寻找其他安卓14手机测试…