面对操作系统的交换,缓存以节省内存是否有意义?

发布于 2024-12-23 16:24:33 字数 996 浏览 2 评论 0原文

免责声明:我对内存管理或性能知之甚少,并且我用 C# 编写代码。

问题:

是否“缓存”中等大小的数据(例如,几十 MB),尤其是 <媒体将随时发送到设备(音频和图像),在磁盘上(而不是“将其保存在(虚拟)内存中”),面对事实上任何操作系统都会交换(也许“页面”是正确的词)未使用的内存到磁盘?

这可能还不清楚,所以我将发布示例。
它主要与用户界面相关,而不是网络I/O

我正在谈论的示例:

  • FooSlideshow 应用程序可以将幻灯片存储在磁盘上,而不是为其分配虚拟内存。
  • BarGame 可以在磁盘上存储不同的、大量事件的声音并加载它们进行播放。
  • BazRenderer 可以将多个层的位图存储在合成图像中,如果它们不易经常变化(如果只有一层发生变化,则只需从磁盘读取其余层)。

我没有谈论的示例:

  • 当歌曲从服务器流式传输时,FooPlayer 会缓存歌曲的缓冲区。
  • BarBrowser 会缓存图像,因为用户可能会访问同一页面。

为什么我应该关心:

因为,假设幻灯片在 1024x768 屏幕上以 32 位/像素全屏显示时,将花费 1024 * 768 * 32 字节 = 3 MiB (8 MiB 对于高清屏幕)。因此,对于 10 张幻灯片的幻灯片,仅缓存图像就需要 30-80 MiB。一首短歌,转换为 16 位样本 44.1 KHz(CD 质量)也会得到平均权重。

从我的 C# 代码(但也可以是 Java、Python 等)来看,我应该关心创建一个复杂的缓存系统以尽可能释放内存,或者我应该信任操作系统 > 把它换掉? (而且,结果是一样的?一种方法会比另一种更好?为什么?)

Disclaimer: I know very little about memory management or performance, and I code in C#.

Question:

Does "caching" medium-sized data (in the order of, say, dozens of MBs), especially media that will be sent at any time to a device (audio and images), on disk (instead of "keeping it in (virtual) memory"), in face of the fact that any OS will swap (maybe "page" is the correct word) unused memory to disk?

This may not have been clear, so I'll post examples.
It is mainly related to user interfaces, not network I/O.

Examples of what I'm talking about:

  • FooSlideshow app could store slides on disk instead of allocating virtual memory for them.
  • BarGame could store sounds of different, numerous events on disk and load them for playing.
  • BazRenderer could store bitmaps of the several layers in a composite image if they're not prone to constant changing (If only one layer changes, the rest just have to be read from disk).

Examples of what I'm not talking about:

  • FooPlayer caches a buffer of the song while it streams from the server.
  • BarBrowser caches images because the user may visit the same page.

Why I should care:

Because, let's say a slideshow, when shown fullscreen on a 1024x768 screen, with 32 bits/pixel, would spend 1024 * 768 * 32 bytes = 3 MiB (8 MiB for an HD screen). So for a 10-slides slideshow, that would be 30-80 MiB just to cache the images. A short song, converted to 16-bit sample 44.1 KHz (CD quality) would also weight that on average.

From my C# code (but it could be Java, Python, whatever), should I care about making a complex caching system to free memory whenever possible, or should I trust the OS to swap that out? (And, the result would be the same? One approach will be better than the other? Why?)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文