Home arrow static arrow Java Programming [Archive] - Unload class (to free static references)?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Unload class (to free static references)?
This topic has 12 replies on 1 page.

Posts:3,369
Registered: 24.10.97
Unload class (to free static references)?  
Jul 30, 2004 2:11 AM



 
My problem: apache fop has a bug that it has a static Map field in a class that increases constantly. Is it possible to "unload" this class to ensure freeing up the references from this static field? And subsequent calls would reload the class when needed?
 

Posts:5,904
Registered: 04/03/99
Re: Unload class (to free static references)?  
Jul 30, 2004 2:19 AM (reply 1 of 12)



 

Classes are only unloaded when the classloader which loaded them is itself garbage collected. You therefore need to load fop via a new classloader instance, and free this when you're done. Note that classes loaded by different classloaders inhabit different namespaces, so find and read a good book/article on them before proceeding (unless you want to experience near-terminal confusion).

Dave.
 

Posts:14,142
Registered: 99-04-02
Re: Unload class (to free static references)?  
Jul 30, 2004 7:50 AM (reply 2 of 12)



 
it would be easier probably to get the source and fix the bug
 

Posts:441
Registered: 2/25/04
Re: Unload class (to free static references)?  
Jul 30, 2004 8:22 AM (reply 3 of 12)



 
Another option (if you don't want to have your own FOP build) may be to use reflection to clear the map.

Pete
 

Posts:3,369
Registered: 24.10.97
Re: Unload class (to free static references)?  
Aug 2, 2004 1:24 AM (reply 4 of 12)



 
Reflection is a good hint - thank you! However, there's another error in FOP that some image InputStreams are not closed after usage. Is it also possible to close them via reflection? Those streams often are created within a method and there's no reference (variable) to it outside the method (e.g. in the class or super class)?
 

Posts:5,904
Registered: 04/03/99
Re: Unload class (to free static references)?  
Aug 2, 2004 1:49 AM (reply 5 of 12)



 

Yes, it may be possible.

However, in the circumstances, have you considered obtaining and fixing the source code ? It's likely to be easier to fix the bug "properly" in the original code, than to mess around using reflection.

Dave.
 

Posts:3,369
Registered: 24.10.97
Re: Unload class (to free static references)?  
Aug 2, 2004 2:04 AM (reply 6 of 12)



 
i try to avoid to fix the bug in the fop code. Last year, i did this with the JFreeChart library. It ended up changing my fixes in all following versions so i�that i just gave up and am stuck now with one custom version. I still want to be able to upgrade to the next version without changing these new versions again. it's easier to write a workaround in my code and just remove it when the new version is finally fixing this.
 

Posts:5,904
Registered: 04/03/99
Re: Unload class (to free static references)?  
Aug 2, 2004 2:08 AM (reply 7 of 12)



 
I still want to be able to upgrade to the
next version without changing these new versions
again.

If you submit your bugfix to those working on the library, you shouldn't have to do this. That's pretty much the point of open source software !

Dave.
 

Posts:2,830
Registered: 9/1/03
Re: Unload class (to free static references)?  
Aug 2, 2004 2:11 AM (reply 8 of 12)



 
why not contact apache ? see if you can fix it from the inside :) they
might appreciate having someone help them fix it, review the problem
and suggest the easiest way to fix it, and maybe they will implement it.
 

Posts:2,830
Registered: 9/1/03
Re: Unload class (to free static references)?  
Aug 2, 2004 2:12 AM (reply 9 of 12)



 
oops, too slow :)
 

Posts:3,369
Registered: 24.10.97
Re: Unload class (to free static references)?  
Aug 2, 2004 3:57 AM (reply 10 of 12)



 
I also posted bug reports to bugzilla. the bugfixes/changes i posted to JFreeCHart (also opensource) didn't get fixed so far. These FOP bugs were already posted by others, but FOP doesn't seem to release another (bugfixed) version. They seem to concentrate on a "new, rewritten" version of FOP (since last year ...). In the end, you can't depend on others to fix the bugs. So, I'm looking for a workaround that i can realize with my code.

FopImageFactory never releases cached images :
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=8003

Image file opened but not closed:
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=30156
 

Posts:5,904
Registered: 04/03/99
Re: Unload class (to free static references)?  
Aug 2, 2004 4:11 AM (reply 11 of 12)



 

Hmm, well it doesn't look like you've posted patches as such - but you've certainly provided them with enough info to fix the problem fairly trivially. A real shame nobody's looked into it.

They seem to concentrate on a "new, rewritten" version of FOP

Oh dear. They're suffering from Second Project syndrome are they ? Well, perhaps reflection will be the way to go, then. Although there's always the option of a coup d'etat (aka forking) JFreeChart. But that hardly reduces the effort required on your part !

Dave.
 

Posts:5,904
Registered: 04/03/99
Re: Unload class (to free static references)?  
Aug 2, 2004 4:18 AM (reply 12 of 12)



 
Those streams often are created within a method
and there's no reference (variable) to it outside
the method (e.g. in the class or super class)?

Not sure that reflection will provide you with a work-around for this; although, you could write your own classloader to load JFreeChart, and have it provide your own implementation of ImageInputStream which retains references to un-closed streams so they can be killed off later.

We're definitely on the way to the land of ick with this sort of hack, though... I think maintaining your own fork of the source code and keeping it in sync with the main project might actually turn out to be easier. That or beating the dev's around the head to get your fixes accepted.

Dave.
 
This topic has 12 replies on 1 page.