How to get the JVM to clear all WeakReferences / keys to WeakHashMap

Is there a way to get jvm to clear all WeakReferences (or all keys from WeakHashMap ) if they no longer reference regular links?

Running garbage collection will not work, weak links remain alive. (I read that weak links will only be cleared if the memory is low).

+4
source share
1 answer

SoftReferences are deleted when memory is low.

WeakReferences are cleared more often (for example, all the time when the object has only weak links)

Check out the extraclass Java document for the package (package documentation is useful for explanation): http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ref/package-summary.html

Can you specify the exact message that you see with Tomcat at shutdown? Are you sure you are not mistaken? For Tomcat, it’s important that there are no hard links that bind ClassLoader to ServletContext.

Re WeakHashMap: http://download.oracle.com/javase/1.5.0/docs/api/java/util/WeakHashMap.html Only keys are weakly held. What do you use as a "value"? Does the value self-determine its "key"? In this regard, I wonder what WeakHashMap gives you (this assumes that this is the right tool for the job).

In my experience, you can get WeakReferences with Sun JVM 5, which will be cleared using System.gc (), 100% reliable for me when I first start GC. But they do not guarantee this.

+1
source

All Articles