Have you ever used PhantomReference in any project?

后端 未结 9 474
再見小時候
再見小時候 2020-11-29 17:02

The only thing I know about PhantomReference is,

  • If you use its get() method, it will always return null
9条回答
  •  鱼传尺愫
    2020-11-29 17:58

    Great explanation of Phantom Reference usage:

    Phantom references are safe way to know an object has been removed from memory. For instance, consider an application that deals with large images. Suppose that we want to load a big image in to memory when large image is already in memory which is ready for garbage collected. In such case, we want to wait until the old image is collected before loading a new one. Here, the phantom reference is flexible and safely option to choose. The reference of the old image will be enqueued in the ReferenceQueue once the old image object is finalized. After receiving that reference, we can load the new image in to memory.

提交回复
热议问题