T
Todor Todorov
Hi,
I am trying to implement a weak dictionary (more like a lookup table) for
caching purposes. The idea is that when you put an object in it, both the
key and the value (and the key-value pair) are eligible for garbage
collection.
I am used to Smalltalk, and the way this is implemented in Smalltalk is to
use a key-value pair with a weak reference to the value object and strong
reference to the key object. Smalltalk also allows the system to notify YOU
(the dictionary) when a specific 3rd. object (the value object) gets
finalized. That way you can clean up any unwanted objects - or more
precisely, remove the pair object, thus making the pair and key objects
eligible for GC once the value object has been collected.
Some assumptions about my .Net objects.
* The type of value objects for the dictionary is well known.
* The key and value pair 1 to 1. Each value object knows its key.
* I can extend the value object class.
* A value object will be stored in only one cache dictionary.
I've been thinking of implementing the weak dictionary in the following way:
class WeakTable : ...
{
private Dictionary<object, Item> innerDictionary;
void Add(Item item)
{
this.innerDictionary.Add(item.Key, new WeakReference(item);
item.Owner = this;
}
Item Get(object key)
{
return this.innerDictionary[key].Target;
}
}
class Item
{
public object Key;
public WeakTable Owner;
void Finalize()
{
this.Owner.RemoveKey(this.Key);
}
}
The basic idea here is:
1. To use WeakReference as the middle-man inside the weak dictionary, so the
reference to the value object will be weak.
2. To have the value object remove itself from the dictionary when it's
GC'ed, so the pair and key objects can be GC'ed as well.
Of course the version above is very simplified without checks etc. More
methods have to be implemented as well as enumerator that handles the
middle-man WeakReference object correctly.
My questions:
- Does this make sense?
- What kind of GC related race conditions should I wary about?
- Is there anything special GC related that I've missed?
I am trying to implement a weak dictionary (more like a lookup table) for
caching purposes. The idea is that when you put an object in it, both the
key and the value (and the key-value pair) are eligible for garbage
collection.
I am used to Smalltalk, and the way this is implemented in Smalltalk is to
use a key-value pair with a weak reference to the value object and strong
reference to the key object. Smalltalk also allows the system to notify YOU
(the dictionary) when a specific 3rd. object (the value object) gets
finalized. That way you can clean up any unwanted objects - or more
precisely, remove the pair object, thus making the pair and key objects
eligible for GC once the value object has been collected.
Some assumptions about my .Net objects.
* The type of value objects for the dictionary is well known.
* The key and value pair 1 to 1. Each value object knows its key.
* I can extend the value object class.
* A value object will be stored in only one cache dictionary.
I've been thinking of implementing the weak dictionary in the following way:
class WeakTable : ...
{
private Dictionary<object, Item> innerDictionary;
void Add(Item item)
{
this.innerDictionary.Add(item.Key, new WeakReference(item);
item.Owner = this;
}
Item Get(object key)
{
return this.innerDictionary[key].Target;
}
}
class Item
{
public object Key;
public WeakTable Owner;
void Finalize()
{
this.Owner.RemoveKey(this.Key);
}
}
The basic idea here is:
1. To use WeakReference as the middle-man inside the weak dictionary, so the
reference to the value object will be weak.
2. To have the value object remove itself from the dictionary when it's
GC'ed, so the pair and key objects can be GC'ed as well.
Of course the version above is very simplified without checks etc. More
methods have to be implemented as well as enumerator that handles the
middle-man WeakReference object correctly.
My questions:
- Does this make sense?
- What kind of GC related race conditions should I wary about?
- Is there anything special GC related that I've missed?