Dynamic class loading/unloading
By user9157252 on Oct 17, 2006
The problem I had is that I only have one base station SPOT. But I wrote the GPS SPOT and also a Remote Controlled Camera SPOT application (not blogged yet). Both of them require a base station in order to bridge the Internet/802.15.4 divide. But remember, I only have one base station.
So my first solution was just to combine the two base station applications into one. But I could see that such a solution was soon going to lose its appeal as the number of applications I needed to run grew. Also, I could not debug one application, or update one of them, without killing the other, at least temporarily. Hmmmm ... how to share the base station in a way that would allow applications to be added and deleted without affecting other applications.
I know this problem has been addressed on other areas, like application servers, but I needed to do it in a very small footprint.
And I needed to do it on Sun SPOTs.
So I began looking in to the whole thing. And it turns out that I could find very little information on how to do what I wanted to do. Writing a class loader to load classes is pretty easy. Actually unloading classes is a tad harder. Also, as I found, there are some cool flags to the VM that are not well documented, but that can show you what, exactly, and in excruciating detail, the class loader and garbage collector is doing. This is very useful information, it turns out.
-verbose:gcWill give you fairly detailed information about what the Garbage Collector is actually doing, but I really needed information on what the ClassLoader was up to. I found these flags:
-XX:+TraceClassLoadingto be hugely helpful in that regard. I could watch classes being loaded and unloaded, so I knew when I had successfully managed to completely nuke a particular class.
I now have a Base Station application that I can dynamically load and unload base station applications into -- though they are not truly 'applications' but rather just threads in an existing application -- which has proven to be hugely helpful to me in running multiple, unrelated base station applications from my one measly base station.
I tend not to post actual code here, but I can, if I get enough interest.
[There's no room in the drug world for amateurs. ]