Read file without disk caching in Linux

前端 未结 2 1593
天命终不由人
天命终不由人 2021-01-04 10:35

I have a C program that runs only weekly, and reads a large amount of files only once. Since Linux also caches everything that\'s read, they fill up the cac

相关标签:
2条回答
  • 2021-01-04 10:54

    I believe passing O_DIRECT to open() should help:

    O_DIRECT (Since Linux 2.4.10)

    Try to minimize cache effects of the I/O to and from this file. In general this will degrade performance, but it is useful in special situations, such as when applications do their own caching. File I/O is done directly to/from user space buffers. The O_DIRECT flag on its own makes at an effort to transfer data synchronously, but does not give the guarantees of the O_SYNC that data and necessary metadata are transferred. To guarantee synchronous I/O the O_SYNC must be used in addition to O_DIRECT.

    There are further detailed notes on O_DIRECT towards the bottom of the man page, including a fun quote from Linus.

    0 讨论(0)
  • 2021-01-04 10:58

    You can use posix_fadvise() with the POSIX_FADV_DONTNEED advice to request that the system free the pages you've already read.

    0 讨论(0)
提交回复
热议问题