Reputation: 1111
I have two applications deployed on a Tomcat server (v7)
, both the applications use same shared library. I wanted to reduce the permgen memory foot print of these apps on JVM so I decided to take the common shared library out of the individual wars and put it in the tomcat library folder
.
However this experiment backfired as the ClassLoader
is per application context even though I moved the libraries to tomcat lib
, shared library is getting loaded into the context of both the apps, worse it is getting loaded into the context of other apps that do not use this shared library.
Is there a way to load the library only once across the contexts in the web server?
I understand the risks of thread safety and security, those risks are mitigated in my case.
Upvotes: 1
Views: 1106
Reputation: 19445
Classes from jars in the Tomcat lib directory get their own class loader which is shared amongst all deployed applications.
As you have noted each individual application also has it's own class loader.
At this point it's important to understand where Tomcat finds classes for each application. To paraphrase Apache Tomcat Class Loader HOW-TO, application classes are located in the following order:
Items 1,2 & 5 above are separate class loaders with their own repository. Items 3 & 4 are a single class loader, but there is a separate one for each web-app.
Therefore, if you have copied your library jar into the Tomcat lib directory, and not removed it from your web-app then you will still be loading and using the jar in your web-app. Double check the deployment to ensure that the library jar is in fact no longer present in the WEB-INF/lib directory of each app.
Upvotes: 3