Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6049475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:34:52+00:00 2026-05-23T07:34:52+00:00

SoftReference , WeakReference , PhantomReference may be used to customize the process of garbage

  • 0

SoftReference, WeakReference, PhantomReference may be used to customize the process of garbage collection. All of them extend Reference<T> therefore it is possible to mix them in single collection. Hard references (most common ones) do no extend Reference<T> therefore it is not possible to mix hard and other types of references in one collection. Am I right and we should put CustomReference<T> extends Reference<T> to the collection in order to achieve the desired result of mixing all types of object links in single collection (Collection<Reference<T>>)?

UPDATE: So when writing SSCCE I’ve found that it is not possible to extend Reference<T> in a usual way (constructor is package-local).

So the question now updates to the following: can I with single collection class create cache which always holds some objects (say 10) and the others are reclaimed by GC when memory not allows? Is there any other means to do this except providing custom wrappers for hard and soft references and storing them in the collection?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:34:53+00:00Added an answer on May 23, 2026 at 7:34 am

    Unfortunately Reference<T> most not (and can not) be subclassed directly, according to its JavaDoc:

    Because reference objects are implemented in close cooperation with the garbage collector, this class may not be subclassed directly.

    As such you won’t be able to easily (i.e. without ugly instanceof + casting) handle both soft/weak/phantom references and normal references in the same Collection.

    You could write a wrapper that either uses two separate Collection objects to handle normal and soft/weak/phantom references or that puts them all into the same Collection<Object> and uses the appropriate instanceof checks with casts to differentiate the objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the difference between java.lang.ref.WeakReference and java.lang.ref.SoftReference ?
I am familiar with WeakReference , but I am looking for a reference type
It seams that Dalvik's garbage collector doesn't respect SoftReferences and removes them as soon
I know there is a WeakHashMap in java.util , but since it uses WeakReference
What is a use case for a soft reference in Java? Would it be
As lots of people do, I use Map and SoftReference to create image cache
I'm trying to extend the Clojure language to extend ACI-guaranteed refs to ACID-guaranteed drefs
If I store bitmaps in a hashmap using SoftReference, will SoftReference call .recycle() on
I'm implementing a manager in my web app that can be called to set
I am using a search library which advises keeping search handle object open for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.