Coder's Guild Mailing List

Re: [java] Class loading and Unloading

Posted by Mark Horan on 2000-06-24

I don't think this is an issue. Somebody correct me if I'm wrong.
But I don't believe that loading a class and instanciating an object
are handled by the same mechanism. Objects are allocated program
heap space dynamically, and this resource is reclaimed by the garbage
collector when no references to the object remain, which issue can
be forced by setting the object reference to null. There are a couple
of System calls you can make to force garbage collection, but I've
read that it's better to let the VM handle this scheduling according
to its own lights. Class loading, on the other hand, happens on as
as-needed basis, and I don't think the rationale behind this is to
save memory, but to save the overhead of loading classes that aren't
begin used, byte code verification, network transfer time, and all that.
I'm quite certain that loading a class again of the same name, but which
code has changed, results in a new definition for that class. This is
commonly done by servlet engines, which check the timestamps of class
files, reloading them as necessary, prior to passing requests to servlets.
There's a call in the Runtime class which returns a long type representing
the amount of available memory. If you print this value out periodically
as your program is running, you see memory dwindling away steadily until,
just when it seems as if nearly all is lost, tons of memory appears to
have been suddenly reclaimed.

Mark 
horan@xxxxxxxxx.xxx 
Dare to be naive.    
-R. Buckminster Fuller

On Sat, 24 Jun 2000, Frank Hale wrote:

> Is it possible for me to unload a class from my java program and the VM release the memory that was associated with that class? 
> 
> For instance say I have a java application which loads classes in dynamically. These classes will be used and when finished released (unloaded). When these classes are unloaded they would need to be garbage collected so that memory would be freed. Is this possible to do? If so is there an example on how to unload a class? 
> 
> Also these classes could be subject to change while the program is still running. Would the new version of the class be loaded in or would the old version still remain in memory?
> 
> Thanks,
> Frank
> 
> ---
> ICQ# 7205161
> http://sapphire.sourceforge.net - Yet Another X11 Window Manager
> 
> 
> Send FREE Greetings for Father's Day--or any day!
> Click here: http://www.whowhere.lycos.com/redirects/fathers_day.rdct
> 
> > IT Professionals: Match your unique skills with the best IT projects at
> http://click.egroups.com/1/3381/12/_/25287/_/961877428/
> > 
>