有什么简单的方法可以解释垃圾收集的工作原理? [英] What is an easy way to explain how Garbage Collection works?

查看:66
本文介绍了有什么简单的方法可以解释垃圾收集的工作原理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的关注时间很短,因此无法通过 Wikipedia文章.

I have a short attention span so I couldn't make my way through the Wikipedia article.

我了解有几种垃圾收集技术,但一种常见的方法是可达性"测试,其中对象的收集资格取决于有根对象是否可以达到"(对我来说,理解是已知不需要收集的对象).当您想知道某个对象是否可达时,您将如何处理?你怎么知道去哪里看?

I understand there are several techniques for garbage collection but a common one is the "reachability" test, where an object's eligibility for collection is based on whether or not it can be "reached" by a rooted object (which, to my understanding is an object that is known to not require collection). When you want to know if a certain object is reachable, how would you go about it? How do you know where to look?

显然,收集器必须知道所有分配的对象和根对象.它如何确定每个对象的可达性?

The collector obviously must be aware of all allocated objects and rooted objects. How does it determine the reachability of each of these objects?

推荐答案

我说,通过遍历指针/引用.原则上,您只需查看对象是否仍然具有指向该对象的引用(来自其他对象,当前执行的代码的局部变量,...).如果还没有,则无法再次获得对该对象的引用(在Java之类的语言中,至少在不能进行指针欺骗的地方),因此通常可以安全地丢弃该特定对象.

By walking the pointers/references, I'd say. In principle you just look whether an object still has references pointing to it (from other objects, local variables of currently executed code, ...). If it hasn't then no reference to this object can be obtained again (in languages like Java, at least where you can't do pointer trickery), so it's usually safe to throw that particular object away.

其他已使用(或仍在使用)的方案例如是引用计数,其中每个对象都有对其的引用计数器,每当有人获得对该对象的引用时该计数器必须增加,而每当有人丢失对该对象的引用时该计数器必须递减目的.如果我没记错的话,Windows中的COM就是这种方式.

Other schemes used (or still in use) are for example reference counting where each object has a counter of references to it which must be incremented each time someone gets a reference to that object and decremented each time someone loses a reference to that object. COM in Windows works that way, if I remember correctly.

Java和.NET使用(尤其是)世代垃圾收集,其中最初假定每个对象很快死亡(世代假设).然后,它进行一些优化以保持垃圾收集周期的快速,从而不会破坏程序的正常运行.在过去,GC在运行时锁定程序有时会持续几秒钟,这种情况并不少见.

Java and .NET use (among others) generational garbage collection where each object is initially assumed to die very quickly (generational hypothesis). It then emplos some optimizations to keep garbage collection cycles fast and thus not disrupt the programs running too much. In ye olde times it wasn't uncommon for GC to lock up a program while it ran, sometimes for several seconds.

除此之外,GC通常仅在内存不足时运行,即. e.积累了太多的死物,需要将其回收.这就是为什么大多数托管应用程序似乎比非托管应用程序浪费更多内存的原因,即使在许多情况下,可以通过运行一次GC来回收很多内存.

Aside from that, GC usually only runs when memory is low, i. e. too many dead objects have accumulated and need to be reclaimed. That's why most managed applications seem to waste much more memory compared to unmanaged ones, even though in many cases much of that memory can be reclaimed by running the GC once.

这篇关于有什么简单的方法可以解释垃圾收集的工作原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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