Diagnosing memory grant starvation (RESOURCE_SEMAPHORE waits) when CPU is low, and fixing it at the query, server and Resource Governor level.
Queries are queued for workspace memory (for sorts and hashes), not CPU: other queries hold large grants and the semaphore has nothing left. I list sys.dm_exec_query_memory_grants to see who holds big grants and who is waiting (grant_time NULL), compare granted_memory_kb with max_used_memory_kb, and find the offenders in Query Store. Usually a few queries over-estimate rows or sort wide columns, so I fix those first. I also check that max server memory is sensible, and use MAX_GRANT_PERCENT or Resource Governor (SQL Server, Azure SQL Managed Instance) as containment, not the cure.
What it means. Sort, hash join, hash aggregate and some other operators need workspace memory. Before a query starts it asks for a grant sized from the optimiser's row and row-size estimates. Grants come from a resource semaphore (by default up to roughly 75% of target memory for all grants together, and 25% per query per Resource Governor workload group). When outstanding grants use it up, new queries wait on RESOURCE_SEMAPHORE before they run a single row. CPU looks idle because nothing is executing — it is all queued.
How I diagnose it.
sys.dm_os_wait_stats (delta over a few minutes) or sys.dm_exec_requests showing RESOURCE_SEMAPHORE.sys.dm_exec_query_memory_grants: rows with grant_time IS NULL are waiting; sort the granted ones by granted_memory_kb. A query granted 6 GB that uses 200 MB (max_used_memory_kb) is the problem.sys.dm_exec_query_resource_semaphores shows the total, available and waiter counts.max_query_max_used_memory, and the plan's MemoryGrantInfo) shows which queries have been asking for big grants historically.max server memory set (and not too low), and is something else (a large plan cache, columnstore, another instance) shrinking the memory available for grants?How I fix it.
nvarchar(max) or wide rows (sort only keys, then join back), and add indexes that supply order so the sort disappears.OPTION (MAX_GRANT_PERCENT = 10) on a known offender, or a Resource Governor workload group with a lower REQUEST_MAX_MEMORY_GRANT_PERCENT for reporting logins. On Azure SQL Database you can't configure Resource Governor, so query fixes and hints are the tools.Production example. A reporting endpoint in an ASP.NET Core API ran a query that sorted on a wide JSON column. Under month-end load, eight concurrent calls each grabbed ~3 GB and every OLTP query that needed a small hash join waited. Projecting only keys in the sort and adding an index that provided the order cut the grant to ~40 MB and the waits disappeared.
Checks that the candidate can diagnose memory grant starvation under concurrency rather than guess at CPU or blocking.
Green flags: names workspace memory and the semaphore; uses sys.dm_exec_query_memory_grants and granted vs used; links grants to cardinality and row-size estimates; fixes queries before hardware; mentions max server memory, Resource Governor and memory grant feedback.
Red flags: thinks it is a lock wait; recommends more CPU or more memory first; can't say where the grant size comes from; puts hints everywhere.
How a grant is sized. The optimiser computes required memory (the minimum to start, e.g. enough for sort buffers) and ideal memory (enough to do all sorts and hashes in memory) from estimated rows × estimated row size. Variable-length columns are estimated at half their declared size, so nvarchar(4000) costs 4,000 bytes per row whether the data is 10 characters or not. The request is capped by the workload group's REQUEST_MAX_MEMORY_GRANT_PERCENT (default 25%).
The semaphore. There are two resource semaphores per resource pool: a small one for cheap queries (under ~5 MB grant, low cost) and a regular one. Waiters are queued by cost; small queries can be stuck behind a big queued query. request_time_out_sec in Resource Governor (or the default derived from query cost, up to 24 times the estimated cost in seconds) eventually fails a waiter with error 8645.
Granted vs used. A grant is reserved up front and held until the operators that need it finish. Over-grants waste memory others need; under-grants spill to tempdb (Sort Warnings / Hash Warnings, spilled_pages in actual plans). Both are estimation problems.
Memory grant feedback. After execution, SQL Server compares the grant with actual use; if the query wasted more than 50% of a large grant, or spilled, the cached plan's grant is adjusted next time. SQL Server 2022 adds percentile-based adjustment (to stop oscillation for parameter-sensitive queries) and persists feedback in Query Store so it survives plan eviction and restarts.
-- Who holds grants and who is waiting
SELECT mg.session_id,
mg.requested_memory_kb,
mg.granted_memory_kb,
mg.max_used_memory_kb,
mg.grant_time, -- NULL = still waiting
mg.wait_time_ms,
mg.dop,
t.text
FROM sys.dm_exec_query_memory_grants AS mg
CROSS APPLY sys.dm_exec_sql_text(mg.sql_handle) AS t
ORDER BY mg.granted_memory_kb DESC;
-- Semaphore capacity and waiters
SELECT resource_semaphore_id, pool_id, target_memory_kb,
available_memory_kb, granted_memory_kb, grantee_count, waiter_count
FROM sys.dm_exec_query_resource_semaphores;
-- Historical offenders from Query Store (memory columns are 8 KB pages)
SELECT TOP (20) q.query_id,
MAX(rs.max_query_max_used_memory) * 8 AS max_used_kb,
SUM(rs.count_executions) AS executions
FROM sys.query_store_runtime_stats AS rs
JOIN sys.query_store_plan AS p ON p.plan_id = rs.plan_id
JOIN sys.query_store_query AS q ON q.query_id = p.query_id
GROUP BY q.query_id
ORDER BY max_used_kb DESC;
-- Containment on a known offender
SELECT OrderId, CustomerId, Total
FROM dbo.Orders
ORDER BY CreatedAt
OPTION (MAX_GRANT_PERCENT = 10);
max server memory at the default on a shared box, so the OS and SQL Server fight and grants shrink.nvarchar(max) or nvarchar(4000) "to be safe" — row-size estimates, and so grants, balloon.MAX_GRANT_PERCENT on everything, which turns over-grants into tempdb spills.MIN_GRANT_PERCENT or MAXDOP changes without measuring granted vs used.The senior framing is: this is a queueing problem caused by estimation. The grant is reserved up front, so the cost of a bad estimate is paid by everyone else, not just the bad query. I'd fix the estimate or the shape (sort fewer, narrower rows) first, use memory grant feedback and hints as containment, and only then talk capacity. I'd also separate workloads — reporting on a readable secondary or a Resource Governor pool — because mixing reporting and OLTP on one semaphore is how a month-end report takes down checkout. And I'd add an alert on RESOURCE_SEMAPHORE wait time so we see it before customers do.
How to tell it (STAR)
**Situation:** At [company], our ordering API slowed to [p95 figure] every month-end while database CPU sat at [percentage]. **Task:** I was asked to find out why. **Action:** I checked wait stats, saw RESOURCE_SEMAPHORE, and used sys.dm_exec_query_memory_grants to find the reporting query holding [size] grants it barely used. I contained it with MAX_GRANT_PERCENT, then fixed the estimate and the wide sort, and moved reporting into its own workload group. **Result:** Grants dropped to [size], API latency at month-end stayed at [figure], and we added an alert so we would see it early.
Last updated 23 Nov 2026.