我正在写入大量的数据,这些数据数周内都不会再次读取 - 由于我的程序运行,机器上的可用内存量(显示为“空闲”或“顶部”)很快下降,我的内存量应用程序使用量不会增加 - 其他进程使用的内存量也不会增加。
这让我相信内存正在被文件系统缓存消耗——因为我不打算长时间读取这些数据,所以我希望绕过系统缓冲区,这样我的数据就会直接写入磁盘。我没有提高性能或成为超级忍者的梦想,我的希望是向文件系统暗示我不会很快回来获取这段内存,所以不要花时间针对这些情况进行优化。
在 Windows 上,我遇到了类似的问题,并使用 FILE_FLAG_NO_BUFFERING|FILE_FLAG_WRITE_THROUGH 修复了问题 - 我的应用程序没有消耗机器内存,并且机器总体上更可用。我希望在 Linux 上复制我所看到的改进。在 Windows 上,写入扇区大小的块有限制,我对我测量的增益量的这种限制感到满意。
Linux 中有类似的方法吗?
我能想到的与您提到的 Windows 标志最接近的等效项是使用以下命令打开文件open(2) http://www.kernel.org/doc/man-pages/online/pages/man2/open.2.html flags O_DIRECT | O_SYNC
:
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. See NOTES below for further discussion.
A semantically similar (but deprecated) interface for block devices is
described in raw(8).
当然,试图对这个标志进行研究以确认它是你想要的我发现这个有趣的作品 http://web.archive.org/web/20120618090457/http://kerneltrap.org/node/7563Linus 告诉你无缓冲 I/O 是一个坏主意,他将其描述为“大脑受损”。据此你应该使用madvise() http://www.kernel.org/doc/man-pages/online/pages/man2/madvise.2.html相反,告诉内核如何缓存页面。 YMMV。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)