Wednesday, February 13, 2008

AnonymousClassLoader in OpenJDK Mlti-Language VM (MLVM)

When we implement JIT interpreter for dynamic languages on JVM, we typically deal with set of data (Environment) and Operations (Methods, Lambda etc).

Design and loading of class definition for data and operations on JVM, has following challenges (pains),

a) We can't define a light weight / anonymous methods in JVM on the fly.

Method definition must live in a class. (Relationship between class that defines method and data could be composition or inheritance).

b) We can't load multiple class definitions of same class / overwrite previously loaded class definition with new one, in the same class loader.

Class loader keeps track of all loaded class in its System Dictionary. When we try to reload class definition (even though class definition is changed after first load), class loader checks for any entry in System dictionary for a given class name. If so, it will return class definition of system dictionary entry.

To circumvent above issue, we instantiate (custom) new class loader every time when class definition changes.

c) Garbage collection issue

Class loader is another Java class in JDK. When a class (Let's call host class) instantiates our custom class loader; say CL1, class loader of host class (could be system class loader or ext class loader or another custom class loader) can reach CL1 class loader. Make sure custom class loaders like CL1 are eligible for GC (there by all classes loaded by that class loader) when they are no longer useful.

OpenJDK Mlti-Language VM, introduces java.dyn.AnonymousClassLoader that alleviates above pain points,

1) No System Dictionary in AnonymousClassLoader and AnonymousClassLoader is independent of existing class loaders in JDK. (Refer AnonymousClassLoader.java)

2) We can load multiple definition of same class using same instance of AnonymousClassLoader and it is responsibility of the user to choose preferred class definition among loaded class definitions to create instance. (Refer testAPI method of DVMTest.java)

3) Classes defined by AnonymousClassLoader are not reachable by class loader of host class. Making not-in-use class definitions available for GC is simple.

Above points are not theory, we have tested and working prototype ready. If you want to try yourself, refer my previous post on building MLVM.

QR code for building MLVM :

No comments: