IIS Application Pool Recycling Under Memory Pressure: Limits, Leaks, and Diagnostics

An IIS worker process that recycles more than a few times a day is usually trying to tell you something. Recycling destroys the in-process cache, terminates in-flight requests where the fast-fail policy allows it, and drops session state for anything not stored out of process. It looks like a cure for a memory problem and is frequently the cause of the next one. This article covers what actually triggers a recycle under memory pressure, how to distinguish a legitimate memory limit from a leak being papered over, and the diagnostics that identify the leak rather than the symptom. Capacity sizing for the host is covered on windows-vps.org.

The Four Things That Cause an Application Pool to Recycle

An application pool recycles for exactly four reasons: a scheduled time interval, a private or virtual memory limit, a request or schedule-based condition such as a fixed number of requests served, or an explicit administrator action including a configuration change. Each produces a different Event ID pair in the System and Application logs — WAS logs Event ID 5074 for the trigger and the worker process logs 1000-series health events. Reading which trigger fired is the first diagnostic step and it tells you whether anyone has a memory problem at all.

The default configuration on a fresh Windows Server install has the periodic time interval set to 1740 minutes, which is 29 hours, and no memory limits. A pool recycling once a day is usually just that default. A pool recycling every twenty minutes is hitting a memory limit, and the question becomes whether the limit is set correctly, whether the application process is balanced across the pool, or whether the code is retaining references it should release.

Private Bytes Versus Virtual Bytes

Private Bytes is committed memory that is not shared — heap allocations, managed and unmanaged. Virtual Bytes is the size of the private address space including reserved regions, which on a 64-bit process is enormous and almost never the number you want to limit. Setting a Virtual Bytes limit on a 64-bit worker process is one of the most common misconfigurations in IIS: the process reserves address space far beyond what it commits, so the limit trips while the machine still has free RAM, and the recycle happens for no operational reason.

Set Private Bytes as the limit if you must set one, and derive the number from observation rather than from a round figure. Capture the working set at peak traffic over a week, add 30 to 50 percent headroom, and set the limit well above that. The configuration is per pool in applicationHost.config and can be applied without a GUI:

appcmd list apppool "MyAppPool" /text:*
appcmd set apppool "MyAppPool" /recycling.periodicRestart.privateMemory:1572864
appcmd set apppool "MyAppPool" /recycling.periodicRestart.time:1.00:00:00
appcmd set apppool "MyAppPool" /recycling.disallowOverlappingRotation:true

Private memory is in kilobytes, so 1572864 is 1.5 GB. The third setting stops two worker processes from co-existing during rotation, which matters under memory pressure: overlapping rotation doubles the pool’s footprint for the duration of the overlap, and on a 4 GB VPS that is often the difference between a graceful recycle and an out-of-memory event across the host.

Reading Memory Pressure From the Counters

The counters that answer the question are not the ones most dashboards collect. Memory Available MB tells you the host is short. What you want is the trajectory of the worker process itself: Process Private Bytes for w3wp, .NET CLR Memory Heap Size Bytes for the managed heap, and the garbage collection counters that show whether the heap is growing because of live objects or because nothing has promoted yet. A managed heap that climbs monotonically between recycles and drops to a stable baseline at each recycle is a leak. A heap that climbs and falls in a sawtooth within a session is normal generational behaviour.

Get the memory counters that correspond to the pool’s own metrics so the limit and the observation are in the same units. The performance count on a recycled pool resets, so capture across a full cycle rather than from the last few minutes:

Get-Counter '\Process(w3wp*)\Private Bytes' -SampleInterval 10 -MaxSamples 180
Get-Counter '\Memory\Available MBytes','\Memory\Pages/sec' -MaxSamples 30
Get-WinEvent -LogName System -MaxEvents 500 |
  Where-Object { $_.Id -in 5074, 5117, 5186 } | Select TimeCreated, Id, Message

Pages/sec above a few hundred sustained on a VPS means the host is paging, and a recycling pool on a paging host will recycle faster each cycle as the working set is evicted. That spiral is the most common memory-pressure failure mode on small instances.

Why Recycling Is Not a Leak Fix

Setting the Right Alarm Instead of a Limit

Once the leak is identified and fixed, the memory limit’s role changes. Its purpose is no longer to contain normal operation but to bound the damage if the leak returns. Set it with enough headroom that it never fires during legitimate peak load, and add a separate monitoring alarm that triggers at 70 percent of the limit. The alarm gives you a warning, the limit gives you a crash barrier, and the two together mean the pool recovers automatically while you still get a report to act on.

Record the baseline working set after the fix, and re-measure after each application deployment. A release that raises the steady-state footprint by a hundred megabytes is not a bug in itself, but it changes the arithmetic for the limit, and leaving the limit where it was means the next innocent release trips it. The measured baseline is the number that makes the whole configuration defensible.

Diagnosing the Leak Properly

The technique that identifies a managed leak without guesswork is a pair of heap dumps taken an hour or two apart, after the process has been warm for a while. Compare object counts by type rather than total size: the type whose instance count grows monotonically is the one being retained. Static collections, event handler subscriptions that are never removed, and caches keyed by something unbounded — a per-request identifier, a user-supplied string — account for the majority of real leaks in line-of-business applications.

Sequence the capture so the comparison is meaningful:

Get-Process w3wp | Select Id, PrivateMemorySize64, StartTime
& "$env:ProgramFiles\IIS Express\..\..\dotnet" --version 2>$null
# Prefer dotnet-dump for .NET 5+ or procdump for .NET Framework
procdump64.exe -ma -accepteula <w3wp-pid> C:\dumps\w3wp_baseline.dmp
# ... wait 60-90 minutes under normal load ...
procdump64.exe -ma -accepteula <w3wp-pid> C:\dumps\w3wp_later.dmp

Configuration Changes That Reduce Pressure Without Recycling

If the host is genuinely short of memory, the right answer is either more RAM or fewer concurrent worker processes, not a faster recycle schedule. Setting maximumWorkerProcesses above 1 creates a web garden, which multiplies the per-process baseline and makes total memory worse while breaking in-process session affinity. On a VPS with 4 GB or less, keep the pool at one worker process and size the hardware to the peak concurrent request rate.

Two configuration levers help within a fixed memory budget. Enable the IIS output cache for static and semi-static content so repeated requests do not re-execute, and check that the application’s own caching is bounded — an unbounded MemoryCache with a sliding expiration and no size limit will consume the pool’s entire allowance and then hold it.

Review the plan specifications against your measured peak working set, and treat the memory limit as an alarm rather than a control. A recycle schedule that fires daily by design and never by limit is the target state; anything more frequent is a bug report your application is writing to the event log.

Leave a Comment