12 Replies Latest reply: Nov 11, 2011 4:15 PM by user514049 RSS

    WeakHashMap, cleanup and API level guarantees

    user514049
      When does WeakHashMap groom/cleanup the entries i.e. remove the WeakReferences from the Map? It appears it does it when the WeakHashMap is accessed. In other words, if for some reason the WeakHashMap is NOT accessed, the entries continue to stay as WeakReferences.

      Is there an API level guarantee as to how the cleanup works? I do not see it documented in WeakHashMap API.

      "...
      A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently from other Map implementations.
      ..."

      Again, if there is any reference on how and when this -- An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. -- happens, it will be useful.

      With regards,
      M.P. Ardhanareeswaran
        • 1. Re: WeakHashMap, cleanup and API level guarantees
          796440
          There's no guarantee as to when it will be removed, only that when it's not referenced outside of the map, it can be. All they're saying is the following:
          Key k = ...
          Value v = ...;
          weakHahshMap.pt(k, v);
          ...
          k = something_else; // now it can be removed at any time
          Assuming k is the only reference to that Key object other than the map, then as soon as k is no longer referencing the object, the WR is eligible to be discarded and the entry can be removed from the map. For more details about a WeakReference's cleanup, see WeakReference's javadocs.
          • 2. Re: WeakHashMap, cleanup and API level guarantees
            user514049
            I am not very familiar what can be changed in a JDK. Can different vendors choose a different strategy for grooming?
            • 3. Re: WeakHashMap, cleanup and API level guarantees
              796440
              user514049 wrote:
              I am not very familiar what can be changed in a JDK. Can different vendors choose a different strategy for grooming?
              Yes. That's an implementation detail that can vary between any two API implementations (in terms of how the Reference classes are implemented) and between any two JVM implementations (in terms of how GC deals with the References). The JVM/GC implementation is where the main differences will show up.
              • 4. Re: WeakHashMap, cleanup and API level guarantees
                user514049
                I want to make sure that I conveyed my concern correctly in the last post.

                Here is a version of the code for WeakHashMap which I found on the web.


                /* Remove all invalidated entries from the map, that is, remove all entries
                whose keys have been discarded. This method should be invoked once by
                each public mutator in this class. We don't invoke this method in
                public accessors because that can lead to surprising
                ConcurrentModificationExceptions. */
                private void processQueue() {
                     WeakKey wk;
                     while ((wk = (WeakKey)queue.poll()) != null) {
                     hash.remove(wk);
                     }
                }

                This code grooms the WeakHashMap(hash.remove(wk) ). processQueue is called from all the mutators.

                I was wondering if this implementation can be different for different vendors and/or can be changed from version to version -- say Java 6 to Java 7.
                • 5. Re: WeakHashMap, cleanup and API level guarantees
                  796440
                  user514049 wrote:
                  I was wondering if this implementation can be different for different vendors and/or can be changed from version to version -- say Java 6 to Java 7.
                  Of course it can. As long as it still conforms to the docs, it can be implemented however they want. Why do you care? If it's more than just curiosity, then WHM is probably not the right tool for whatever you're trying to accomplish.
                  • 6. Re: WeakHashMap, cleanup and API level guarantees
                    user514049
                    This is more than casual curiosity.

                    WeakHashMap is actually working for me by "accident". In my case, I am storing some PhantomReferences in this Map as values (and not keys) for cleanup. There is a quiet period before the application is torn down during which there are no updates to the WeakHashMap. My application is able to retrieve the PhantomReferences at close time and clean them up.

                    It appears that it is "not advisable" to rely on this behavior.
                    • 7. Re: WeakHashMap, cleanup and API level guarantees
                      796440
                      user514049 wrote:
                      This is more than casual curiosity.

                      WeakHashMap is actually working for me by "accident". In my case, I am storing some PhantomReferences in this Map as values (and not keys) for cleanup. There is a quiet period before the application is torn down during which there are no updates to the WeakHashMap. My application is able to retrieve the PhantomReferences at close time and clean them up.

                      It appears that it is "not advisable" to rely on this behavior.
                      Not sure exactly what you mean here. Are the keys referenced anywhere else, or only in the map? If only in the map, then the entries can be removed from the map and the keys GCed at any time. If you need to know that you'll have access to some entry at some point in your code, then you'll have to make sure that there's a reference to the key outside of the map.

                      If "by accident" you mean that, so far, the entries are always there even without you maintaining other references to the keys, then, yes, that is just happenstance, and not to be relied on.

                      To quote the third sentence of WHM's javadoc (emphasis mine): "More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed."

                      Edited by: jverd on Nov 11, 2011 12:46 PM
                      • 8. Re: WeakHashMap, cleanup and API level guarantees
                        user514049
                        I meant this -- If "by accident" you mean that, so far, the entries are always there even without you maintaining other references to the keys, then, yes, that is just happenstance, and not to be relied on.

                        It is a bug waiting to happen :-(
                        • 9. Re: WeakHashMap, cleanup and API level guarantees
                          796440
                          user514049 wrote:
                          I meant this -- If "by accident" you mean that, so far, the entries are always there even without you maintaining other references to the keys, then, yes, that is just happenstance, and not to be relied on.

                          It is a bug waiting to happen :-(
                          Bug in your code? Yes. It means you're using WHM the wrong way.

                          Or did you mean bug in WHM? If so, then no, it's not a bug; it's how WHM is intended to work.

                          Let me ask you this: If you don't want entries to be removed behind your back, why did you choose WHM in the first place? What functionality are you trying to achieve?
                          • 10. Re: WeakHashMap, cleanup and API level guarantees
                            user514049
                            This is a bug waiting to happen in MY code.

                            I knew going in that the references can be removed from underneath me. It was surprising to see that they were "NOT" being removed if the mutators are NOT accessed.  That I had to look in the code to figure out that these references won't be removed unless the mutators are accessed is what "bothered" me.
                            • 11. Re: WeakHashMap, cleanup and API level guarantees
                              796440
                              user514049 wrote:
                              This is a bug waiting to happen in MY code.

                              I knew going in that the references can be removed from underneath me. It was surprising to see that they were "NOT" being removed if the mutators are NOT accessed.  That I had to look in the code to figure out that these references won't be removed unless the mutators are accessed is what "bothered" me.
                              Oh, I see.

                              Maybe.

                              So, the problem that you're having is that you're counting on them being removed, but that, specific-implementation-code-spelunking notwithstanding, you can't rely on them being removed?

                              Or am I still missing something?
                              • 12. Re: WeakHashMap, cleanup and API level guarantees
                                user514049
                                Yes.

                                Thanks for your indulgence.