# 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.
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)

Comment (HTML not allowed)  

Live Comment Preview