Phantom Referenced Objects

前端 未结 6 1560
星月不相逢
星月不相逢 2021-02-15 19:49

Phantom References serve for post-mortem operations. The Java specification states that a phantom referenced object will not be deallocated until the phantom-re

6条回答
  •  梦谈多话
    2021-02-15 20:11

    The only good use-case I can think of, that would prevent deallocation, is one where some kind of JNI-implemented asynchronous data source is writing into the referenced object, and must be told to stand down - to stop writing into the object - before the memory is recycled. If prior deallocation were allowed, a simple forgot-to-dispose() bug could result in memory corruption.

    This is one of the cases where finalize() would have been used in the past, and probably drove some of its quirks.

提交回复
热议问题