I am troubleshooting a high traffic C# .NET Framework 4.61 website that utilizes System.Runtime.Caching.MemoryCache quite extensively. In the most recent week, I've seen slowdowns and when I put up some Perfmon counters, I saw that the MemoryCache gets emptied out every two minutes.
All other counters in the image show a similar picture (e.g. Cache Hits, Misses, etc...).
Initially, I thought that perhaps the app is running up against maximum limits of MemoryCache. So I added the following to the web.config:
<system.runtime.caching>
<memoryCache>
<namedCaches>
<add name="CacheManager"
cacheMemoryLimitMegabytes="8000"
physicalMemoryLimitPercentage="99"
pollingInterval="00:05:00" />
</namedCaches>
</memoryCache>
</system.runtime.caching>
However, nothing changed and the MemoryCache continues to be dumped every 2 minutes. Some other troubleshooting notes:
- There is no memory pressure. The application gets up to 2-3 GB of RAM before the 2 minute mark and then memory drops a bit (as evidenced by the
# Bytes in all Heaps
counter). The server has 24 GB of RAM. - Every 2 minutes there is a CPU spike because the app has to go back to the database to fetch data again.
- The application is running on IIS in Windows Server 2019. It was previously running on Windows Server 2008, where these issues didn't exist.
So what could cause the MemoryCache to drop every 2 minutes?
P.S. With the help of MS Support and a couple of proc dumps, we were able to determine that the application domain is restarting every 2 minutes. We've added HKLM\Software\Microsoft\ASP.NET\FCNMode=1 to disable ASP.NET from responding to file system changes. However, what actually causes the restart is still unknown. According to Procmon, no changes are happening application directory.