What’s the difference between SoftReference and WeakReference in Java?

Technology CommunityCategory: JavaWhat’s the difference between SoftReference and WeakReference in Java?
VietMX Staff asked 3 years ago
  • A Strong reference is a normal reference that protects the referred object from collection by GC. i.e. Never garbage collects.
  • A Soft reference is eligible for collection by garbage collector, but probably won’t be collected until its memory is needed. i.e. garbage collects before OutOfMemoryError.
  • A Weak reference is a reference that does not protect a referenced object from collection by GC. i.e. garbage collects when no Strong or Soft refs.
  • A Phantom reference is a reference to an object is phantomly referenced after it has been finalized, but before its allocated memory has been reclaimed.