Netaji Bandi
Netaji Bandi

Reputation: 21

Does iis recycle cleans memory?

I am having a web application deployed to IIS, my app uses static Dictionary which is filled in from an external api frequently.

Sometimes I observe that the Dictionary is being cleared once in a while & I suspect it is because of IIS Automatic Recycle.

Can anyone please confirm that this could be a reason? So basically my question would be will IIS Recycle cleans up the static memory that a webapp is using? (Although I understand that this will only happens when there are no active connections to the server)

Upvotes: 1

Views: 4163

Answers (2)

A.Sideris
A.Sideris

Reputation: 530

Yes, the IIS by default recycles your app pool by calling a garbage collector to clear the memory on every 20 minutes.

You can see Idle-timeout setting in your app pool -> Advanced settings, but better do not change it.

All static things are "Bad" do not use them, your option is caching. You can make a generic cache service that is using the default MVC cache and make it thread safe.

You can also use the [OutputCache] attribute on child actions controller and set minutes. Between this interval the data will be cached

Or you can implement your own caching logic.

From all the three things I will suggest you the first one with using the default MVC cache. I will provide you a sample implementation thanks to #TelerikAcademy and #NikolayKostov

namespace Eshop.Services.Common
{
using System;
using System.Web;
using System.Web.Caching;
using Contracts;

public class HttpCacheService : IHttpCacheService
{
    private static readonly object LockObject = new object();

    public T Get<T>(string itemName, Func<T> getDataFunc, int durationInSeconds)
    {
        if (HttpRuntime.Cache[itemName] == null)
        {
            lock (LockObject)
            {
                if (HttpRuntime.Cache[itemName] == null)
                {
                    var data = getDataFunc();
                    HttpRuntime.Cache.Insert(
                        itemName,
                        data,
                        null,
                        DateTime.Now.AddSeconds(durationInSeconds),
                        Cache.NoSlidingExpiration);
                }
            }
        }

        return (T)HttpRuntime.Cache[itemName];
    }

    public void Remove(string itemName)
    {
        HttpRuntime.Cache.Remove(itemName);
    }
}

}

The usage of it is super simple with anonymous function and time interval

You can set it as a protected property of a Base Controller and to Inherit BaseController in every controller you use. Than you will have the cache service in every controller and you can simply use it that way

var newestPosts = this.Cache.Get(
     "newestPosts",
      () => this.articlesService.GetNewestPosts(16).To<ArticleViewModel().ToList(), 
           GlobalConstants.DefaultCacheTime);

Let's assume that GlobalConstants.DefaultCacheTime = 10

Hope that this answer will be useful to you. :)

Upvotes: 2

Lesmian
Lesmian

Reputation: 3952

If you look at this MS article: https://technet.microsoft.com/pl-pl/library/cc753179(v=ws.10).aspx

In addition to recycling an application pool on demand when problems occur, you can configure an application pool to recycle a worker process for the following reasons: At a scheduled time

  • After an elapsed time

  • After reaching a number of requests

  • After reaching a virtual memory threshold

  • After reaching a used memory threshold

So if IIS recycle would not clean up memory recycling it on memory threshold would not make sense. Additionally, IIS recycle cause application restart so it's obviously clears it memory too.

Upvotes: 1

Related Questions