垃圾回收机制是如何工作的? [英] How does the Garbage Collection mechanism work?

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

问题描述

用外行术语来说,垃圾收集机制是如何工作的?

In a lay-man terminology how does the garbage collection mechanism work?

如何确定对象可用于垃圾回收?

How an object is identified to be available for garbage collection?

另外,Reference Counting, Mark and Sweep, Copying, Train 在 GC 算法中是什么意思?

Also, what do Reference Counting, Mark and Sweep, Copying, Train mean in GC algorithms?

推荐答案

当您使用带有垃圾收集功能的语言时,您将无法直接访问内存.相反,您可以访问该数据之上的一些抽象.正确抽象的一件事是数据块在内存中的实际位置,以及指向其他数据块的指针.当垃圾收集器运行时(这种情况偶尔会发生),它会检查您是否仍然持有对它为您分配的每个内存块的引用.如果不这样做,它将释放该内存.

When you use a language with garbage collection you wont get access to the memory directly. Rather you are given access to some abstraction on top of that data. One of the things that is properly abstracted away is the the actual location in memory of the data block, as well as pointers to other datablocks. When the garbage collector runs (this happens occasionally) it will check if you still hold a reference to each of the memory blocks it has allocated for you. If you don't it will free that memory.

不同类型的垃圾收集器之间的主要区别在于它们的效率以及它们可以处理的分配方案类型的任何限制.

The main difference between the different types of garbage collectors is their efficiency as well as any limitations on what kind of allocation schemes they can handle.

最简单的是正确的引用计数.当您创建对对象的引用时,该对象上的内部计数器会增加,当您碰巧引用或它不再在范围内时,(前)目标对象上的计数器会减少.当这个计数器达到零时,对象不再被引用并且可以被释放.

The simplest is properly reference counting. When ever you create a reference to an object an internal counter on that object is incremented, when you chance the reference or it is no longer in scope, the counter on the (former) target object is decremented. When this counter reaches zero, the object is no longer referred at all and can be freed.

引用计数垃圾收集器的问题在于它们无法处理循环数据.如果对象 A 有对对象 B 的引用,而后者又对对象 A 有一些(直接或间接)引用,则它们永远不会被释放,即使链中的任何对象都没有被引用到链外(因此没有被引用)程序根本无法访问).

The problem with reference counting garbage collectors is that they cannot deal with circular data. If object A has a reference to object B and that in turn has some (direct or indirect) reference to object A, they can never be freed, even if none of the objects in the chain are refereed outside the chain (and therefore aren't accessible to the program at all).

另一方面,标记和清除算法可以处理这个问题.标记和清除算法的工作原理是定期停止程序的执行,将程序分配的每个项目标记为不可达.然后程序遍历程序拥有的所有变量并将它们指向的标记为可访问的.如果这些分配中的任何一个包含对程序中其他数据的引用,则该数据同样被标记为可达等.

The Mark and sweep algorithm on the other hand can handle this. The mark and sweep algorithm works by periodically stopping the execution of the program, mark each item the program has allocated as unreachable. The program then runs through all the variables the program has and marks what they point to as reachable. If either of these allocations contain references to other data in the program, that data is then likewise marked as reachable, etc.

这是算法的标记部分.在这一点上,一切程序可以访问的东西,无论多么间接,都被标记为可达,而程序不能访问的所有东西都被标记为不可达.垃圾收集器现在可以安全地回收与标记为无法访问的对象关联的内存.

This is the mark part of the algorithm. At this point everything the program can access, no matter how indirectly, is marked as reachable and everything the program can't reach is marked as unreachable. The garbage collector can now safely reclaim the memory associated with the objects marked as unreachable.

标记和清除算法的问题在于它的效率不高——必须停止整个程序才能运行它,而且很多对象引用都不会改变.

The problem with the mark and sweep algorithm is that it isn't that efficient -- the entire program has to be stopped to run it, and a lot of the object references aren't going to change.

为了改进这一点,标记和清除算法可以通过所谓的分代垃圾收集"进行扩展.在这种模式下,已经在系统中进行了一些垃圾回收的对象会被提升到老年代,而老年代并不经常检查.

To improve on this, the mark and sweep algorithm can be extended with so called "generational garbage collection". In this mode objects that have been in the system for some number of garbage collections are promoted to the old generation, which is not checked that often.

这提高了效率,因为对象往往很年轻(想想一个字符串在循环内被改变,导致可能有几百个周期的生命周期)或存活很长时间(用于表示应用程序主窗口的对象, 或 servlet 的数据库连接).

This improves efficiency because objects tend to die young (think of a string being changed inside a loop, resulting in perhaps a lifetime of a few hundred cycles) or live very long (the objects used to represent the main window of an application, or the database connection of a servlet).

可以在维基百科上找到更多详细信息.

Much more detailed information can be found on wikipedia.

根据评论添加:

使用标记和清除算法(以及除引用计数之外的任何其他垃圾收集算法)垃圾收集不会在您的程序上下文中运行,因为它必须能够访问您的程序无法直接访问的内容.因此说垃圾收集器在堆栈上运行是不正确的.

With the mark and sweep algorithm (as well as any other garbage collection algorithm except reference counting) the garbage collection do not run in the context of your program, since it has to be able to access stuff that your program is not capable of accessing directly. Therefore it is not correct to say that the garbage collector runs on the stack.

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

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