在Java中,什么时候对象变得不可访问?

发布于 2024-11-01 08:45:24 字数 79 浏览 0 评论 0原文

在Java中,什么是不可达对象?什么时候对象变得不可访问?在研究垃圾收集时,我无法理解这个概念。

任何人都可以举例给出任何想法吗?

In Java, what is an unreachable object? When does the object become unreachable? While studying garbage collection I was not able to understand this concept.

Can anyone give any ideas with examples?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终陌 2024-11-08 08:45:24

当不再有任何引用变量引用它时,或者当它在孤岛上成为孤儿时。

岛是一个具有指向它的引用变量的对象,但是该对象没有指向它的引用变量。

class A { int i = 5; }
class B { A a = new A(); }
class C {
   B b;
   public static void main(String args[]) {
      C c = new C();
      c.b = new B();
      // instance of A, B, and C created
      c.b = null;
      // instance of B and A eligible to be garbage collected.
   }

编辑:只是想指出,即使 A 的实例有一个引用,它现在在一个岛上,因为 B 的实例没有对它的引用。 A 实例符合垃圾回收条件。

When there are no longer any reference variables referring to it, OR when it is orphaned in an island.

An island being an object that has a reference variable pointing to it, however that object has no reference variables pointing to it.

class A { int i = 5; }
class B { A a = new A(); }
class C {
   B b;
   public static void main(String args[]) {
      C c = new C();
      c.b = new B();
      // instance of A, B, and C created
      c.b = null;
      // instance of B and A eligible to be garbage collected.
   }

EDIT: Just want to point out that even though the instance of A has a reference, it is on an island now because the instance of B does not have a reference to it. The A instance is eligible for garbage collection.

杀お生予夺 2024-11-08 08:45:24

当不再有对某个对象的引用,或者这些引用本身来自无法访问的对象时,该对象就是不可访问的。

Integer i = new Integer(4);
// the new Integer object is reachable  via the reference in 'i' 
i = null;
// the Integer object is no longer reachable. 

An object is unrechable when there are no more references to it, or those references are themselves from unrechable objects.

Integer i = new Integer(4);
// the new Integer object is reachable  via the reference in 'i' 
i = null;
// the Integer object is no longer reachable. 
女皇必胜 2024-11-08 08:45:24

在对象图中,当没有链接到它时,引用将变得无法访问。然后垃圾收集器扫描这些悬空的孤立对象并清除以重新获得分配的内存。

java.lang.ref.{Phantom,Soft,Weak} 引用 将无法访问的对象排入队列。
如果终结器运行,GC 就知道它无法访问。

阅读有关终结的信息 - http://java.sun.com/developer/technicalArticles/javase/ Finalization/

在只有强引用的语言中,堆对象可以从
程序。可达对象的集合是由程序中的类变量和方法变量的集合决定的
指向堆对象。该集合通常称为程序的根集。变量指向的对象
在程序的根集中是可以访问的。此外,对象可能是间接可达的。也就是说,一个对象是
如果有另一个可达对象指向它,则可达。来自程序根集的此类引用链
到堆对象的路径称为可达路径。一个对象可能有多个到达它的可达路径,并且有
根本没有可达路径。如果对象没有可达路径,则它被视为垃圾并且可以立即被处理
由垃圾收集器收集。

当对象不再存在强引用时,该对象将进入不可访问状态。当一个对象不可访问时,它就成为收集的候选者。请注意措辞:仅仅因为一个对象是收集的候选者并不意味着它将立即被收集。 JVM 可以自由地延迟收集,直到对象立即需要消耗内存为止。
需要注意的是,并非任何强引用都会在内存中保存对象。这些必须是从垃圾收集根链接的引用。 GC 根是一类特殊的变量,包括

(任何线程的)堆栈上的临时变量
静态变量(来自任何类)

如果您喜欢http //java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html

in the object graph when one is not linked to it, then the reference become unreachable. then then the garbage collector scans for these dangling orphan objects and swipe out regaining allocated memory.

java.lang.ref.{Phantom,Soft,Weak} Reference Enqueues unreachable objects.
If the finalizer is run, the GC already knows its unreachable.

read about finalization- http://java.sun.com/developer/technicalArticles/javase/finalization/

In a language which has only strong references, heap objects can either be reachable or unreachable from the
program. The set of reachable objects is determined by the set of class variables and method variables in the program
pointing to heap objects. This set is usually referred to as the root set of the program. An object pointed by a variable
in the root set of the program is reachable. In addition, an object might be indirectly reachable. That is, an object is
reachable if there is another reachable object pointing to it. Such chain of references from the root set of the program
to a heap object is called reachability path. An object may have more than one reachability path to it as well as have
no reachability paths at all. If the object has no reachability paths it is deemed garbage and can be immediately
collected by the garbage collector.

An object enters an unreachable state when no more strong references to it exist. When an object is unreachable, it is a candidate for collection. Note the wording: Just because an object is a candidate for collection doesn't mean it will be immediately collected. The JVM is free to delay collection until there is an immediate need for the memory being consumed by the object.
It's important to note that not just any strong reference will hold an object in memory. These must be references that chain from a garbage collection root. GC roots are a special class of variable that includes

Temporary variables on the stack (of any thread)
Static variables (from any class)
Special references from JNI native code

more if you like to http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html

半岛未凉 2024-11-08 08:45:24

无法访问的对象是没有“可访问”引用的对象。
换句话说,没有提及它。

An unreachable object, is an object that doesn't have a "reachable" reference to it.
In other words, no references to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文