c++ - 离开范围时,我的 boost managed_shared_memory 似乎没有从内存空间取消映射

标签 c++ memory boost memory-leaks shared-memory

我意识到使用 boost managed_shared_memory 我有一种奇怪的内存泄漏

在打开或创建共享内存后,一旦超出范围,我的进程持有的内存量不会减少。

这是重现问题的示例:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <chrono>
#include <thread>
#include <iostream>

using namespace boost::interprocess;
using namespace boost::container;

int main()
{

  string shMemName = "name";

  // Remove shared memory to reset example
  shared_memory_object::remove(shMemName.c_str());

  if (true) // Local scope
  {
    // Create shared memory
    managed_shared_memory managed_shm{ open_or_create, shMemName.c_str(), 100000000 };

    // Adding something random to take up most of the place in the memory
    vector<short>* sharedVector = managed_shm.construct<vector<short>>(shMemName.c_str()) (); 
    sharedVector->resize(40000000);
  } // <--- I expect to lose access to the shared memory therefore having it unmapped

  // Even if I remove the shared memory, there seems to still be a leak
  bool success = shared_memory_object::remove(shMemName.c_str());

  std::cout << "Done " << success << std::endl;
  std::this_thread::sleep_for(std::chrono::seconds(50));

  // If we check the amount of memory used by the process, it is > 80 Mb

  return 0;
}

不是删除共享内存,而是使用以下删除其中的 vector 似乎可以有效地释放内存:

managed_shm.destroy<vector<short>>(shMemName.c_str());

但是,这不是我要实现的目标。我希望它仍然在内存中,只是未映射到创建它的进程。

为什么不离开范围取消映射共享内存?我是否必须做一些具体的事情,如果是的话,那是什么?

编辑:

修改示例使程序因内存饱和而崩溃:

int main()
{
  string shMemName = "name";
  shared_memory_object::remove(shMemName.c_str());

  for (int i = 0; i < 500; ++i) // 500 should make it crash on any computer
  {                             // unless you have way too much RAM
    // Create shared memory
    managed_shared_memory managed_shm{ open_or_create, shMemName.c_str(), 100000000 };

    // Adding something random to take up most of the place in the memory
    vector<short>* sharedVector = managed_shm.construct<vector<short>>(shMemName.c_str()) ();
    sharedVector->resize(40000000);

    // Remove memory
    bool success = shared_memory_object::remove(shMemName.c_str());

    std::cout << "Is removal of shared memory successful ?"
      << "(If 1 yes, if 0 no) : " << success << std::endl;
  }
  return 0;
}

共享内存总是被删除并重新创建。它不应该泄漏任何东西……但它确实泄漏了。

最佳答案

您的 vector/实例/在共享内存中,但所有元素数据都不在。因为您不使用共享内存分配器。而不是 std::vector<T>使用 std::vector<T, myallocator>其中 myallocator从共享内存分配。

例如:

靠 Coliru 生存

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <iostream>

namespace bip = boost::interprocess;
using ShMem = bip::managed_shared_memory;
using Vec = bip::vector<short, bip::allocator<short, ShMem::segment_manager> >;

int main()
{
    std::string shMemName = "name";
    bip::shared_memory_object::remove(shMemName.c_str());

    for (int i = 0; i < 500; ++i) // 500 should make it crash on any computer
    {                             // unless you have way too much RAM
        {
            // Create shared memory
            bip::managed_shared_memory managed_shm{ bip::create_only, shMemName.c_str(), 100000000 };

            // Adding something random to take up most of the place in the memory
            Vec& sharedVector = *managed_shm.construct<Vec>("vecname") (managed_shm.get_segment_manager());
            sharedVector.resize(40000000);
        }

        // Remove memory
        bool success = bip::shared_memory_object::remove(shMemName.c_str());

        std::cout << "Is removal of shared memory successful? " << std::boolalpha << success << std::endl;
    }
}

请注意 remove 有一个小问题在 managed_shared_memory 的析构函数之前的循环中对象。

关于c++ - 离开范围时,我的 boost managed_shared_memory 似乎没有从内存空间取消映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51540023/

相关文章:

c++ - 将从方法返回的原始指针存储到智能指针中

c++ - std::atomic 中的自定义类型

c++ - 这是C++中最好的分配方式

c++ - 启动 Boost 线程时的保证

c++ - 我可以在没有结构实例的情况下使用 `hana::keys` 吗?

C++:一般来说,我应该使用字符串还是字符数组?

c - 查找由 malloc 函数分配的大小

c++ - 增加 C/C++ 程序使用的(非堆栈)内存

c++ - 无法解释的行为 boost::scoped_lock

c++ - 为什么Boost图形库的read_graphviz()函数会更改节点的索引