垃圾收集器的替代品 [英] Alternative for Garbage Collector

查看:97
本文介绍了垃圾收集器的替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道垃圾收集器的最佳选择,有其优点和缺点。我的首要任务是速度,记忆力不那么重要。如果垃圾收集器没有任何暂停,请告诉我。



我正在研究一种安全的语言(即没有悬挂指针的语言,检查

解决方案

我怀疑你会是最好的坚持使用垃圾回收(根据JVM),除非您有其他很好的理由。现代GC是非常快速,通用和安全的。除非你可以设计你的语言来利用一个非常特殊的特例(就像上面的一个分配器那样),那么你不可能击败JVM。

唯一的我现在看到的作为反对现代GC的论点的真正令人信服的理由是由GC暂停引起的延迟问题。对于大多数目的来说,这些都很小,很少见,并不是真正的问题(例如,我已经成功地用Java编写了3D引擎),但它们仍然可能在非常紧密的实时情况下导致问题。



话虽如此,但仍有一些特殊情况,其中不同的内存分配方案可能有意义,所以我列出了一些有趣的选项:



一个非常快速,专用的内存管理方法的例子是许多游戏中使用的每帧分配器。这是通过递增单个指针来分配内存,并在一段时间结束时(通常是一个可视化的框架),通过简单地将指针设置回基地址并在下一个分配中覆盖它们,立即丢弃所有对象。这可能是安全的,但是对象生命周期的限制会非常严格。如果你能保证所有内存分配的大小都是有限的,并且只对处理的范围有效,则可能是赢家。一个服务器请求。



另一种非常快速的方法是针对不同类别的对象使用专用对象池。释放的对象可以在池中循环使用,使用类似链接的免费对象插槽列表。操作系统通常将这种方法用于常见的数据结构。然而,您需要再次观察对象生命期并通过将对象返回到池来显式处理处置。

没有意义,因为每当您更改指针值时,您经常不得不取消引用和更新两个对象的计数。这种成本通常比具有简单快速的内存管理的优势更差,并且在循环引用的情况下也不起作用。



堆栈分配速度极快,可以安全运行。根据您的语言,可以做到没有堆,并且完全在基于堆栈的系统上运行。不过,我怀疑这会在某种程度上限制你的语言设计,因此可能不是首发。仍然可能值得考虑某些DSL。



经典的malloc / free 非常快,如果您对对象有足够的约束,创作和生活,你可以用你的语言来强制执行。例如,如果是,例如,你对指针的使用有很大的限制。



无论如何 - 希望这是有用的食物!


I'd like to know the best alternative for a garbage collector, with its pros and cons. My priority is speed, memory is less important. If there is garbage collector which doesn't make any pause, let me know.

I'm working on a safe language (i.e. a language with no dangling pointers, checking bounds, etc), and garbage collection or its alternative has to be used.

解决方案

I suspect you will be best sticking with garbage collection (as per the JVM) unless you have a very good reason otherwise. Modern GCs are extremely fast, general purpose and safe. Unless you can design your language to take advantage of a very specific special case (as in one of the above allocators) then you are unlikely to beat the JVM.

The only really compelling reason I see nowadays as an argument against modern GC is latency issues caused by GC pauses. These are small, rare and not really an issue for most purposes (e.g. I've successfully written 3D engines in Java), but they still can cause problems in very tight realtime situations.

Having said that, there may still be some special cases where a different memory allocation scheme may make sense so I've listed a few interesting options below:

An example of a very fast, specialised memory management approach is the "per frame" allocator used in many games. This works by incrementing a single pointer to allocate memory, and at the end of a time period (typically a visual "frame") all objects are discarded at once by simply setting the pointer back to the base address and overwriting them in the next allocation. This can be "safe", however the constraints of object lifetime would be very strict. Might be a winner if you can guarantee that all memory allocation is bounded in size and only valid for the scope of handling e.g. a single server request.

Another very fast approach is to have dedicated object pools for different classes of object. Released objects can just be recycled in the pool, using something like a linked list of free object slots. Operating systems often used this kind of approach for common data structures. Again however you need to watch object lifetime and explicitly handle disposals by returning objects to the pool.

Reference counting looks superficially good but usually doesn't make sense because you frequently have to dereference and update the count on two objects whenever you change a pointer value. This cost is usually worse than the advantage of having simple and fast memory management, and it also doesn't work in the presence of cyclic references.

Stack allocation is extremely fast and can run safely. Depending on your language, it is possible to make do without a heap and run entirely on a stack based system. However I suspect this will somewhat constrain your language design so that might be a non-starter. Still might be worth considering for certain DSLs.

Classic malloc/free is pretty fast and can be made safe if you have sufficient constraints on object creation and lifetime which you may be able to enforce in your language. An example would be if e.g. you placed significant constraints on the use of pointers.

Anyway - hope this is useful food for thought!

这篇关于垃圾收集器的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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