I am familiar with WeakReference
, but I am looking for a reference type that is cleared only when memory is low, not simply every time when the gc runs (just like Java's SoftReference
). I'm looking for a way to implement a memory-sensitive cache.
A Soft reference is eligible for collection by garbage collector, but probably won't be collected until its memory is needed. i.e. garbage collects before OutOfMemoryError . A Weak reference is a reference that does not protect a referenced object from collection by GC.
Soft reference objects, which are cleared at the discretion of the garbage collector in response to memory demand. Soft references are most often used to implement memory-sensitive caches. Suppose that the garbage collector determines at a certain point in time that an object is softly reachable.
A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist.
In computer programming, a weak reference is a reference that does not protect the referenced object from collection by a garbage collector, unlike a strong reference.
The ASP.NET cache gives you the memory-sensitive behaviour you want, with the drawback that everything needs a unique key. However, you should be able to hold a WeakReference to an object that you've placed in the ASP.NET cache. The cache's strong reference will keep the GC at bay until the cache decides that it needs to be scavenged to free memory. The WeakReference gives you access to the object without doing a lookup with the cache key.
Foo cachedData = new Foo();
WeakReference weakRef = new WeakReference( cachedData );
HttpRuntime.Cache[Guid.NewGuid().ToString()] = cachedData;
...
if ( weakRef.IsAlive )
{
Foo strongRef = weakRef.Target as Foo;
}
You could create a SoftReference class by extending WeakReference along the lines of
class SoftReference : WeakReference
{
public SoftReference( object target ) : base( target )
{
HttpRuntime.Cache[Guid.NewGuid().ToString()] = target;
}
}
You'd also need to override the setter on Target to make sure that any new target goes into the cache.
Maybe the ASP.NET Cache class (System.Web.Caching.Cache) might help achieve what you want? It automatically remove objects if memory gets low:
Here's an article that shows how to use the Cache class in a windows forms application.
In addition to the ASP.NET Cache, there is the Caching Application Block from the Microsoft Patterns and Practices group.
http://msdn.microsoft.com/en-us/library/cc309502.aspx
Although a SoftReference
might seem like a convenient way to implement memory caching, it requires the Java runtime to make a somewhat arbitrary determination as to whether the benefit to keeping an object around exceeds the cost of storing it. Unfortunately, the runtime has limited information about the real cost of keeping an object around (bearing in mind that the real cost may include the impact of a application's memory usage on other applications), and practically no information about the benefit to keeping the object around.
If it will be worthwhile to keep an object around even when no outside strong references to it exist, a cache should keep a strong reference to it (at least as long as it seems worthwile). If the benefit from keeping the object in the cache will only extend as long as an outside reference exists (e.g. because producing instances is cheap, but having two logical entities that hold identical data use the same instance to hold them would facilitate comparisons between those entities), one should use a WeakReference
.
Incidentally, if I had my druthers, .net would support another kind of reference which I've not seen in any platform: an "of interest to someone else" reference, which would be used in conjunction with a type of WeakReference
. An "of interest to someone else" reference could be used as a strong reference, but a suitably-configured WeakReference
would be invalidated if the only strong references to its target were "of interest to someone else". Such a concept could improve efficiency when using a concurrent GC, in cases where a weak-event handler would repeatedly generate a strong reference to its target. If nobody's really interested in what the event handler is doing with its target, it would be desirable if the handler could get unsubscribed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With