Quantcast
Channel: Performance
Viewing all articles
Browse latest Browse all 477

ASP.Net / MemoryCache behaviour for w3wp worker process memory usage

$
0
0

Hi,

Actually I had different question / problem asked  here before but I realized there is something specific needs to be clear.

I am using simple MVC 5 project for test. w3wp process start with 150 MB and (after calling home page with doing nothing 250 MB) when I call an action to populate cache it increase to around 2 GB.

Every item has a short time out value and all items timed out after 11 minutes.

I can see them in performance counters. (firs increase in few seconds and all timed out in around 10 minutes).

While item is time out it calls Removed event with Expired statu as expected.

And same thing hapining with ObjectCache and asp.net internal cache too. 

The problem is worker process memory usage stay stay around 2 GB and never drops till recycle.

Is there something needs to be done extra to remove items expired or only solution is recycle ? 

Worker process memory usage stay same if I remove items manually with waiting time out.

My environment  : IIS 8.5 .Net 4.5 

 string rootKey = DateTime.Now.ToString("hh.mm.ss.ffffff");

            string smallText = "small text";
            int smallTextSize = 0;
            for (int i = 0; i < 1000000; i++)
            {
                smallTextSize += smallText.Length;
                string key = string.Format("smallkey:{0}.{1}", rootKey, i);
                this.ControllerContext.HttpContext.Cache.Add(key, smallText, null, DateTime.Now.AddSeconds(i / 1000),
                    System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, new System.Web.Caching.CacheItemRemovedCallback(CacheItemRemoved));
                //MemoryCache.Default.AddOrGetExisting(string.Format("smallkey:{0}.{1}", rootKey, i), smallText, DateTime.Now.AddSeconds(i / 1000));

                System.Diagnostics.Debug.WriteLine("added-smallkey:{0}, date:{1}", key, DateTime.Now.ToString("hh.mm.ss.ffffff"));
            }

            System.Threading.Thread.Sleep(2000);

            string bigText = "2 K string";
            int bigTextSize = 0;
            for (int i = 0; i < 1000000; i++)
            {
                bigTextSize += bigText.Length;

                string key = string.Format("bigkey:{0}.{1}", rootKey, i);
                //default cache
                this.ControllerContext.HttpContext.Cache.Add(key, smallText, null, DateTime.Now.AddSeconds(i / 1000), 
                    System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
                //Memory cache
                //MemoryCache.Default.AddOrGetExisting(string.Format("bigkey:{0}.{1}", rootKey, i), bigText, DateTime.Now.AddSeconds(i / 1000));
                //memory cache with lock
                //AddOrGetExistingWithLock<string>(rootKey + "." + i, () => bigText, i);    

                System.Diagnostics.Debug.WriteLine("added-bigkey:{0}, date:{1}", key, DateTime.Now.ToString("hh.mm.ss.ffffff"));
            }


Viewing all articles
Browse latest Browse all 477

Trending Articles