Memory cache is not getting released after expiration

Pavithra V 21 Reputation points
2022-01-28T11:31:31.663+00:00

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?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 114.7K Reputation points
    2022-02-07T14:30:19.833+00:00

    Therefore, the memory is released, but not reported to you immediately, because the system does not execute GC.Collect if it is not needed yet. (Calling GC.Collect too frequently and at the inappropriate time usually reduces the overall performance).

    0 comments No comments