Weak hash map with class key

Is it worth using the class as a key in a weak hash map for caching (WeakHashMap)? As you know, a class object is created when a program starts and is destroyed when it ends. So, is there any sense for this, or do I have some kind of misunderstanding?

+4
source share
1 answer

A class is loaded when the class loader loads it. If ClassLoader is unloaded, then its classes. ClassLoader cannot be unloaded until all its classes have been cleared, so using a weak collection of classes is a very good idea if you ever want to offload the class loader.

In a simple Java Se program, you can have two or three classes of bootloaders that are provided to you and live throughout the life of the program, and you never need to think about them.

However, if you have a container, such as Java EE or OSGi, they can load each application or each module into their own class loader, which allows them to install, update or remove on the fly (without restarting the JVM).

+5
source

Source: https://habr.com/ru/post/1436090/


All Articles