使用多个堆进行内存管理是否有任何好处?

Net*_*ire 7 c++ winapi memory-management

我是系统软件系的学生.现在我正在为Windows开发一个内存管理器.这是我的简单实现malloc()free():

HANDLE heap = HeapCreate(0, 0, 0);

void* hmalloc(size_t size)
{
    return HeapAlloc(heap, 0, size);
}

void hfree(void* memory)
{
    HeapFree(heap, 0, memory);
}

int main()
{
    int* ptr1 = (int*)hmalloc(100*sizeof(int));
    int* ptr2 = (int*)hmalloc(100*sizeof(int));
    int* ptr3 = (int*)hmalloc(100*sizeof(int));

    hfree(ptr2);
    hfree(ptr3);
    hfree(ptr1);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它工作正常.但我无法理解是否有理由使用多个堆?好吧,我可以在堆中分配内存并将地址获取到已分配的内存块.但在这里我使用ONE堆.是否有理由使用多个堆?也许对于多线程/多进程应用程序?请解释.

Rax*_*van 11

使用多个堆/自定义分配器的主要原因是为了更好的内存控制.通常在大量新的/删除之后,内存可能会碎片化并且应用程序的性能松散(同时应用程序将消耗更多内存).在更受控制的环境中使用内存可以减少堆碎片.

另一个用法是防止应用程序中的内存泄漏,你可以释放你分配的整个堆,而不需要为释放在那里分配的所有对象而烦恼.

另一种用法是紧密分配的对象,如果您有一个列表,那么您可以在较小的专用堆中分配所有节点,并且应用程序将获得性能,因为在迭代节点时将减少缓存未命中.

编辑:内存管理是一个很难的主题,在某些情况下,它是不正确的.Andrei Alexandrescu曾经有过一次谈话,他说,对于某些应用程序,用默认的替换自定义分配器可以提高应用程序的性能.


mna*_*bil 5

这是一个很好的链接,详细说明了为什么您可能需要多个堆: https://caligari.dartmouth.edu/doc/ibmcxx/en_US/doc/libref/concepts/cumemmng.htm

"Why Use Multiple Heaps?
Using a single runtime heap is fine for most programs. However, using multiple 
heaps can be more efficient and can help you improve your program's performance 
and reduce wasted memory for a number of reasons:

1- When you allocate from a single heap, you may end up with memory blocks on
   different pages of memory. For example, you might have a linked list that 
   allocates memory each time you add a node to the list. If you allocate memory for
   other data in between adding nodes, the memory blocks for the nodes could end up
   on many different pages. To access the data in the list, the system may have to 
   swap many pages, which can significantly slow your program.

   With multiple heaps, you can specify which heap you allocate from. For example, 
   you might create a heap specifically for the linked list. The list's memory blocks 
   and the data they contain would remain close together on fewer pages, reducing the 
   amount of swapping required.

 2- In multithread applications, only one thread can access the heap at a time to 
    ensure memory is safely allocated and freed. For example, say thread 1 is 
    allocating memory, and thread 2 has a call to free. Thread 2 must wait until 
    thread 1 has finished its allocation before it can access the heap. Again, this 
    can slow down performance, especially if your program does a lot of memory 
    operations.

    If you create a separate heap for each thread, you can allocate from them 
    concurrently, eliminating both the waiting period and the overhead required to 
    serialize access to the heap.


 3- With a single heap, you must explicitly free each block that you allocate. If you 
    have a linked list that allocates memory for each node, you have to traverse the 
    entire list and free each block individually, which can take some time.

    If you create a separate heap for that linked list, you can destroy it with a 
    single call and free all the memory at once.

  4- When you have only one heap, all components share it (including the IBM C and 
     C++ Compilers runtime library, vendor libraries, and your own code). If one 
     component corrupts the heap, another component might fail. You may have trouble 
     discovering the cause of the problem and where the heap was damaged.

     With multiple heaps, you can create a separate heap for each component, so if 
     one damages the heap (for example, by using a freed pointer), the others can 
     continue unaffected. You also know where to look to correct the problem."
Run Code Online (Sandbox Code Playgroud)