使用多个堆进行内存管理有什么好处? [英] Is there any benefit to use multiple heaps for memory management purposes?

查看:112
本文介绍了使用多个堆进行内存管理有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a student of a system software faculty. Now I'm developing a memory manager for Windows. Here's my simple implementation of malloc() and 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;
}

工作正常.但是我不明白有没有使用多个堆的理由?好了,我可以在堆中分配内存,并将地址获取到分配的内存块中.但是这里我使用 ONE 堆.有使用多个堆的理由吗?也许适用于多线程/多进程应用程序?请解释.

It works fine. But I can't understand is there a reason to use multiple heaps? Well, I can allocate memory in the heap and get the address to an allocated memory chunk. But here I use ONE heap. Is there a reason to use multiple heaps? Maybe for multi-threaded/multi-process applications? Please explain.

推荐答案

您有一些很好的想法,这对于C语言是有效的,但是在C ++中,您有析构函数,它们运行非常重要.

You have some good thoughts and this'd work for C but in C++ you have destructors, it is VERY important they run.

您可以将所有类型都视为具有构造函数/析构函数,只是在逻辑上什么都不做".

You can think of all types as having constructors/destructors, just that logically "do nothing".

这是关于分配器的.请参阅好友算法",该算法使用2的幂来对齐和重用内容.

This is about allocators. See "The buddy algorithm" which uses powers of two to align and re-use stuff.

如果我在某处分配4个字节,则分配器可能只为4个字节分配分配4kb的部分.这样,我可以在该块中放入1024个4字节的东西,如果需要更多,可以添加另一个块,依此类推.

If I allocate 4 bytes somewhere, my allocator might allocate a 4kb section just for 4 byte allocations. That way I can fit 1024 4 byte things in the block, if I need more add another block and so forth.

询问它4kb,它不会在4byte块中分配它,它可能有一个单独的请求较大的请求.

Ask it for 4kb and it wont allocate that in the 4byte block, it might have a separate one for larger requests.

这意味着您可以将大事放在一起.如果我先写17个字节,然后再写13个字节,再写1个字节,然后再释放13个字节,那么我只能在其中< = 13个字节.

This means you can keep big things together. If I go 17 bytes then 13 bytes the 1 byte and the 13byte gets freed, I can only stick something in there of <=13 bytes.

因此,伙伴系统和2的幂都可以使用lshifts轻松完成,如果我想要一个2.5kb的块,我将其分配为2的最小幂(在这种情况下为4kb),可以使用然后放置< = 4kb的项目.

Hence the buddy system and powers of 2, easy to do using lshifts, if I want a 2.5kb block, I allocate it as the smallest power of 2 that'll fit (4kb in this case) that way I can use the slot afterwards for <=4kb items.

这不是用于垃圾回收,这只是使事情更紧凑和整洁,使用您自己的分配器可以停止对OS的调用(取决于new的默认实现,并删除它们可能已经对编译器执行了此操作),快速新建/删除.

This is not for garbage collection, this is just keeping things more compact and neat, using your own allocator can stop calls to the OS (depending on the default implementation of new and delete they might already do this for your compiler) and make new/delete very quick.

堆压缩是非常不同的,您需要一个指向堆的每个指针的列表,或者某种遍历整个内存图的方式(例如 spits Java),因此当您四处移动时并压缩",您可以将指向该事物的所有内容更新为当前位置.

Heap-compacting is very different, you need a list of every pointer that points to your heap, or some way to traverse the entire memory graph (like spits Java) so when you move stuff round and "compact" it you can update everything that pointed to that thing to where it currently is.

这篇关于使用多个堆进行内存管理有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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