Friday, December 21, 2018

Java references

In java we have 4 types of references
  • Strong
  • Weak
  • Soft
  • Phantom
Soft vs Weak vs Phantom References
TypePurposeUseWhen GCed
Strong An ordinary reference. Keeps objects alive as long as they are referenced.normal reference.Any object not pointed to can be reclaimed.
Soft Keeps objects alive provided there’s enough memory.to keep objects alive even after clients have removed their references (memory-sensitive caches), in case clients start asking for them again by key.After a first gc pass, the JVM decides it still needs to reclaim more space.
Weak Keeps objects alive only while they’re in use (reachable) by clients.Containers that automatically delete objects no longer in use.After gc determines the object is only weakly reachable
Phantom Lets you clean up after finalization but before the space is reclaimed (replaces or augments the use of finalize())Special clean up processingAfter finalization.

Articles to read

Baeldung :  weak, soft, phantom
Kdgregory : java references

No comments:

Post a Comment

Thank you for your comment!