Dynamics 内存管理内部
我是学生,想了解更多有关动态内存管理的信息。对于C++来说,调用operator new()可以在Heap(Free Store)下分配一块内存块。事实上,我不知道如何实现它。
有几个问题: 1)操作系统分配内存块的机制是什么?据我所知,有一些基本的内存分配方案,如首次适应、最佳适应和最差适应。操作系统是否使用其中之一在堆下动态分配内存?
2)对于Android、IOS、Window等不同平台,它们是否使用不同的内存分配算法来分配内存块?
3)对于C++,当我调用operator new()或malloc()时,内存分配器是否在堆中随机分配内存块?
希望任何人都可以帮助我。
谢谢
i am student and want to know more about the dynamics memory management. For C++, calling operator new() can allocate a memory block under the Heap(Free Store ). In fact, I have not a full picture how to achieve it.
There are a few questions:
1) What is the mechanism that the OS can allocate a memory block?? As I know, there are some basic memory allocation schemes like first-fit, best-fit and worst-fit. Does OS use one of them to allocate memory dynamically under the heap?
2) For different platform like Android, IOS, Window and so on, are they used different memory allocation algorithms to allocate a memory block?
3) For C++, when i call operator new() or malloc(), Does the memory allocator allocate a memory block randomly in the heap?
Hope anyone can help me.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
malloc
不是系统调用,它是库 (libc
) 例程,它会遍历其一些内部结构,为您提供所需大小的空闲内存块的地址。仅当进程的数据段(即它可以使用的虚拟内存)根据所讨论的 malloc 逻辑不够“足够大”时,它才会执行系统调用。 (在 Linux 上,扩大数据段的系统调用是brk
)简单地说,
malloc
提供细粒度的内存管理,而操作系统则管理更粗粒度的大块可用内存到那个过程。不仅不同平台,不同库使用不同的
malloc
;一些程序(例如python
)使用其内部分配器,因为它们知道自己的使用模式并且可以通过这种方式提高性能。维基百科上有一篇关于 malloc 的长文。
malloc
is not a system call, it is library (libc
) routine which goes through some of its internal structures to give you address of a free piece of memory of the required size. It only does a system call if the process' data segment (i.e. virtual memory it can use) is not "big enough" according to the logic ofmalloc
in question. (On Linux, the system call to enlarge data segment isbrk
)Simply said,
malloc
provides fine-grained memory management, while OS manages coarser, big chunks of memory made available to that process.Not only different platforms, but also different libraries use different
malloc
; some programs (e.g.python
) use its internal allocator instead as they know its own usage patterns and can increase performance that way.There is a longthy article about malloc at wikipedia.