何时不使用垃圾回收? [英] When NOT to use garbage collection?

查看:128
本文介绍了何时不使用垃圾回收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不使用垃圾回收的明显情况是很难实时的,内存严重有限,并且想要用指针来操作。有没有其他的,少讨论的,为什么有人会喜欢手动内存管理而不是GC的好理由?

如果你有完全增量的垃圾回收器,每个分配的内存字节有限的执行时间,那么就足够疯狂了,它不一定是不使用垃圾回收的理由:)

垃圾收集的一个基本问题是,难以估计和管理内存中工作集的实际大小,因为垃圾收集器只能延迟释放内存。所以,是的,当内存受到限制时,垃圾收集可能不是一个好选择。



垃圾收集的另一个问题是它有时会干扰释放其他资源,如文件描述符,窗口句柄等等,因为垃圾收集器可能只会延迟释放这些资源,从而导致资源匮乏。

垃圾收集也会导致缓存垃圾,因为内存不一定以本地方式分配。例如,堆栈分配的内存比堆分配的短期对象更容易缓存。



最后,垃圾回收当然会消耗CPU时间:)所以如果你可以手动编写内存管理,你可以节省垃圾收集器消耗的CPU周期:)


The obvious cases for not using garbage collection are hard realtime, severely limited memory, and wanting to do bit twiddling with pointers. Are there any other, less discussed, good reasons why someone would prefer manual memory management instead of GC?

解决方案

It IS possible to use garbage collection with hard real time, if you have fully incremental garbage collector with bounded execution time per byte of allocated memory, so, crazily enough, it is NOT necessarily a reason not to use garbage collection :)

One fundamental problem with garbage collection, though, is that it is difficult to estimate and manage the actual size of the working set in memory, because garbage collector can free your memory only delayedly. So, yes, when memory is restricted, garbage collection might not be a good choice.

Another problem with garbage collection is that it sometimes interferes with freeing other resources such as file descriptors, window handles, etc., because, again, the garbage collector might free those resources only delayedly, causing resource starvation.

Garbage collection can also cause cache trashing, because the memory is not necessarily allocated in a local fashion. For example, stack allocated memory is much more cache-friendly than heap-allocated short-lived objects.

Finally, garbage collection of course consumes CPU time :) So if you can code manually memory management you can save the CPU cycles the garbage collector would consume :)

这篇关于何时不使用垃圾回收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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