一年后回顾这篇文章,我在原来的帖子中发现了更多关于缓存“随机删除”的信息。 MSDN 对可配置缓存属性 CacheMemoryLimitMegabytes 和 PhysicalMemoryLimitPercentage 声明如下:
默认值为0,表示MemoryCache类的
默认情况下使用 autosize 启发式。
进行一些反编译和调查后,CacheMemoryMonitor.cs 类深处有一些预先确定的场景来定义内存阈值。以下是 AutoPrivateBytesLimit 属性上该类中的 cmets 示例:
// Auto-generate the private bytes limit:
// - On 64bit, the auto value is MIN(60% physical_ram, 1 TB)
// - On x86, for 2GB, the auto value is MIN(60% physical_ram, 800 MB)
// - On x86, for 3GB, the auto value is MIN(60% physical_ram, 1800 MB)
//
// - If it's not a hosted environment (e.g. console app), the 60% in the above
// formulas will become 100% because in un-hosted environment we don't launch
// other processes such as compiler, etc.
特定的值并不一定像理解为什么经常使用缓存一样重要:存储我们不想获取的大对象和结束。如果这些大型对象被存储在缓存中,并且超出了基于这些内部计算的托管环境内存阈值,则您可能会自动从缓存中删除该项目。这当然可以解释我的 OP,因为我在托管服务器上的内存中存储了一个非常大的集合,其中可能有 2GB 的内存在 IIS 中运行多个应用程序。
设置这些值有一个明确的覆盖。您可以通过配置(或在设置MemoryCache 实例时)设置CacheMemoryLimitMegabytes 和PhysicalMemoryLimitPercentage 值。这是来自以下MSDN 链接的修改示例,我将physicalMemoryPercentage 设置为95 (%):
<configuration>
<system.runtime.caching>
<memoryCache>
<namedCaches>
<add name="default"
physicalMemoryLimitPercentage="95" />
</namedCaches>
</memoryCache>
</system.runtime.caching>
</configuration>