Thanks Jeff for looking into it and giving a nice explanation.
have you actually measured and found that it's consuming so much as to be a problem?
Yes, we have seen the application throwing OOM error if it runs for long time. We are debugging on what classes are not getting GC and what else can be done to reduce the heap space consumed by the application.
Yes, you are right that it should not be a concern since its not consuming much of the heap space. However, I could see that there are many configuration files that are being read and kept in the run time memory for the duration application runs (As they are stored in HashMap, they would never be GC).
I am just looking for a better caching mechanism which could be used here.
Also, there is one more issue:
We have identified a problem with Apache log4j library used in our application - 1.2 Million instances of log4j Logger object were seen that seems too many!
On debugging, we found that there is a HashMap <
String, LoggerObjectInstance> - " looks the Culprit for 1.2M instances", when we need a logger object, first check if it's in the HashMap. If the required object isn't there, create a new instance of it and put it into the HashMap. This way, the next time we need it, it's right where we left it.
The tricky part is the cleanup. At some point, the application needs to go through the HashMap and clean up the objects that haven't been used for a while and are wasting memory.
Where do we put this cleanup code? How often do we call it? These are problematic issues.
Would using WeakHashMap solve it?