如何现代化的虚拟机处理内存分配? [英] How do modern VMs handle memory allocation?

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

问题描述

我正在用C写的一个简单的栈机器上,主要用于学习的目的。使用的malloc /免费我的记忆操作后,我认为这将是一个好主意,读现代虚拟机的一些内存分配的特定code。

I'm working on a simple stack machine written in C, mostly for learning purposes. After using malloc/free for my memory operations, I thought it would be a good idea to read some memory allocation specific code from modern virtual machines.

我下载的Lua源$ C ​​$ C,并开始阅读。过了一会儿,我发现有很多东西宏观参与,我找不到code其中真正的内存分配完成(即的malloc 调用)

I downloaded Lua source code and started reading it. After a while, I realized there are lots of macro stuff involved, and I couldn't find the code where real memory allocation is done (ie. malloc call).

find . -exec grep -i "malloc" '{}' \; -print

据印只有一些Lua中宏有的malloc 字,他们的名字。 Lua的VM(和编程语言)不使用的malloc 在所有!

It printed only some Lua macros that have malloc word in their names. The Lua VM (and programming language) doesn't use malloc at all!

因此​​,这使我的问题是:如何做到现代化的虚拟机处理内存分配?怎样的Lua从堆中分配的内存?是否有任何分配方面比的malloc 其他?有什么利弊/其他方法缺点?

So this leads me to the question: how do modern VMs handle memory allocation? How does Lua allocate memory from the heap? Are there any ways for allocation other than malloc? What are the pros/cons of other methods?

我也想知道有关最佳实践,设计模式等对分配的内存安全工作。我看到Lua的来源有很多间接的分配内存之前。我在哪里可以了解这种东西?

I'm also wondering about best-practices, design-patterns, etc. for safely working on allocated memory. I see in Lua's source that there is lots of indirection before allocating memory. Where can I learn about this stuff?

推荐答案

Lua的最肯定使用的malloc ,形式为的realloc (人们还可以通过自定义分配器太),但是,因为Lua使用如基于虚拟机的语言99%的GC,它使用宏自动GC的头块添加到配置。

Lua most definitely uses malloc, in the form of realloc (one can also pass a custom allocator too), however, because Lua uses a GC like 99% of VM based languages, it uses the macros to automatically add the GC header block to the allocation.

您会发现Lua的内存全部由 LuaM处理在 lmem.c _ 程序和 lmem.h ,这些都使用虚拟机的全局状态来存储一个allocator,它最初被设置为 l_alloc (从 lauxlib.c ),但可以通过 lua_setallocf 。

You'll find Lua's memory all handled by the LuaM_ routines in lmem.c and lmem.h, these all use the global state of the VM to store an allocator, which is initially set to l_alloc (from lauxlib.c), but can be changed by lua_setallocf.

近日,LuaJIT增加分配下沉和计划的一些非常酷的内存功能,您可以这篇文章的 LuaJIT垃圾收集。文章涵盖了很多的策略和设计围绕旋转VM / JIT内存分配,下沉,聚集和垃圾回收的。

Recently, LuaJIT added allocation sinking and plans for some really cool memory features, which you can read up on this article on LuaJIT Garbage Collection. The article covers a lot of strategy and design revolving around VM/JIT memory allocation, sinking, aggregation, and garbage collecting.

正如你可以看到,内存的分配和下沉战略是非常紧密地联系到GC 1员工(如果有的话)。

As you can see, memory allocation and sinking strategies are very closely link to the GC one employs (if any).

在亲的和不同的内存的分配器反对的方面的,使用标准的的malloc 使用简单,但在速度和浪费的成本调准及各种额外块标记到每个分配。

In terms of pro's and con's of various memory allocators, using standard malloc is simple to use, but at the cost of speed and wastage to alignment and various extra blocks tagged on to each allocation.

转移到更高级的舞台,游泳池,板和块分配器,我们可以显着地(特别是固定大小的内部虚拟机分配)加快速度,避免了很多,可与更普遍的分配器,如发生分裂和开销的malloc ,但当然这些分配器是更复杂,你必须对它们进行调试,如果你从头开始(这就像一个VM一个更大的系统只是自找麻烦)并列为对尝试和测试CRT 的malloc 实施

Moving to more advanced arena, pool, slab and block allocators, we can speed things up dramatically (especially for fixed size internal VM allocations) and avoid a lot of the fragmentation and overhead that can occur with more general allocators such as malloc, but of course these allocators are more complex, and you have to debug them if you start from scratch (which in a bigger system like a VM is just asking for problems), as apposed to the tried-and-tested CRT malloc implementation.

这篇关于如何现代化的虚拟机处理内存分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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