This discussion is archived
6 Replies Latest reply: Aug 26, 2013 2:47 AM by so*797109*as RSS

How to make Weakrefernce in Concurrent HashMap

so*797109*as Newbie
Currently Being Moderated

Hi All,

    In my Program I have a scenario, where Mutiple Thread will operate (put and get) on a single Map. Like Thread a using put a key/value in the map and the same time another thread is retrieving the key/value from the map for the same or different key. Now if I make my map synchronize then it wil be a big performance issue, so I decided to move to ConcurrentHashMap. During this scenario, I have another complexity that after certain period of time(not known to my application) few of the key/value pairs are not needed in my application, so I need to remove the same(Garbage Collection) to free memory. Though my programm doesn't have the idea that which key/values are not needed I thought to use weakrefernce. So, after ceratin period of time if the key(String) is not reachable it will be automatically garbage collected. I know the process to do the same in WeakhashMap but doesn't know the process to do it in ConcurrentHashMap. So, can anyone tell me how to do a weakrefernce in ConcurrentHashMap. Or else is there any other way to achieve the above scenario?

  • 1. Re: How to make Weakrefernce in Concurrent HashMap
    rp0428 Guru
    Currently Being Moderated

    You seem to be making a LOT of assumptions.

    if I make my map synchronize then it wil be a big performance issue

    What testing have you done that supports that statement? Please post details about the 'big' performance issue that you discovered.

    I have another complexity that after certain period of time(not known to my application) few of the key/value pairs are not needed in my application

    Does that make any sense to you? How can YOU know that 'few of the key/value pairs are not needed' if your app doesn't know it?

     

    And if you or your app KNEW that a few pairs were NOT needed then there is NOTHING that you need to do; just leave them in the map. They won't hurt anything and they won't be referenced since, by definition, 'not needed' means 'not referenced'.

     

    Please explain why there is ANY need to remove those 'few pairs from the map.

     

    I suggest you try to avoid CTD (compulsive tuning disorder) and focus on REAL problems that have a REAL impact on your application.

  • 2. Re: How to make Weakrefernce in Concurrent HashMap
    jtahlborn Expert
    Currently Being Moderated

    Instead of trying to roll your own, possibly complex implementation, why not use a pre-existing, well-tested implementation like http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/MapMaker.html

  • 3. Re: How to make Weakrefernce in Concurrent HashMap
    so*797109*as Newbie
    Currently Being Moderated

    What testing have you done that supports that statement? Please post details about the 'big' performance issue that you discovered.

    It's a normal scenario that when using sychnronize then multiple thread will not be able to access it concurrently, means one thread need to wait for other thread to complete. So, when you have near 50 thread(In my scenario) accessing the map it will be some performance issue.

     

    And if you or your app KNEW that a few pairs were NOT needed then there is NOTHING that you need to do; just leave them in the map. They won't hurt anything and they won't be referenced since, by definition, 'not needed' means 'not referenced'.

    If I leave the key(String) to the map, then it will occupy some memory and though the Map is in strong reference the keys will not be garbage collected. In long run it can cause memory error.

  • 4. Re: How to make Weakrefernce in Concurrent HashMap
    so*797109*as Newbie
    Currently Being Moderated

    For Map maker below scenario will not work bacuase for weakKeys() it will check for object identity instead of value.

     

        ConcurrentMap<String, String> weakHashMap = new MapMaker()
                .concurrencyLevel(4)
                .weakKeys()
                .makeMap();
        weakHashMap.put(keyHashMap, "helloWeakHash");
        System.out.println("Before:  weak hash map value:" + weakHashMap.get("keyHashMap"));
        keyHashMap = null;
        System.gc();
        System.out.println("After:  weak hash map value:" + weakHashMap.get("keyHashMap"));
  • 5. Re: How to make Weakrefernce in Concurrent HashMap
    rp0428 Guru
    Currently Being Moderated

    It's a normal scenario that when using sychnronize then multiple thread will not be able to access it concurrently, means one thread need to wait for other thread to complete. So, when you have near 50 thread(In my scenario) accessing the map it will be some performance issue.

    You still haven't addressed what I ask:

    What testing have you done that supports that statement? Please post details about the 'big' performance issue that you discovered.

    Have you done ANY testing at all using synchronization? Has ANY of that testing shown a performance issue?

    If I leave the key(String) to the map, then it will occupy some memory and though the Map is in strong reference the keys will not be garbage collected. In long run it can cause memory error.

    You are just making more assumptions and not really basing your expectations on any real facts about your use case.

     

    You need to base your architecture and design on FACTS not on assumptions.

     

    How many 'unused keys' are you even talking about?

    What type of 'keys' and 'values' are you talking about?

    What is the size of those objects?

    Over what period of time?

    What is the access pattern of the threads?

     

    It's ok to be proactive and try to anticipate issues that MIGHT occur.

     

    But if you identify such an issue that mandates the creation of a substantial new object class that should be a red flag.

     

    That red flag means that your design likely has a major flaw somewhere and you should design and conduct the proper tests to validate your hypotheses about the problem that you think 'might' occur.

     

    The reality is that even if your assumptions are correct and you need to write your new class you STILL need to:

    1. design and write the new class

    2. design and conduct proper tests to make sure that new class really does work properly

    3. design and conduct proper tests to make sure your app does NOT have the problems you anticipate

     

    Look CAREFULLY at the above list.

     

    Step #3 has to be be done anyway. That is why you should just do it first to VALIDATE that a problem even exists.

     

    Because if you cannot cause your app to produce a problem then the new class, and the significant work of steps #1 and #2, is not even needed.

     

    And if you CAN cause your app to produce a problem during the testing then the tests will help you design the code to solve the problem AND you can use the same set of tests for step #3 (meaning creating the tests was NOT wasted effort).

     

    The conclusion is what I said to begin with: you need to test that you actually have a problem to solve BEFORE you begin exploring options to solve that problem.

  • 6. Re: How to make Weakrefernce in Concurrent HashMap
    so*797109*as Newbie
    Currently Being Moderated

    Hi Can you suggest a tool to check the performance.

     

    Right now I am using concurrentHashMap but for the key of the map, I am usng Weak Reference

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points