Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / IIS

Limit CPU Usage for ASP.NET Application

4.87/5 (7 votes)
9 Apr 2014CPOL5 min read 29K  
This article explains how you can set a maximum limit of CPU usage for your website.

Introduction

I have encountered a problem many times that one website is running at almost full CPU usage, thus negatively impacting the performance of other websites. When multiple websites are running on the same server like a shared hosting environment, it is important to control the resource consumption by each website so that all the websites can run together without impacting the performance of each other. In this article, I will explain the ways to put a maximum limit on the CPU usage for a website such that if it tries to use the cpu beyond a certain limit, system will automatically take a preventive action.

CPU Throttling In Action

IIS can help you by posting a sentry to guard the system CPU. In IIS you can control the CPU usage for each Application Pool. The solution can be applied to ASP.NET application or any websites hosted in IIS. It provides you the option to put a maximum limit of CPU usage. If an application tries to go beyond this limit, it provides various options to prevent any harm to the other applications. In IIS, there are following properties for CPU throttling:

  • Limit (percent) - This is the maximum limit of CPU time that an application pool can consume. This value is in percentage, and the allowed value is from 0 to 100. If this limit is exceeded for the time interval defined by Limit Interval, an event is written in the event log and the action set by Limit Action property will be performed. The default value of this property is 0, which means that it is disabled. Note: Some articles specify that the limit is in defined as 1/1000 of a percent i.e. valid value is 0 to 100000. This is the case when you are using IIS 8 on Windows Server 2012 operating system. If you are using Windows 8, the valid range is 0 to 100.

  • Limit Action - You can define the action to be taken when the limit set by "Limit (percent)" property is exceeded. An event log entry is always generated, irrespective of the value of this property. The possible value are:
    • NoAction - This means that an entry will be made to the event log and no further action will be taken. This property can be useful if you just want to monitor that if an application pool is exceeding a certain limit but do not want the system to take any action automatically.
    • KillW3wp - This means that terminate the process for the rest of the interval defined by Limit Interval property.
    • Throttle - In this case, application pool is not allowed to cross the limit. The application will keep running at the set limit but will not go beyond this. Limit Interval property is ignored in this case.
    • ThrottleUnderLoad - This is similar to Throttle, but the difference is that limit is enforced only when there is another process trying to use the CPU. If the CPU is idle, the application poll is allowed to consume the CPU beyond the limit. Limit Interval property is ignored in this case.
  • Limit Interval (minutes) - This is the time interval for CPU monitoring and throttling. CPU usage is monitored for this interval and the counters are reset after this interval. In case Limit Action is Throttle or ThrottleUnderLoad, this value is ignored.

Which Limit Action should I use?

Administrators are often confused about the setting of Limit Action property or the other settings related to cpu throttling. There are no preset rules for this, but there are some guidelines:

1. NoAction - If you don't want any restrictions on CPU usage for your applications, still its a good idea to use these options for some time say for a week. Use a high value for limit say 80% with Limit Action set to NoAction. If your application is crossing this limit, you will see the entries in event log and take preventive actions like moving to a more powerful server to avoid any future issues.

2. KillW3wp - Some cases where this property can be considered:

  • If you are a shared hosting provider and wants strict limits for your tenants, this might be a choice for you.
  • In many cases we know that if the application is going to a very high limit say 80%, then there could be some problem in the application itself. In many hang some other important processes, so its good idea to stop the application for some interval.

3. Throttle - This is a good choice when the users in a shared hosting environment are charged based upon the CPU limit. Users can choose the hosting package according to their needs and administrator will setup the limit action to Throttle. With this application will be restricted to a predefined limit.

4. ThrottleUnderLoad - I would prefer this option in most of the cases. This allows the processes to cross the limit if there is no contention for CPU usage. IIS will put a restriction only when there are other processes wanting for CPU cycles. Otherwise process will keep running beyond the limit but only an event log entry will be generated.

Now we know the options available with us. Let's see how we can use it:

1. Open Internet Information Services (IIS) Manager. You can use the command inetmgr for this.

2. Select Application Pools from the left pane and then select your application pool from the right pane. You can use DefaultAppPool for testing.

3. Right click DefaultAppPool or any other application pool for which you want to define the limit. Now choose "Advanced Settings" from the context menu.

4. The options for CPU limit are available under CPU category. Here you can do the settings according to your requirements.

By default, cpu throttling is disabled. The following snapshot shows the default values:

Points of Interest

You can define the cpu limit settings as the default settings such that these are applied to every new application pool. For this use the "Set Application Pool Defaults..." option and the define the cpu limit settings.

You may also need the memory and bandwidth limits to have a complete control over the applications. The memory and bandwidth limit can be set using the options provided by Windows Server 2012 or Windows Server 2008.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)