Boost managed_mapped_file:设置允许的最大内存使用率

Boost managed_mapped_file: setting maximum allowed memory usage

本文关键字:大内 内存 使用率 设置 mapped managed file Boost      更新时间:2023-10-16

有什么方法可以设置managed_mapped_file使用的最大允许内存吗?例如,我有64GB的内存,我创建了一个20GB的文件。这些都已加载到内存中。例如,有没有办法指定只使用1GB的内存?即使近似也足够了。

编辑:我应该添加我使用boost::interprocess::vector。。也许有一种方法可以专门化分配器?

typedef bi::allocator<Node, bi::managed_mapped_file::segment_manager> allocator_node_t;
typedef bi::vector<Node, allocator_node_t> vector_node_t;
bi::managed_mapped_file* nodeFile = new bi::managed_mapped_file(bi::open_or_create, "nodes_m.bin", bigSize);
allocator_node_t alloc_n(nodeFile->get_segment_manager());
vector_node_t* nodes = nodeFile->find_or_construct<vector_node_t>("nodes")(alloc_n);

没有这样的方法(便携)。

前提也是错误的:

例如,我有64GB的内存,我创建了一个20GB的文件。这些都加载到内存中

错误:它将只加载所使用的页面。是的,这可能意味着你最终可能拥有完整的20GB内存。只要没有其他进程需要物理内存来执行其他任务,操作系统就可以自由执行。

操作系统出于任何原因任意取消映射数据都是愚蠢的。您希望操作系统利用可用内存。否则,那些硅片上的钱就浪费掉了。

编辑:我应该添加我使用的boost::interprocess::vector。。也许有一种方法可以专门化分配器?

在没有自定义分配器的情况下使用boost::interprocess::vector首先不会使用共享内存。首先需要使用例如boost::interprocess::allocator<T, boost::interprocess::managed_mapped_file::segment_manager>来使用映射文件。

不,分配器中的任何东西都不能覆盖操作系统虚拟内存调整参数。

没有什么需要专门化(在C++的意义上)

bi::managed_mapped_file* nodeFile = new bi::managed_mapped_file(bi::open_or_create, "nodes_m.bin", bigSize);
allocator_node_t alloc_n(nodeFile->get_segment_manager());
vector_node_t* nodes = nodeFile->find_or_construct<vector_node_t>("nodes")(alloc_n);

第一次执行此代码(即创建"nodes_m.bin")将不会加载bigSize。事实上,它甚至不会在磁盘上分配bigSize!在所有支持它的系统上(据我所知,没有主流操作系统不支持它),该文件是创建的稀疏