Java或C ++中的堆管理器如何跟踪线程或进程使用的所有内存位置? [英] How does the heap manager in java or C++ keep track of all the memory locations used by the threads or processes?

查看:77
本文介绍了Java或C ++中的堆管理器如何跟踪线程或进程使用的所有内存位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解在C ++或C跟踪线程和进程使用的内存位置的情况下,Java或OS中堆管理器的哪些数据结构.一种方法是使用对象和内存地址的映射以及内存起始地址和对象在内存中的大小的反向映射. 但是在这里它将无法在O(1)时间内满足新的内存请求.有没有更好的数据结构可以做到这一点?

I wanted to understand what data structures the heap managers in Java or OS in case of C++ or C keep track of the memory locations used by the threads and processes. One way is to use a map of objects and the memory address and a reverse map of memory starting address and the size of the object in the memory. But here it won't be able to cater the new memory requests in O(1) time. Is there any better data structure to do this?

推荐答案

请注意,非托管语言将通过系统调用来分配/释放内存,通常不会自己对其进行管理.仍然不管什么抽象级别(运行时使用的是OS),都必须对此进行处理:

Note that unmanaged languages are going to be allocating/freeing memory through system calls, generally not managing it themselves. Still regardless of what level of abstraction (OS to the run time), something has to deal with this:

一种方法称为伙伴块分配,该方法在 Wikipedia 上得到了很好的描述.它实质上跟踪大小不同(通常为2的倍数)的内存中空间的使用情况.可以使用具有巧妙索引的多个数组来完成此操作,或者可以使用二叉树更直观地做到这一点,每个节点都可以判断某个块是否空闲,同一层上的所有节点都代表相同大小的块.

One method is called buddy block allocation, described well with an example on Wikipedia. It essentially keeps track of the usage of spaces in memory of varying sizes (typically multiples of 2). This can be done with a number of arrays with clever indexing, or perhaps more intuitively with a binary tree, each node tell whether a certain block is free, all nodes on a level representing the same size block.

这遭受内部分裂;随着事情的发展,您最终可能会分散数据而不是有效地合并数据,从而使其更难放入大数据中.可以用更复杂的动态系统来解决,但伙伴块具有以下优势:简单.

This suffers from internal fragmentation; as things come and go, you might ended up with your data scattered rather than being efficiently consolidated, making it harder to fit in large data. This could be countered by a more complicated, dynamic system, but buddy blocks have the advantage of simplicity.

这篇关于Java或C ++中的堆管理器如何跟踪线程或进程使用的所有内存位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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