AnonymousClassLoader in JRuby
Charles Oliver Nutter
charles.nutter at sun.com
Sat Apr 26 14:10:28 PDT 2008
I've managed to wire the AnonymousClassLoader into JRuby. A patch is
attached, that will probably be rather confusing to anyone unfamiliar
with JRuby.
ClassCache is basically a smart class-loading utility that can be used
to share loaded classes across JRuby instances without keeping hard
references to them and preventing them from unloading.
AnonymousClassLoader is an all-static utility class that wraps the logic
of instantiating "one-shot" classloaders for loading bodies of code. In
this case, it either uses java.dyn.AnonymousClassLoader or our own
"OneShotClassLoader" which is not anonymous but which is used for only a
single class.
The code generally appears to work about the same with
java.dyn.AnonymousClassLoader available, which is good. But I'm having
trouble quantifying the benefit to JRuby. I'd like to be able to show
how it helps, but memory profiles look practically the same between the
version using an OneShotClassLoader per method body and the version
using AnonymousClassLoader per method body. I have not done a lot of
work to isolate the cost of loading, but it seems to be unnoticeable in
my simple benchmark which defines 100_000 Ruby methods and forces them
to JIT.
So the runtime benefits may not be so great. The practical benefits,
such as being able to chuck byte[] into AnonymousClassLoader without
decorating class names and ensuring uniqueness, I have not yet utilized
in this code. Those benefits may show how writing class caches like the
one in JRuby are made a lot easier. For the moment, in order to allow
JRuby to support both MLVM work and pre-JDK7 JVMs, this patch still does
name-mangling to ensure methods are unique.
I think part of my confusion is that originally I desired a class loader
for which I could have a single instance I would throw *many* byte[] at,
and they'd all be loaded using that classloader but without hard
references and without the overhead of a classloader-per-method. But the
AnonymousClassLoader interface appears to one exactly one byte[] per
instance, though it does have considerably reduced in-memory cost per
AnonymousClassLoader instance.
Basically, what I've been looking for to make my life easier and memory
costs lower is:
ClassLoader cl = someClassLoader....;
Class first = cl.defineClass(firstClass);
Class second = cl.defineClass(secondClass);
first = null; // and at some point I would expect the class to GC
Is this the purpose of AnonymousClassLoader? Am I doing it wrong? I'll
be poking around for example code now.
- Charlie
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: anonymous_class_loading_support.patch
Url: http://mail.openjdk.java.net/pipermail/mlvm-dev/attachments/20080426/12d83c9d/attachment.ksh
More information about the mlvm-dev
mailing list