No can do. java.lang.ClassLoader has a field named "Vector classes"  
defined as:

     // The classes loaded by this class loader.  The only purpose of this  
table
     // is to keep the classes from being GC'ed until the loader is GC'ed.
     private Vector classes = new Vector();

It also has a method:

     void addClass(Class c) {
         classes.addElement(c);
     }

that is invoked by *native* JVM code to add a class to the class loader  
when it is loaded. It being package-private method, it can't be overridden  
outside the package, and you also can't define a new class within  
java.lang.* package since it is sealed. Oh, and it is also pretty much  
specific to Sun JRE too :-)

Mind you, it *might* be possible to do horrible hackery, like retrieving  
the "classes" field through reflection, with Field.setAccessible(false),  
and then clearing it from time to time. Needless to say, it would *not*  
work in any sane enterprise scenario when JVM runs under a security  
manager.

The other way to get around this would be to run java with a tampered  
rt.jar in which ClassLoader.addClass() is modified to be a no-op.

Again, all these solutions either require you to run without a security  
manager, or assume a Sun JRE, or both.

However, also notice that the "classes" field is only used for the sole  
purpose of preventing GC of the class. I.e. it is not used by  
findLoadedClass, which actually delegates to native findLoadedClass0()  
method, hinting that there's further association of loaded class to its  
class loader somewhere in JVMs native guts. Meaning it might not be a good  
idea to try to pull the rug out of it, as it could well be assuming that  
the classes can't be GCed until the loader can be.

You can say I gave some thought to this idea already :-)

Attila.

On Wed, 03 Oct 2007 16:15:04 +0200, Matt Fowles <[EMAIL PROTECTED]>  
wrote:

>
> All~
>
> Could you write your own class loader that uses PhatomReferences to
> allow Classes to be GCed?
>
> Matt
>
> On 10/3/07, Charles Oliver Nutter <[EMAIL PROTECTED]> wrote:
>>
>> Attila Szegedi wrote:
>> > Not referenced as in, unreachable? You use  
>> java.lang.ref.PhantomReference
>> > for that.
>>
>> As in using Weak or Soft references to hold the classes; If some type of
>> classloader had some kind of weak reference to the classes it contains,
>> it would allow spinning up just the class rather than an entire
>> garbage-collectable classloader to contain it (as is the case right now
>> in JRuby).
>>
>> - Charlie
>>
>> >
>>
>
> >



-- 
home: http://www.szegedi.org
weblog: http://constc.blogspot.com
Visit Szegedi Butterfly fractals at:
   http://www.szegedi.org/fractals/butterfly/index.html

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JVM 
Languages" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/jvm-languages?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to