madvise和fadvise的区别
2016-02-29 17:47
651 查看
madvise() and posix_fadvise() are not synonymous. madvise() tells the kernel (give advise) what to do with existing memory region while fadvise() tells the kernel what to do with cached (or future cache) of a file data.
For example, if you mmap() anonymous region you should use madvise() to hint the kernal not to swap out (MADV_RANDOM) or to swap out only after access. (MADV_SEQUENTIAL)
If you mmap() a file, or part of a file, you can use either madvise() or fadvise() to hint the kernel to readahead for you (MADV_WILLNEED) or to free that cache (MADV_DONTNEED) or to free after access (POSIX_FADV_NOREUSE, fadvise() only) in additional to the
above.
If you use file without mapping the data to your process memory (without using mmap()), you should use fadvise() only. madvise() has no meaning.
As far as kernel subsystem, in linux, it is the same subsystem, simply different ways to refer to memory pages and file cache. Please note that those are only hints and when memory is in dire, the kernel might decide to swap out or reuse cached data despite
the hint. Only mlock() and mlockall() can prevent that.
In your case, not giving any hint may help, especially if some pages are being read more than other, since the kernel will figure out which pages are "hot" and will attempt to keep in memory.
For example, if you mmap() anonymous region you should use madvise() to hint the kernal not to swap out (MADV_RANDOM) or to swap out only after access. (MADV_SEQUENTIAL)
If you mmap() a file, or part of a file, you can use either madvise() or fadvise() to hint the kernel to readahead for you (MADV_WILLNEED) or to free that cache (MADV_DONTNEED) or to free after access (POSIX_FADV_NOREUSE, fadvise() only) in additional to the
above.
If you use file without mapping the data to your process memory (without using mmap()), you should use fadvise() only. madvise() has no meaning.
As far as kernel subsystem, in linux, it is the same subsystem, simply different ways to refer to memory pages and file cache. Please note that those are only hints and when memory is in dire, the kernel might decide to swap out or reuse cached data despite
the hint. Only mlock() and mlockall() can prevent that.
In your case, not giving any hint may help, especially if some pages are being read more than other, since the kernel will figure out which pages are "hot" and will attempt to keep in memory.
相关文章推荐
- mySQl数据库中不能插入中文的处理办法
- 假期德国实践报告 学习
- 菜单选项卡-2中方法加载选项卡的内容
- 【作业】第一周作业
- iOS 类别 (category)和 类扩展 (extension)的区别
- PDO的使用
- 机器学习常见算法分类汇总
- Web 单点登录系统 JA-SIG CAS
- 在Xcode中使用Git进行源码版本控制
- windows下遍历文件夹
- 软件工程概论课后作业1
- Python 优化集锦
- iOS倒计时按钮Button标题闪烁处理
- ORA-39083 ORA-00959
- redis
- Servlet 与 CGI 的比较
- BMP文件格式详解
- Pycharm设置
- 修改PHP上传文件大小限制的方法
- 异步执性两个线程,两个线程执行完毕后再进行其他操作