java 垃圾收集的“孤岛”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/792831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Island of isolation" of Garbage Collection
提问by Tony
Could anyone please explain the concept of Island of isolationof Garbage Collection?
谁能解释一下垃圾收集隔离岛的概念?
回答by Tamas Czinege
Object A references object B. Object B references object A. Neither object A nor object B is referenced by any other object. That's an island of isolation.
对象 A 引用对象 B。对象 B 引用对象 A。对象 A 和对象 B 都没有被任何其他对象引用。那是一座孤岛。
Basically, an island of isolation is a group of objects that reference each other but they are not referenced by any active object in the application. Strictly speaking, even a single unreferenced object is an island of isolation too.
基本上,隔离岛是一组相互引用但未被应用程序中的任何活动对象引用的对象。严格来说,即使是单个未引用的对象也是一个孤岛。
Edit from Comment:
从评论编辑:
class A {
B myB;
}
class B {
A myA;
}
/* later */
A a = new A();
B b = new B();
a.b = b;
b.a = a;
回答by schnaader
Here is a good explanationof this term. Excerpt:
这是对这个术语的一个很好的解释。摘抄:
- "If an object obj1 is garbage collected, but another object obj2 contains a reference to it, then obj2 is also eligible for garbage collection"
- "If object obj2 can access object obj1 that is eligible for garbage collection, then obj2 is also eligible for garbage collection"
This is called "Island of Isolation". An "island of isolation" describes one or more objects have NO references to them from active parts of an application.
- “如果一个对象 obj1 被垃圾回收,但另一个对象 obj2 包含对它的引用,那么 obj2 也有资格进行垃圾回收”
- “如果对象 obj2 可以访问有资格进行垃圾收集的对象 obj1,那么 obj2 也有资格进行垃圾收集”
这就是所谓的“隔离岛”。“隔离岛”描述了一个或多个对象在应用程序的活动部分没有对它们的引用。
回答by Leigh
The thing to keep in mind is that objects are only collected if they are referenced, either directly or indirectly, from a GC root object(threads, current local variables, static variables etc). If two (or more) objects reference each other, but are not referenced from a root, then they are eligible for garbage collection.
要记住的事情是,只有在从GC 根对象(线程、当前局部变量、静态变量等)直接或间接引用对象时,才会收集对象。如果两个(或更多)对象相互引用,但不是从根引用,则它们有资格进行垃圾回收。
回答by Adrian Kreator
In fact, if you understand the concept of Mark and Sweep of Garbage Collection, you'll understand better Island of Isolation too:
事实上,如果你理解了垃圾回收的标记和清除的概念,你也会更好地理解隔离岛:
- The algorithm starts from the GC roots: main thread, local variables in the main method, static variables of the main class.
- The algorithm traverses all object references, starting with the GC roots, and marks every object found as alive.
- All of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects.
- If two or more objects are referencing each other but they are not referenced by objects linked with any root, they are in the Island of Isolation, and are swept too.
- 算法从GC根开始:主线程、main方法中的局部变量、主类的静态变量。
- 该算法遍历所有对象引用,从 GC 根开始,并将找到的每个对象标记为活动的。
- 所有未被标记对象占用的堆内存都被回收。它被简单地标记为空闲,基本上清除了未使用的对象。
- 如果两个或多个对象相互引用,但它们没有被与任何根链接的对象引用,则它们在隔离岛中,也会被扫描。

