# 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]
Wednesday, 27 May 2009 15:30:33 (W. Europe Daylight Time, UTC+02:00)
Hi. It's very intresting that:
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName("MySuperAsm"),
AssemblyBuilderAccess.RunAndSave);
byte[] rawModule = File.ReadAllBytes(@"C:\ClassLibrary1.dll");
ab.DefineDynamicModule("MySuperMod");
return ab.LoadModule("MySuperMod", rawModule).GetTypes()[0];

throws NotImplementedException on "ab.LoadModule" in .NET 4.0. ))
Wednesday, 27 May 2009 15:33:30 (W. Europe Daylight Time, UTC+02:00)
Sorry, AssemblyBuilderAccess.RunAndCollect, though it does not matter.
Name
E-mail
Home page

I apologize for the lameness of this, but the comment spam was driving me nuts. In order to be able to post a comment, you need to answer a simple question. Hopefully this question is easy enough not to annoy serious commenters, but hard enough to keep the spammers away.

Anti-Spam Question: What method on java.lang.System returns an object's original hashcode (i.e. the one that would be returned by java.lang.Object.hashCode() if it wasn't overridden)? (case is significant)

Answer:  
Comment (HTML not allowed)  

Live Comment Preview