Dynamic class loading/unloading

Ok, sol I'm no Java guru by any means. Not by a long shot. But I have been doing a lot of Java programming lately. A lot. And some of it is actually starting to get rather complicated. As an example ... the Sun SPOT kits (and no, they are not available yet, sorry) come with a base station. The base station is a computer-conected SPOT with no sensor board or battery, used mainly as a bridge between the laptop/desktop world (and hence the Internet) and the world of Sun SPOTs (the IEEE 802.15.4 world).

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:gc
Will 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:+TraceClassLoading

-XX:+TraceClassUnloading

to 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. ]

Comments:

Post a Comment:
Comments are closed for this entry.
About

user9157252

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today