I have been asked this question several times. Mi è stato chiesto a questa domanda più volte. Recently Xyling asked the same question in his blog. Xyling recentemente la stessa domanda nel suo blog. So I thought a simple explanation may be in order. Così ho pensato una semplice spiegazione può essere in ordine.

To unload a class you have to create a custom classloader and load the class using it. Per scaricare una classe si deve creare un classloader personalizzato e caricare la classe di utilizzarlo. Tomcat does it and so does JRun. Tomcat e lo fa in modo non JRun. You can look in Tomcat code for an example. È possibile osservare in Tomcat codice per un esempio.

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. Dopo aver finito con la classe è necessario liberare tutti i riferimenti alla classe e alla classe del caricatore di riassegnazione delle variabili o di regolazione a nulla.

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. Poi o attendere System.gc () per scaricare la classe o di chiamare direttamente in un ciclo fino a più byte non possano essere liberati. however normally calling it twice does the trick. tuttavia normalmente chiamata due volte il trucco.

Note: You cannot unload a single class. Nota: non è possibile scaricare una singola classe. You have to unload the classloader along with it. Dovete scaricare il classloader con essa. So obviously System classloader is not the suitable for this task. Così, ovviamente, classloader di sistema non è adatto per questo compito.

Note 2: This is how JSP pages are reloaded dynamically everytime you change the code. Nota 2: Questo è il modo in pagine JSP sono ricaricati dinamicamente ogni volta che modifichi il codice. And yes that is why first time takes much longer to load then subsequent times. E sì che è il motivo per cui prima volta richiede molto più tempo per caricare poi le successive volte.