如何实现内存堆 [英] How to implement a memory heap

查看:118
本文介绍了如何实现内存堆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不完全知道如何短语的称号,但问题是:

Wasn't exactly sure how to phrase the title, but the question is:

我听说程序员在程序开始分配的连续内存的一大段,然后根据需要处理它。这与简单地将要在OS需要每次存储器。
我听说,这将是更快,因为它会避免要求的内存不断连续的块操作系统的成本。

I've heard of programmers allocating a large section of contiguous memory at the start of a program and then dealing it out as necessary. This is in contrast to simply going to the OS every time memory is needed. I've heard that this would be faster because it would avoid the cost of asking the OS for contiguous blocks of memory constantly.

我相信JVM不只是这一点,维护自身的内存部分,然后从分配对象。

I believe the JVM does just this, maintaining its own section of memory and then allocating objects from that.

我的问题是,怎么会真正实现吗?

My question is, how would one actually implement this?

谢谢,
dragonwrenn

Thanks, dragonwrenn

推荐答案

大多数C和C ++编译器已经提供了堆内存管理器作为标准库的一部分,所以你不需要为了做任何事情来避免撞上每个请求操作系统。

Most C and C++ compilers already provide a heap memory-manager as part of the standard library, so you don't need to do anything at all in order to avoid hitting the OS with every request.

如果你想提高性能,也有一些改进的周围,你可以简单地联系起来,去分配器。例如,该麦片在现在删除的答复中提到(这实际上是相当不错的 - - ?麦片,你为什么删除)

If you want to improve performance, there are a number of improved allocators around that you can simply link with and go. e.g. Hoard, which wheaties mentioned in a now-deleted answer (which actually was quite good -- wheaties, why'd you delete it?).

如果你想编写自己的堆管理器作为一个学习的过程,这里有它需要做基本的东西:

If you want to write your own heap manager as a learning exercise, here are the basic things it needs to do:


  • 请求大内存块从OS

  • 保持空闲块的链表

  • 当分配请求进来:

    • 在列表中找到一个块对存储一起请求大小再加上一些簿记变量不够大。

    • 分拆块一个足够大的块当前请求,把剩下的放回自由列表

    • 如果没有块足够大,回到操作系统,并要求另一大块


    • 读取头找出尺寸

    • 新释放的块添加到空闲列表

    • 可选,看内存紧随其后的也是免费的列表中列出,并且两个相邻块合并成一个更大的一个(称为合并堆)

    这篇关于如何实现内存堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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