Have you ever used PhantomReference in any project?

后端 未结 9 465
再見小時候
再見小時候 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:52

    A general diced-up table explanation, from the Java Glossary.

    Which of course coincides with the PhantomReference documentation:

    Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.

    And last but not least, all the gory details (this is a good read): Java Reference Objects (or How I Learned to Stop Worrying and Love OutOfMemoryError).

    Happy coding. (But to answer the question, I've only ever used WeakReferences.)

提交回复
热议问题