I have been asked this question several times.有人問我這個問題的幾倍。 Recently Xyling asked the same question in his blog.最近xyling提出同樣的問題在他的博客。 So I thought a simple explanation may be in order.所以我認為一個簡單的解釋,可能會在秩序。

To unload a class you have to create a custom classloader and load the class using it.卸下一類,你必須創建一個用戶自定義的類和負載的階級使用它。 Tomcat does it and so does JRun.雄貓是否和那麼jrun 。 You can look in Tomcat code for an example.你可以看看在Tomcat代碼的一個例子。

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.之後,你所做的與一流的,你需要釋放所有的提述,階級以及為類加載器指派的變量或設置他們為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.然後等待system.gc ( )卸下階級或您呼叫它直接在循環中,直至沒有更多的字節可以被釋放。 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.注2 :這是如何JSP頁面是重裝上陣動態每次您變更代碼。 And yes that is why first time takes much longer to load then subsequent times.是的這就是為什麼第一次需要的時間要長得多的負載,然後以後的時代。