# Friday, 29 May 2009
.NET 4.0 -- System.Runtime.CompilerServices.ConditionalWeakTable

Back in the PDC build of .NET 4.0 another interesting new feature was introduced: ConditionalWeakTable<TKey, TValue>. This is a special purpose dictionary to associate objects with other objects. It was introduced to help the DLR, which needs the ability to add properties to arbitrary objects. The documentation is pretty clear and the CLR team blog also has some info on it, so I won't rehash that. Instead I'll just mention that ConditionalWeakTable itself is not a magic type (i.e. the runtime knows nothing about it), but instead it is built on top of a private value type System.Runtime.Compiler.Services.DependentHandle. DependentHandle is essentially a handle based ephemeron implementation.

JVM vs CLR

This means that the CLR now comes a bit closer to the JVM in terms of memory management features. The JVM has had some very interesting reference types for a long time (WeakReference, SoftReference and PhantomReference) and the ability to have these references posted to a ReferenceQueue by the GC when the relevant change in reachability to the referenced object occurs.

Unfortunately there still isn't parity between the CLR and JVM, even though the CLR now provides a capability the JVM doesn't.

Java .NET Notes
ReferenceQueue n/a  
WeakReference WeakReference (short) .NET has no ReferenceQueue equivalent notification mechanism.
n/a WeakReference (long)    
SoftReference n/a  
PhantomReference     n/a  
WeakHashMap n/a  
n/a ConditionalWeakTable  

(If you think that Java's WeakHashMap and .NET's ConditionalWeakTable are similar, consider that ConditionalWeakTable is ephemeron based. Plus the fact that WeakHashMap uses short weak references and ConditionalWeakTable uses long weak references.)

IKVM.NET

ConditionalWeakTable is very useful for IKVM in several places:

  • Used to support class unloading by mapping Assembly to ClassLoader.
  • Used in caching of MethodBase properties.
  • Can be used to track the type of ghost arrays.
  • Can be used to more efficiently implement adding references to a ReferenceQueue.

Java

Given the effort going into Java 7 to improve support for dynamic languages it would not be surprising nor unwelcome to see ephemerons being added to the JVM.

Friday, 29 May 2009 08:08:54 (W. Europe Daylight Time, UTC+02:00)  #    Comments [1]
# Wednesday, 27 May 2009
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]
# Monday, 25 May 2009
Development Snapshot

.NET 4.0

I wasn't sure whether to release a snapshot or not, because things are still very much in flux, but I decided that the ability to build on .NET 4.0 was a big enough feature to at least warrant a development snapshot (albeit a source snapshot this time).

I had to make a number of changes to be able to build on .NET 4.0:

  • System.Type now overloads the == operator, so TypeBase.Equals had to be modified to use ReferenceEquals to avoid infinite recursion.
  • Apply the SecurityRules(Level1) attribute to IKVM.Runtime.dll to opt out of the new security model. This is needed because IKVM.Runtime.dll has the AllowPartiallyTrustedCallers and SecurityCritical attributes.
  • Modify ikvmc and the runtime to apply the SecurityRules(Level1) attribute to all generated assemblies to opt out of the new security model to work around a (likely) bug in .NET 4.0. Special thanks to Shawn Farkas for helping with this.
  • Modify JNI code to move RuntimeMethodHandle from unmanaged to managed data.
  • Changed IKVM.Reflection.Emit to write current runtime version into module header instead of a hardcoded "v2.0.50727".

Visual Studio 2010

Building with Visual Studio was never supported and it still isn't. You need to build with NAnt ("nant clean && nant"). You can use Visual Studio 2010 to edit the source and do test compiles (after first building with NAnt to make sure the required files are all there). If you want to use nant to build on .NET 4.0 you'll have to modify NAnt.exe.config to add the net-4.0 target framework and add a <supportedRuntime ... /> line to the <startup> section.

Changes

  • Many AWT / Swing related changes as Volker continues to merge in OpenJDK code.
  • Fixed java.io.File.list() to not throw a System.NotSupportedException for certain invalid paths.
  • Added workaround for .NET C# compiler bug that prevents it from subclassing a Java class that implements a protected abstract method using a public method. This workaround is required to build IKVM.AWT.WinForms.dll, but the Mono C# compiler has a related bug that I have not been able to work around. Filed Mono Bug #506757.
  • Added support for java.io.File.list() to VFS.
  • Added support for declarative security attributes on assemblies and types.
  • Added some sun.misc.Unsafe methods that appear to be used by JRuby.
  • Various fixes for .NET 4.0 beta 1.
  • Removed code that supports .NET 2.0 RTM (SP1 is now required).

The source-only snapshot is availabe here: ikvmsrc-0.41.3432.zip.

Monday, 25 May 2009 07:10:29 (W. Europe Daylight Time, UTC+02:00)  #    Comments [0]
# Friday, 22 May 2009
.NET 4.0 -- AssemblyBuilderAccess.RunAndCollect

This week .NET 4.0 beta 1 was released. I've been playing around with it a little bit and was very excited to find a new Reflection.Emit feature. If you're a reflection nut like me, the title of this blog entry probably already gave it away. Dynamic assemblies can now be garbage collected!

A Little Demo

Running this simple example will show that a System.Type instance is created and then garbage collected.

using System;
using System.Reflection;
using System.Reflection.Emit;

class RunAndCollectDemo
{
  static void Main()
  {
    Type type = CreateType();
    object obj = Activator.CreateInstance(type);
    Console.WriteLine(obj);
    WeakReference weak = new WeakReference(type);
    type = null;
    obj = null;
    Console.WriteLine("type = " + weak.Target);
    GC.Collect();
    Console.WriteLine("type = " + weak.Target);
  }

  static Type CreateType()
  {
    AssemblyBuilder ab =
      AppDomain.CurrentDomain.DefineDynamicAssembly(
        new AssemblyName("foo"),
        AssemblyBuilderAccess.RunAndCollect);
    ModuleBuilder modb = ab.DefineDynamicModule("foo.dll");
    TypeBuilder tb = modb.DefineType("Foo");
    return tb.CreateType();
  }
}

As you can imagine, this required some pretty deep runtime changes. One of them is that RuntimeMethodHandle is no longer a handle. Previously it contained an unmanaged pointer to the native CLR datastructure corresponding to the method, but now it contains a managed interface reference, because the corresponding method can be garbage collected (I haven't looked into it, but I assume that the interface reference points either to a RuntimeMethodInfo or in the case of a method in a RunAndCollect assembly a proxy that contains a weak reference to the RuntimeMethodInfo. UPDATE: A RuntimeMethodHandle represents a strong reference.)

Somewhat ironically, this change breaks the compilation of ikvm's JniInterface.cs, because it stuffs a RuntimeMethodHandle in unmanaged memory. The fix is fairly straightforward (using a GCHandle to point to the MethodBase instead), but it does reveal an interesting property of C#, if you use unsafe code the (normally hidden) implementation details of managed value types can cause your code to break (and not because you explicitly depend on any internals).

What Does This Mean for IKVM.NET?

It would be really awesome if this feature could be used to finally make ClassLoaders garbage collectable, but unfortunately for that to happen this ancient bug first has to be fixed.

Friday, 22 May 2009 07:26:20 (W. Europe Daylight Time, UTC+02:00)  #    Comments [3]