# Wednesday, May 27, 2009
« Development Snapshot | Main | .NET 4.0 -- System.Runtime.CompilerServi... »
Class Unloading

I never thought I'd see the day, but it has arrived:

using System;
using java.net;

class ClassGC
  static void Main(string[] args)
    URLClassLoader loader = new URLClassLoader(new URL[] { new URL("file:/c:/j/") });
    WeakReference weak = new WeakReference(loader);
    object obj = loader.loadClass("test").newInstance();

Running this on .NET 4.0 (with references to a version of IKVM 0.41 in which I've prototyped support for class gc):
(Don't forget to run in Release mode, otherwise the JIT will extend the lifetime of the local variables to the end of the method.)

Uncomment the GC.KeepAlive(obj) line to show that the object instance really does keep alive the class loader (and all associated IKVM and .NET infrastructure):

Source + binaries (remember this is prototype level code): ikvm-classgc-prototype.zip

Wednesday, May 27, 2009 7:31:59 AM (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]
Wednesday, May 27, 2009 3:30:33 PM (W. Europe Daylight Time, UTC+02:00)
Hi. It's very intresting that:
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName("MySuperAsm"),
byte[] rawModule = File.ReadAllBytes(@"C:\ClassLibrary1.dll");
return ab.LoadModule("MySuperMod", rawModule).GetTypes()[0];

throws NotImplementedException on "ab.LoadModule" in .NET 4.0. ))
Wednesday, May 27, 2009 3:33:30 PM (W. Europe Daylight Time, UTC+02:00)
Sorry, AssemblyBuilderAccess.RunAndCollect, though it does not matter.
Comments are closed.