# Wednesday, 27 May 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();
    Console.WriteLine(obj);;
    GC.Collect();
    Console.WriteLine(weak.Target);
    //GC.KeepAlive(obj);
  }
}

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, 27 May 2009 07:31:59 (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]