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

  • Home
  • SEARCH
  • 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 8352527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:59:10+00:00 2026-06-09T08:59:10+00:00

I have a ConcurrentMap<String, SoftReference<X>> map = new ConcurrentHashMap<String, SoftReference<X>>(); and would like the

  • 0

I have a

ConcurrentMap<String, SoftReference<X>> map = new ConcurrentHashMap<String, SoftReference<X>>();

and would like the key/value pair removed from the map when the referent of the SoftReference is GC’ed.

How do I go about achieving this?

  • 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-06-09T08:59:11+00:00Added an answer on June 9, 2026 at 8:59 am

    Key/value pair will not be removed, when the referent of the SoftReference is GC’ed.
    THe following program shows the result.

    package test.gc;
    
    import java.lang.ref.Reference;
    import java.lang.ref.ReferenceQueue;
    import java.lang.ref.SoftReference;
    import java.util.Date;
    import java.util.concurrent.ConcurrentHashMap;
    
    public class TestSoftReference {
    
        public static ReferenceQueue<TestModel> referenceQueue = new ReferenceQueue<>();
        public static ConcurrentHashMap<Integer, SoftReference<TestModel>> map = new ConcurrentHashMap<>();
    
        public static void main(String[] args) throws InterruptedException {
            final int KEY1 = 1;
    
            TestModel testModel1 = new TestModel(KEY1);
            SoftReference<TestModel> reference1 = new SoftReference<TestModel>(testModel1, referenceQueue);
    
            map.put(testModel1.getNumber(), reference1);
    
            testModel1 = null;
    
            while (true) {  
                Object obj = referenceQueue.poll();  
                if (obj != null) {  
                    System.out.println("queue.poll at " + new Date() + " " + obj);
                    break;  
                }  
                System.gc();  
            }  
    
            SoftReference<TestModel> tempReference = null;
            tempReference = map.get(KEY1);
            System.err.println("reference:" + tempReference);
            if (tempReference != null) {
                System.err.println("referent:" + tempReference.get());
            }
        }
    
    }
    
    class TestModel {
    
        private int number = 0;
        private int[] bigArray = null;
    
        public int getNumber() {
            return number;
        }
    
        public TestModel(int n) {
            if (n <= 0) {
                throw new IllegalArgumentException("argument n must greater than 0.");
            } else {
                number = n;
                bigArray = new int[n * 1024 * 1024];
            }
        } 
    
        @Override
        public String toString() {
            return "field bigArray's baseNumber is " + number + " and bigArray's length is " + bigArray.length;
        }
    
    }
    

    And the result is:

        queue.poll at Thu Jul 26 14:37:21 CST 2012 java.lang.ref.SoftReference@1c501f7
        reference:java.lang.ref.SoftReference@1c501f7
        referent:null
    

    As it shows, the key/value pair is not removed, when the refent is GC’ed.

    You can implement your own RefenceQueue and SoftReference to achive your target, and override RefenceQueue’s poll method, in which you can do your own job, like this:

    class SoftReferenceMonitor extends ReferenceQueue<TestModel> {
    
        @Override
        public Reference<? extends TestModel> poll() {
            @SuppressWarnings("unchecked")
            Reference<TestModel> ref = (Reference<TestModel>) super.poll();
            if (ref != null) {
                int id = ((CustomizedSoftReference)ref).getId();
                TestSoftReference.map.remove(id);
                System.err.println("remove key/value '" + id + "' from map.");
            }
            return ref;
        }
    
    }
    
    class CustomizedSoftReference extends SoftReference<TestModel> {
    
        private int id;
    
        public int getId() {
            return id;
        }
    
        public CustomizedSoftReference(TestModel referent, ReferenceQueue<? super TestModel> q) {
            super(referent, q);
            this.id = referent.getNumber();
        }
    
    } 
    

    Then replace SoftReference and SoftReferenceMonitor with SoftReferenceMonitor and CustomizedSoftReference respectively, and run the program. The result is:

    remove key/value '1' from map.
    reference:null
    queue.poll at Thu Jul 26 14:46:54 CST 2012 test.gc.CustomizedSoftReference@4e7958
    

    Now the key/value pair has been removed.

    Besides, you can use another thread to construct the SoftReferenceMonitor instance and monitor the SoftReference.

    Hope this useful.

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

Sidebar

Related Questions

I have been using Java's ConcurrentMap for a map that can be used from
I have the following generic code: public V put(K key, V value){ Object o
Have converted devise new session from erb to Haml but doens't work, this is
Have a xml string, goal is to replace an xml element value to a
have an an array String classname[]={'a','b','c','d'}; ArrayAdapter<CharSequence> adapterClasses = new ArrayAdapter<CharSequence>( getApplicationContext(), R.layout.spinner_item_class, R.id.spinnerclasstxt,
I have a utility method to help use ConcurrentMap.putIfAbsent like this: public static <K,
I have a usecase where I have to insert a new value if the
I have an interesting problem I would like some help with. I have implemented
I have ConcurrentMap<Integer, MyObj> in my program. Can I modify MyObj if I know
If I have: var myObjects = new ConcurrentBag<object>(); And try to remove objects via:

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.