# Friday, 29 May 2009
« Class Unloading | Main | New Development Snapshot »
.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.


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


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.


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 [2]
Friday, 29 May 2009 08:52:30 (W. Europe Daylight Time, UTC+02:00)

Interesting. It would be nice to get ephemerons indeed. This has been discussed a few times in the concurrency mailing list and there was talk that it would be in JDK7.

However, as time has passed, it seems like that is not going to happen anymore.

Friday, 03 March 2017 17:42:42 (W. Europe Standard Time, UTC+01:00)
Very interesting, but it also means that any Java Lib which uses ReferenceQueue to free itself up, is a Memory Leak in ikvm
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