Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I unit test GC?

For a project, we need a way to run user scripts that can come with attached JAR files with additional classes.

What are my options when I want to write a couple of tests to make sure normal script don't leave anything dangling behind?

I specifically need to know: Are all classes from the attached JARs "unloaded"?

Note: I'm not looking for the 100% super-watertight solution that works across all versions of Java from 1.0 to 7. Right now, I just need to be better than "I have no idea".

like image 663
Aaron Digulla Avatar asked Jan 09 '12 15:01

Aaron Digulla


1 Answers

The likely best option is to ensure your loaded jars are loaded by a specific class loader, and then to discard that class loader (after discarding all the objects).

As far as unit testing the unloading, if you go with this option, you need to extend your testing framework and customized class loaders to have a "create class loader on demand" flag. Then you load the class once with the flag on, discard the class loader, and attempt to load the class again with the flag off. If the class is truly not reachable, the second attempt should throw a class not found exception. You then wrap your unit tests to pass if they fall into the exception, and fail if they succeed in hitting the line after the second load attempt.

If you are disposed to use more than pure-Java tools, an OSGi container might be a consideration. Most of the established OSGi container implementations explicitly test class unloading.

like image 154
Edwin Buck Avatar answered Oct 19 '22 05:10

Edwin Buck