简体   繁体   中英

Weak hash map with Class key

Is it worth to use Class as a key in a weak hash map for caching(WeakHashMap)? As know, class object is created when program starts and is destroyed when it finishes. So, is there any sence to do it or I have some misunderstanding with this?

A Class is loaded when a class loader, loads it. If the ClassLoader is unloaded, so are its classes. A ClassLoader cannot be unloaded until all its classes can be cleaned up so using a Weak collection of Classes is a very good idea if you ever want to unload a class loader.

In a simple Java Se program you might have two or three class loaders which are provided for you and live for the life of the program and you never need think about them.

However if you have a container like Java EE or OSGi, these can load each application or each module in its own class loader, allowing them to be installed, upgraded or removed on the fly (without restarting the JVM)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM