Equivalent to SoftReference in .net?

青春壹個敷衍的年華 提交于 2019-12-07 05:42:14

问题


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.


回答1:


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?




回答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:

  • ASP.NET Caching Overview

Here's an article that shows how to use the Cache class in a windows forms application.




回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/632039/equivalent-to-softreference-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!