在 C++ 中寻找基于 Windows RAM 的共享内存解决方案 [英] looking for Windows RAM-based shared memory solution in C++

查看:69
本文介绍了在 C++ 中寻找基于 Windows RAM 的共享内存解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一种情况,我需要将数百兆字节的内存从一个进程传递到另一个进程.现在我是通过文件来做的,它太慢了.我想为了让它更快,这些文件应该直接写入 RAM 并可以从另一个进程访问.不需要花哨的同步.一个进程将创建共享内存对象并用数据填充它们.另一个进程将读取并删除它们.但是,我进行了快速研究,似乎您无法在 Windows 的 RAM 中共享内存 - 共享内存由文件或分页文件支持.boost::interprocess 的文档证实了这一点.如果共享内存实现仍然使用磁盘,那么速度在哪里?有没有使用基于 RAM 的共享内存的 C++ 库?

I'm facing a situation where I need to pass up to several hundreds of megabytes of memory from one process to another. Right now I'm doing it via files and it's too slow. I guess that to make it quicker, those files should be written directly to RAM and be accessible from another process. No fancy synchronization required. One process would create shared memory objects and would fill them with data. The other process would read and remove them. However I've done a quick research and it seems like you can't share memory in RAM in Windows - the shared memory is backed by either a file or paging file. The docs of boost::interprocess confirm this. Where is the speed up then if the shared memory implementation still uses disk? Is there any C++ library that uses RAM-based shared memory?

我做了一些进一步的阅读:1. 来自 boost::interprocess 文档:由于操作系统必须将文件内容与内存内容同步,因此内存映射文件不如共享内存快."2. 来自 http://msdn.microsoft.com/en-us/library/ms810613.aspx:一个内存映射文件也可以同时被多个应用程序映射.这是两个或多个进程在 Windows NT 中直接共享数据的唯一机制."

I made some further reading: 1. from boost::interprocess docs: "as the operating system has to synchronize the file contents with the memory contents, memory-mapped files are not as fast as shared memory. " 2. from http://msdn.microsoft.com/en-us/library/ms810613.aspx: "A memory-mapped file can also be mapped by more than one application simultaneously. This represents the only mechanism for two or more processes to directly share data in Windows NT."

推荐答案

我认为这里有一个根本性的误解:你认为,如果你创建一个由分页文件支持的文件映射,它会和实际写入一样慢磁盘上的东西.

I think that here is a fundamental misunderstanding: you think that, if you create a file mapping backed by the paging file, it will be as slow as actually writing stuff on disk.

这绝对不是这样的:文档中由分页文件支持"的意思是指共享内存一般驻留在内存中,但是如果有的话,它在分页文件中有一个保留的地方来写这样的数据没有足够的可用物理内存,虚拟内存管理器需要换出内存页.

This is definitely not the case: the meaning of "backed by the paging file" in the documentation means that the shared memory in general resides in memory, but it has a reserved place in the paging file to write such data if there's not enough free physical memory and the virtual memory manager needs to swap out memory pages.

这从文档中并不是很清楚,但是 MSDN 上的文件映射 页面确认:

This is not really clear from the documentation but the File Mapping page on MSDN confirms:

[...] 它由磁盘上的文件支持.这意味着当系统换出文件映射对象的页面时,对文件映射对象所做的任何更改都会写入文件.当文件映射对象的页面被换回时,它们从文件中恢复.

[...] It is backed by the file on disk. This means that when the system swaps out pages of the file mapping object, any changes made to the file mapping object are written to the file. When the pages of the file mapping object are swapped back in, they are restored from the file.

请注意,这适用于由分页文件支持的共享内存以及由常规文件支持的内存(VMM 保证各种视图保持一致).

Notice that this applies to shared memory backed by the paging file as well as memory backed by regular files (the VMM guarantees that the various views are kept coherent).

顺便说一句,这就是用户进程中常规"(=虚拟)内存的工作方式:如果当前未使用并且系统需要将物理内存用于其他内容,则可以将分配的每一位内存换出到页面文件中(例如,使当前使用的内存页面可用于您/另一个应用程序).

Incidentally, this is how "regular" (=virtual) memory in user processes works: every bit of allocated memory can be swapped out to the paging file if it's not currently used and the system need to use physical memory for other stuff (e.g. making memory pages that are used at the moment available to your/another application).

这篇关于在 C++ 中寻找基于 Windows RAM 的共享内存解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆