使用linux用户空间内存进行缓存

发布于 2025-01-07 17:40:51 字数 394 浏览 0 评论 0原文

我想在我的用户空间 Linux 应用程序中实现内存缓存(某种类型)。

我所追求的行为本质上与Linux的文件系统缓冲区缓存相同:任何其他不使用的内存都会被我的缓存进程使用。

如果我要用硬编码限制我的缓存应该有多大来编写此内容,那么我本质上必须将我的缓存限制为某个预定义的值,而不是如果其余进程没有使用尽可能多的内存,或者它会增长,那么它就不会增长如果内存使用量激增,则会缩小。

我想要的是,Linux 在实际需要内存时调用某种回调,通知我从缓存中删除一些条目。

这样,进程需要的任何内存都可以从我的缓存中取出,而剩下的内存则专用于我的缓存。

有没有办法在linux下做到这一点?

最好的方法是监视 /proc/meminfo 并增加缓存大小,使使用的物理内存几乎小于总物理内存?

I would like to implement an in memory cache (of some type) in my user space linux application.

what i'm after is essentially the same behavior as linux's filesystem buffer cache : whatever memory isn't used by anything else, is used by my processes that are caching.

if i were to write this with a hard coded limit on how large my cache should be, I must essentially limit my cache to some predefined value, instead of it growing if the rest of the processes aren't using as much memory, or it shrinking, if memory usage balloons.

what i would like, is linux to call some kind of callback that notifies me to drop some entries from my cache, when it actually needs memory.

that way, whatever memory is needed by the processes can be taken away from my cache, and whatever is left of memory is dedicated to my cache.

is there a way to do this in linux?

is the best method of doing this just monitoring /proc/meminfo and increasing my cache size such that the used physical memory is barely less than total physical memory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆 2025-01-14 17:40:52

这听起来很像您正在尝试围绕虚拟内存管理器 (VMM) 进行编码。这不太可能按照您希望的方式工作,或者即使可以:也非常高效。

Poul-Henning Kamp 撰写的 Varnish 架构说明 中讨论了这个问题。我强烈建议您阅读这份文档,因为他谈到了 Varnish 是如何设计来与 VMM 进行有效交互的,并且可能会给您一些关于缓存的替代设计思路的想法。

任何尝试调整自身以适应系统内存压力的用户进程,例如通过查看比率:驻留集大小 (rss) / 虚拟大小 (vsize)。将会受到与 VMM 的反馈循环的影响,并且该反馈循环可能对流程、系统或两者产生有害影响。

查看 /proc 的文档至于从哪里获取 rss,以及进程的 vsize。

祝你好运。

This sounds very much like you are trying to code around the virtual memory manager (VMM). This is not likely to either work the way you would like, or even if it does: be very efficient.

This issue is discussed in the Varnish architecture notes written by Poul-Henning Kamp. I urge you to read this document, as he talks about how Varnish was designed to interact efficiently with the VMM, and may give you some ideas for alternative design ideas for your cache.

Any user process that is attempting to adjust itself to system memory pressure, say by looking at the ratio: resident set size (rss) / virtual size (vsize). Is going to suffer from being in a feedback loop with the VMM, and that feedback loop may have detrimental effects on the process, the system, or both.

Look at the documentation for /proc as to where to get rss, and vsize for a process.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文