IIS App Pool Recycle Schedule and Memory Leak Diagnosis

(Get-Counter '\Process(w3wp*)\Private Bytes' -MaxSamples 5).CounterSamples | Format-Table InstanceName,CookedValue — run that on any IIS server complaining about memory and you will see whether the pool is climbing steadily or oscillating. Recycling is the mechanism that copes with the climb; it does not fix it. This guide covers how to set a recycle schedule and memory limits deliberately, then how to prove whether you have a leak or merely a large working set. For hosts running several pools, see Windows VPS plans with RDP included and size RAM against measured peaks, not guesses.

What a recycle actually does, and what it does not

A recycle starts a second worker process with the same application pool identity, waits for the old one to drain, then retires it. Sessions in InProc state, in-memory caches and static data all disappear. That is why a recycle “fixes” a slow application for ten minutes and why it double-counts memory during the overlap: both processes hold their allocations at once.

Check the current state of a pool before you change anything: Get-IISAppPool lists pools and states, (Get-Process w3wp -IncludeUserName).StartTime shows how long each worker has run, and Get-WinEvent -LogName System -MaxEvents 50 | Where-Object Id -eq 5074 shows who triggered recent recycles. If recycles are already happening hourly, the schedule is masking something.

Set the schedule in applicationHost.config, not the GUI

The GUI writes the same XML, but doing it in applicationHost.config keeps the change reviewable. A time-based schedule belongs in a periodicRestart element with time values, for example time="03:00:00" and time="15:00:00" for a twice-daily cycle. Avoid the default 1740-minute (29-hour) interval: it fires at arbitrary times, so you can never correlate a slowdown with it.

Apply through %windir%\system32\inetsrv\appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='MyPool'].recycling.periodicRestart.schedule.[value='03:00:00']" /commit:apphost. Then confirm with appcmd list apppool "MyPool" /config. Keep the schedule outside business hours in the site’s dominant timezone, and note that it is local server time, not UTC.

Memory limits: private vs virtual, and why both matter

Two limits exist: privateMemory (committed private bytes) and virtualMemory (virtual address space). Private is the one that maps to how much RAM you actually need. A sensible starting limit is roughly 1.5x the observed steady state, so a pool that sits at 600 MB after an hour gets privateMemory="1048576" (1 GB in KB). Set it too low and the pool recycles mid-request under load.

Enable logEventOnRecycle for the reasons you care about: memory, privateMemory, onDemand, configChange. Without it, Event ID 5079/5080 gives you no reason code. Add disallowOverlappingRotation="false" only when you have headroom; the default overlap is what causes the brief doubling in process count during recycle.

Distinguish a leak from a large steady working set

A leak keeps climbing until the limit is hit, every time, from the same starting point. A large working set climbs on cold start and then flattens. Sample hourly for 24 hours: Get-Counter '\Process(w3wp*)\Private Bytes' -SampleInterval 3600 -MaxSamples 24 | Export-Csv pool.csv. If the delta after warm-up is under a few percent per hour, you do not have a leak.

If it does climb, identify whether managed or native. Compare .NET CLR Memory(_Global)\# Bytes in all Heaps against Private Bytes: rising managed heap is a managed leak (static collections, event handlers never unsubscribed, caches without eviction); rising private bytes with a flat heap is native — a COM interop handle, a Graphics object, or unmanaged buffer misuse.

Capture a dump when the limit is reached, not afterwards

A dump taken after the recycle tells you nothing, because the process is gone. Configure a trigger so the dump is written while the pool is still alive: procdump -accepteula -ma -p w3wp.exe -m 20 -c 20 -n 1 C:\dumps writes a full dump at the first working-set event once the process passes ~20 MB of growth over the previous trigger, which in practice fires on the climb rather than at the limit.

Better, use the built-in WER local dumps key: HKLM:\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\w3wp.exe with DumpType=2. Then analyse with WinDbg and !dumpheap -stat for managed, or !heap -s for native. Two dumps taken an hour apart let you diff growth by type instead of guessing.

Reduce the pressure before you widen the limits

Before adding RAM, check three cheap wins. Output caching and response compression reduce per-request allocations. ASP.NET session state moved to a database or distributed provider stops InProc requirements and makes recycles invisible to users. Running multiple worker processes (web garden) is usually the wrong answer — affinitiy problems outweigh the isolation.

Also confirm you are not running 32-bit pools on a 64-bit OS by accident: appcmd list apppool /config /xml | Select-String enable32BitAppOnWin64. A 32-bit worker caps at 2 GB and will recycle constantly on a site that needs twice that. Turn it off unless a legacy ISAPI or COM component demands it.

Alert on recycles instead of discovering them

A recycle storm is a symptom worth an alert. Watch Event ID 5074 frequency in the System log and worker process start time: if Get-Process w3wp shows an uptime under 30 minutes during business hours, something is cycling. Wire that into your monitoring rather than waiting for a customer call.

A stable configuration: a schedule at a chosen hour, a private memory limit at 1.5x steady state, recycle reason logging on, session state out of process, and a dump trigger for the one pool that misbehaves. Review the full Windows Server specs and pricing if the fix is simply more RAM, and re-measure rather than assuming the leak is gone.

Leave a Comment