Tomcat+Reload

最近开发一个东西,借鉴一下Tomcat的热部署,但首先得避免Class不能卸载,找到几篇文章:

 

YouKit

 

http://zhwj184.iteye.com/blog/764575

 

To unload a class you have to create a custom classloader and load the class using it. Tomcat does it and so does JRun. You can look in Tomcat code for an example.

After you are done with the class you need to release all references to the class as well as to the class loader by reassigning the variables or setting them to null.

Then either wait for System.gc() to unload the class or you call it directly in a loop till no more bytes can be freed. however normally calling it twice does the trick.

Note: You cannot unload a single class. You have to unload the classloader along with it. So obviously System classloader is not the suitable for this task.

Note 2: This is how JSP pages are reloaded dynamically everytime you change the code. And yes that is why first time takes much longer to load then subsequent times.

 

 

 

 

posted @ 2012-08-28 11:21  规格严格-功夫到家  阅读(329)  评论(0编辑  收藏  举报