Equivalent to SoftReference in .net?
Asked Answered
D

5

6

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.

Deliberation answered 10/3, 2009 at 20:4 Comment(0)
U
0

No there isn't an equivalent. Is there a particular reason why WeakReference won't do the job?

Here is a similar question to yours:

Why doesn't .NET have a SoftReference as well as a WeakReference, like Java?

Unclose answered 10/3, 2009 at 20:11 Comment(1)
-1 Tony said he wanted to implement a memory-sensitive cache. Yet MSDN says this about WeakReferences: "Avoid using weak references as an automatic solution to memory management problems. Instead, develop an effective caching policy for handling your application's objects."Anabal
G
3

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.

Graviton answered 10/3, 2009 at 20:36 Comment(0)
M
2

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.

Mathis answered 10/3, 2009 at 20:12 Comment(0)
L
2

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

Laurettelauri answered 10/3, 2009 at 20:14 Comment(0)
B
1

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.

Blalock answered 9/6, 2012 at 18:49 Comment(0)
U
0

No there isn't an equivalent. Is there a particular reason why WeakReference won't do the job?

Here is a similar question to yours:

Why doesn't .NET have a SoftReference as well as a WeakReference, like Java?

Unclose answered 10/3, 2009 at 20:11 Comment(1)
-1 Tony said he wanted to implement a memory-sensitive cache. Yet MSDN says this about WeakReferences: "Avoid using weak references as an automatic solution to memory management problems. Instead, develop an effective caching policy for handling your application's objects."Anabal

© 2022 - 2024 — McMap. All rights reserved.