0

I used System.Runtime.Caching.ObjectCache to store images in video. After expiration interval, cache is getting expired. But memory didn’t get released. Same behavior was observed in .Net framework 4.6.1 and .Net 5.0. Please find the code snippet below

int expiration_interval_minutes = 5;
ObjectCache cache = MemoryCache.Default;
CacheItemPolicy policy = new CacheItemPolicy();
policy.SlidingExpiration = TimeSpan.FromMinutes(expiration_interval_minutes);
string cacheKey = "some_key";
cache.Set(cacheKey, file_byte_array, policy);

Can anyone guide us to resolve this issue?

  • Try this: Create a simple class that implements IDisposable. Add that to the cache. Put a breakpoint on the Dispose method. When the object expires does the method get called? If so, then caching is working as expected. – Neil Jan 28 '22 at 11:27
  • I tried implementing IDisposable. Dispose is called before the expiration time. I tried GC.Collect() , memory was cleared but why GC.Collect has to be called expilictly? – Pavithra Feb 10 '22 at 10:24

0 Answers0