Asp.net Core中内存使用的限制

时间:2016-08-12 14:51:06

标签: asp.net-core memorycache

我在项目中使用IMemoryCache。我想知道如果我的应用程序在缓存中推送许多长生命对象会发生什么。它可以占用所有可用的内存吗?我可以全局定义应用程序的最大内存吗?

1 个答案:

答案 0 :(得分:6)

这是一年前写的,所以我假设你正在使用v1.x.x包的Microsoft.Extensions.Caching.Memory

由于SizeLimit中没有MemoryCacheOptions属性,如v2.x.x,在深入研究代码一段时间后,我找到了以下文档。

https://github.com/aspnet/Caching/blob/rel/1.1.2/src/Microsoft.Extensions.Caching.Memory/MemoryCache.cs#L329

/// This is called after a Gen2 garbage collection. We assume this means there was memory pressure.
/// Remove at least 10% of the total entries (or estimated memory?).

因此,程序包将占用内存,因为操作系统将允许您的代码拥有。当它达到该限制时,它将开始压缩(逐出)缓存条目。

使用v2.x.x,您可以使用SizeLimit属性手动设置限制,甚至可以在达到限制CompactionPercentage时设置压缩量。

相关问题