Pud*_*ler 2 .net c# variables memory-management
背景
所以前提是我有一个用属性提供数据的类.
class ExampleClass
{
string _largeCalculatedVariable = null;
public string largeCalculatedVariable
{
get
{
if (_largeCalculatedVariable == null)
{
_largeCalculatedVariable = LongRunningCalculate();
}
return _largeCalculatedVariable
}
}
}
Run Code Online (Sandbox Code Playgroud)
该属性隐藏了这样的事实:如果数据尚未生成,则数据可以即时计算数据,但如果之前已经预先计算或计算,则将返回缓存值.
问题
问题是可能会生成一个大的ExampleClass列表,如果我访问largeCalculatedVariable足够的它们,我可能会耗尽内存.既然我可以随时重新计算这个值是否有办法告诉.NET _largeCalculatedVariable何时需要内存?
注意:我有一种感觉,我可能在这里使用错误的设计模式.
如果您使用的是.NET 4.0或更新版本,您可以使用MemoryCache该类,您可以将其设置为具有应存储多少数据的规则,并且如果已达到其限制,它将自行清除以腾出空间.
class ExampleClass
{
//This is a static constructor;
static ExampleClass()
{
var settings = new NameValueCollection();
settings.Add("PhysicalMemoryLimitPercentage", "75");
_cache = new MemoryCache("ExampleClassCache", settings);
}
private static MemoryCache _cache;
public ExampleClass()
{
_cacheKey = Guid.NewGuid().ToString();
}
private readonly string _cacheKey;
public string largeCalculatedVariable
{
get
{
var record = _cache.Get(_cacheKey) as string;
//If record was null that means the item was not in the cache.
if(record == null)
{
record = LongRunningCalculate();
_cache.Add(_cacheKey, record, new CacheItemPolicy(), null);
}
return record;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想要,你也可以ExampleClass从缓存中取出物品,以便在处理对象时释放空间,实现起来并不多.
class ExampleClass : IDisposable
{
//...Everything else is the same from the previous code example.
public void Dispose()
{
Dispose(true)
GC.SupressFinialize(this);
}
bool _disposed;
protected virtual void Dispose(bool disposing)
{
if(!_disposed)
{
if(disposing)
{
//Nothing to do here, we want to remove from the cache if Dispose or the finalizer is called.
//But you may have IDisposeable objects in your real class, they should be disposed in here.
}
if(_cacheKey != null)
_cache.Remove(_cacheKey, null);
_disposed = true;
}
}
~ExampleClass()
{
Dispose(false);
}
}
Run Code Online (Sandbox Code Playgroud)