Phantom Referenced Objects

前端 未结 6 1024
悲哀的现实
悲哀的现实 2021-02-15 20:04

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:17

    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.

提交回复
热议问题