20

I am trying to understand ConditionalWeakTable. What is the difference between

class ClassA
{
    static readonly ConditionalWeakTable<ClassA, OtherClass> OtherClassTable
        = new ConditionalWeakTable<ClassA, OtherClass>();
}

and

class ClassB
{
    OtherClass otherClass;
}

? What would be the pros and cons of using ClassA or ClassB to reference a nullable field?

2

3 Answers 3

54
+200

I don't exactly get what you're asking about, I assume though you're asking whether you should use property inside your type or ConditionalWeakTable, to which you can attach such a property for that particular type instance. If so, you could alternatively ask whether you should use property alone versus dictionary, which could contain this property under specific key (which will be your particular type instance). Unless you need such a dictionary, it's pretty nonsense.

Understanding of ConditionalWeakTable<TKey, TValue>:

What ConditionalWeakTable actually do is, it allows you to attach additional information to existing, managed, non-dynamic CLR objects. Essentially it can be understood just as a dictionary, where the keys are weakly referenced, so a value is kept alive as long as the key is alive. More information can be found on MSDN.

So, you should ask yourself what are your needs. Assuming your types are instantiated:

var classA = ClassA(); 
var classB = ClassB(); 
var other = OtherClass();

do you want to use the property binded to such instances in this manner:

/* set */
var other = new OtherClass();        
ClassA.OtherClassTable.Add(classA, other);
/* get */
OtherClass data = null;
var result = ClassA.OtherClassTable.TryGetValue(classA, out data);

instead of this one below?

/* set */
classB.OtherClass = other;
/* get */
var result = classB.OtherClass;

Unless have particular needs, the answer seems to be pretty obvious. There are of course further issues here:

What is weak reference and why would you want to use it?

This MSDN article shortly explains the topic. It basically says weak references do not extend the lifetime of the object, by allowing it to be garbage collected, once such an object can still be reached by the application code. Weak references can be useful for pointing to objects which should be available for GC if they are not actively in use. However, if the program uses large number of small objects, weak references can negatively affect memory usage. Threads like this and this should also clarify some remaining doubts.

If you're looking for an example, when you could use ConditionalWeakTable<TKey, TValue> over standard Dictionary<TKey, TValue>, imagine the following case. You'd like to bind a dictionary of properties to an instance at runtime, but at the same time do not want to prevent them from being collected if you've stopped actively using them. Unfortunately in standard approach it's impossible - GC is blocked because dictionary still holds a strong references to them, like this:

var x = new object();
x.Props().Y = "hello";

static class ExpandoExtensions 
{
    private static IDictionary<object, dynamic> props = 
        new Dictionary<object, dynamic>();
    public static dynamic Props(this object key)
    { 
        dynamic o;
        if (!props.TryGetValue(key, out o)){
            o = new ExpandoObject();
            props[key] = o;
        }
        return o;       
    } 
}

Of course you can always take down them manually, but isn't this approach shown below simpler?

static class ExpandoExtensions
{
    private static readonly ConditionalWeakTable<object, ExpandoObject> props =
        new ConditionalWeakTable<object, ExpandoObject>();

    public static dynamic Props(this object key)
    { 
        return props.GetOrCreateValue(key);       
    } 
}

At the same time (MSDN)

avoid using weak references as an automatic solution to memory management problems. Instead, develop an effective caching policy for handling your application's objects.

These extension methods shown above are taken from this thread.

4
  • 1
    You mention that the Value is kept alive till the key is alive.Is n't it the other way around in a conditionalWeakTable? ie the key can be queried upon only until the value resides on the memory? Mar 19, 2016 at 5:34
  • 1
    MSDN says that the key is not kept alive by ConditionalWeakTable. It does not explicitly say the same about value but then goes on to say, that if there's no reference to key except one from a value, the key will not be kept alive. Unless there's some special magic at work this should mean that both key and value are weak-referenced. However, the sample on MSDN does not proof that this is the case (it only proofs that keys are weak referenced), and MSDN has been found to be wrong before. Mar 30, 2017 at 7:52
  • Nice explanation but I would also be interested to know if there are any significant performance differences between a plain old field and using ConditionalWeakTable. Is the runtime smart enough to not actually be using a binary search tree but instead throwing the values in a bucket tied to the instance? Or perhaps a more apt question is whether ConditionalWeakTable performs better than a standard Dictionary keyed with the instance? Feb 16, 2021 at 17:29
  • Quick and dirty test of getting a value from 1000 objects 100000 times, the ConditionalWeakTable method was 15x slower than direct field accessing. So there you go. (This was .NET 5). That said, it seems like the runtime could have been intelligent enough to associate the CWT with the key object directly to avoid the costly lookup. Disappointing. Feb 16, 2021 at 17:45
11

The biggest difference between the two--indeed, the primary reason that ConditionalWeakTable exists and the reason it's part of CompilerServices--is that adding a field to ClassA requires the ability to add a field to ClassA, but constructing a ConditionalWeakTable with a key type of ClassA does not. Effectively, ConditionalWeakTable exists to allow code to effectively "add a field" to instances of any class without needing to modify the class itself. While it might in many cases be possible to use an identity-based weak-key dictionary for such a purpose, that would only work as long as no value held a direct or indirect reference to its key, and there were no cycles of keys which were referenced directly or directly via other key's values. The design of ConditionalWeakTable allows keys and values to be collected even when such cycles exist.

1

One advantage to the first approach is that you can cache the instances of OtherClass. For example,

class ClassA
{
    static readonly ConditionalWeakTable<ClassA, OtherClass> OtherClassTable
        = new ConditionalWeakTable<ClassA, OtherClass>();

    public void Attach(OtherClass otherClass)
    {
        OtherClassTable.Add(this, otherClass);
    }

    public bool Get(out OtherClass otherClass)
    {
        return OtherClassTable.TryGetValue(this, out otherClass);
    }
}

Then, you can attach an instance of OtherClass to an instance of ClassA. The instance of OtherClass will be kept in memory as long as the instance of ClassA to which it is attached is kept alive.

1
  • 9
    You can get the exact same behavior with a normal reference field in ClassA. ConditionalWeakTable is for when you cannot modify ClassA.
    – Paul Groke
    Dec 10, 2013 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.