论坛首页 Java企业应用论坛

深入理解java的finalize

浏览 70865 次
精华帖 (0) :: 良好帖 (9) :: 新手帖 (12) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-10-16  
额  有点混
楼主的意思是被标记为Finalized之后就不会把它再放入F-Queue了吧?
上面说的是"finalizer-reachable 是除了reachable外,从F-Queue可以通过引用到达的对象。"但finalizer-reachable状态并不是指就在F-Queue的,比如finalizer-reachable+Finalized。楼主这样理解可对?
0 请登录后投票
   发表时间:2009-10-16  
楼主我想问下第一行第二第三个图,就是Reachable+Finalizable怎么变到Reachable+Finalized的?
0 请登录后投票
   发表时间:2009-10-16  
patrickyao1988 写道
额  有点混
楼主的意思是被标记为Finalized之后就不会把它再放入F-Queue了吧?
上面说的是"finalizer-reachable 是除了reachable外,从F-Queue可以通过引用到达的对象。"但finalizer-reachable状态并不是指就在F-Queue的,比如finalizer-reachable+Finalized。楼主这样理解可对?

0 请登录后投票
   发表时间:2009-10-16  
patrickyao1988 写道
楼主我想问下第一行第二第三个图,就是Reachable+Finalizable怎么变到Reachable+Finalized的?


考虑如下场景:
A有一个B的引用。然后A变成垃圾,有待回收。在GC的作用下,A和B被加入F-QUEUE。
A和B都是F-Reachable+Finalizable。
当回收A时,A从F-Reachable+Finalizable变到Reachable+Finalized。但是B此时还是在F-QUEUE的,B的状态变成Reachable+Finalizable(因为A的状态为Reachable)。
以后GC回收B的时候B的状态就从Reachable+Finalizable变为Reachable+Finalized。
0 请登录后投票
   发表时间:2009-10-16   最后修改:2009-10-16
zhang_xzhi_xjtu 写道

考虑如下场景:
A有一个B的引用。然后A变成垃圾,有待回收。在GC的作用下,A和B被加入F-QUEUE。

这种场景下,B在其他地方应该没有强引用吧?
zhang_xzhi_xjtu 写道

A和B都是F-Reachable+Finalizable。
当回收A时,A从F-Reachable+Finalizable变到Reachable+Finalized。但是B此时还是在F-QUEUE的,B的状态变成Reachable+Finalizable(因为A的状态为Reachable)。

这样的话,A是经历了F-Reachable+Finalizable,得以从Reachable+Finalizable发展到Reachable+Finalized的吧,那最后那个B
zhang_xzhi_xjtu 写道

以后GC回收B的时候B的状态就从Reachable+Finalizable变为Reachable+Finalized。

是不是也要经历F-Reachable+Finalizable这个状态,直接从这两个状态切换可以吗?
麻烦楼主了!感激不尽
0 请登录后投票
   发表时间:2009-10-19  
patrickyao1988 写道
zhang_xzhi_xjtu 写道

考虑如下场景:
A有一个B的引用。然后A变成垃圾,有待回收。在GC的作用下,A和B被加入F-QUEUE。

这种场景下,B在其他地方应该没有强引用吧?

恩。

zhang_xzhi_xjtu 写道

A和B都是F-Reachable+Finalizable。
当回收A时,A从F-Reachable+Finalizable变到Reachable+Finalized。但是B此时还是在F-QUEUE的,B的状态变成Reachable+Finalizable(因为A的状态为Reachable)。

这样的话,A是经历了F-Reachable+Finalizable,得以从Reachable+Finalizable发展到Reachable+Finalized的吧,那最后那个B
zhang_xzhi_xjtu 写道

以后GC回收B的时候B的状态就从Reachable+Finalizable变为Reachable+Finalized。

是不是也要经历F-Reachable+Finalizable这个状态,直接从这两个状态切换可以吗?
麻烦楼主了!感激不尽


B刚开始是在F-Reachable+Finalizable,然后因为A的关系跳到Reachable+Finalizable,GC对其回收的时候跳到Reachable+Finalized。
0 请登录后投票
   发表时间:2009-10-20  
呵呵,不错,看另一个解释
http://msdn.microsoft.com/en-us/library/aa244021(VS.60).aspx

When an object is first created (A), it is reachable and unfinalized.

As references to an object are discarded during program execution, an object that was reachable may become finalizer-reachable (B, C, D) or unreachable (E, F). (Note that a finalizer-reachable object never becomes unreachable directly; it becomes reachable when the finalizer from which it can be reached is invoked, as explained below.)

If the Java Virtual Machine detects that an unfinalized object has become finalizer-reachable or unreachable, it may label the object finalizable (G, H); moreover, if the object was unreachable, it becomes finalizer-reachable (H).

If the Java Virtual Machine detects that a finalized object has become unreachable, it may reclaim the storage occupied by the object because the object will never again become reachable (I).

At any time, a Java Virtual Machine may take any finalizable object, label it finalized, and then invoke its finalize method in some thread. This causes the object to become finalized and reachable (J, K), and it also may cause other objects that were finalizer-reachable to become reachable again (L, M, N).

A finalizable object cannot also be unreachable; it can be reached because its finalizer may eventually be invoked, whereupon the thread running the finalizer will have access to the object, as this (§15.7.2). Thus, there are actually only eight possible states for an object.

After an object has been finalized, no further action is taken until the automatic storage management determines that it is unreachable. Because of the way that an object progresses from the unfinalized state through the finalizable state to the finalized state, the finalize method is never automatically invoked more than once by a Java Virtual Machine for each object, even if the object is again made reachable after it has been finalized.

Explicit invocation of a finalizer ignores the current state of the object and does not change the state of the object from unfinalized or finalizable to finalized.

If a class does not override method finalize of class Object (or overrides it in only a trivial way, as described above), then if instances of such as class become unreachable, they may be discarded immediately rather than made to await a second determination that they have become unreachable. This strategy is indicated by the dashed arrow (O) in the transition diagram.

Java programmers should also be aware that a finalizer can be automatically invoked, even though it is reachable, during finalization-on-exit (§12.9); moreover, a finalizer can also be invoked explicitly as an ordinary method. Therefore, we recommend that the design of finalize methods be kept simple and that they be programmed defensively, so that they will work in all cases.
0 请登录后投票
   发表时间:2009-12-10  
javaeye高手众多啊,此贴我认为不错,但是投新手的多
0 请登录后投票
   发表时间:2009-12-10  
确实写的很详细了,我明天再看一遍,争取把握住了
0 请登录后投票
   发表时间:2009-12-11  
谢谢各位捧场
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics