I have created a REST service in which I am loading the JDBC driver jar at runtime. Below is the code which does this:
Driver driverInstance;
URLClassLoader driverClassLoader;
driverClassLoader = new URLClassLoader (new URL[] { "c:/mysql.jar" }, System.class.getClassLoader());
Class<?> driverClass = driverClassLoader.loadClass("oracle.jdbc.driver.OracleDriver");
Connection conn = driverInstance.connect(connectionString, userDbCredentials);
After using this connection to load data into database, I am closing the connection and driverClassLoader both. But after running it for some time, I am getting java.lang.OutOfMemoryError: PermGen space in tomcat server.
I took the heap dump, and open it using eclipse Memory Analyzer and I found that the below Leak suspect message: 13 instances of "org.apache.catalina.loader.WebappClassLoader", loaded by "java.net.URLClassLoader @ 0xc155fc10" occupy 14,083,776 (32.21%) bytes
I understand that it is due to loading of driver jar at runtime but I am unable to find a solution to it.