Forum Discussion
Possible GENERATE_SERIES memory leak?
Hey all,
We've been experimenting with SQL Server 2022 features and stumbled upon what looks like a memory leak on every execution of the GENERATE_SERIES() function. A script that demonstrates it:
SELECT * FROM sys.dm_os_memory_clerks WHERE [type] = 'MEMORYCLERK_SOSNODE'
DECLARE @i INT = 1
WHILE @i <= 100000 BEGIN
IF (SELECT COUNT(*) FROM GENERATE_SERIES(1, 1, 1)) != 1
BREAK
SET @i += 1
END
-- ~9ΜΒ leaked, ~96 bytes per iteration, on SOS_Node 0
SELECT * FROM sys.dm_os_memory_clerks WHERE [type] = 'MEMORYCLERK_SOSNODE'
It can be reproduced on fresh SQL Server installations, in an empty database with default settings, on:
- Windows
- Linux (Ubuntu/docker)
- SQL Managed Instance on Azure
It does NOT reproduce on Azure SQL.
Using docker, we've tried every build from RTM up to CU12 that was just released, the behavior is exactly the same. The memory does not seem to ever be released. With enough iterations, SQL Server reaches a point where it cannot execute anything anymore. (when testing, lower the max memory setting to get there faster).
Also captured a trace (with Tracking page allocations / freed for MEMORYCLERK_SOSNODE) and using SQLCallStackResolver this call stack appears thousands of times:
00 SqlDK!GenericEvent::PublishAndCallAction
01 SqlDK!XeSosPkg::page_allocated::Publish
02 SqlDK!MemoryClerkInternal::AllocatePagesWithFailureMode
03 SqlDK!MemoryClerkInternal::AllocatePages
04 SqlDK!CMemThread<CMemObj>::PbGetNewPages
05 SqlDK!TVarPageMgr<0>::PbAllocate
06 SqlDK!CMemObj::Alloc
07 SqlDK!CMemThread<CMemObj>::Alloc
08 SqlDK!operator new
09 sqllang!CTVFInfoGenSeries::PtiUserArgumentType
0a sqllang!CSTVFGenSeries::Init
0b sqlmin!CQScanTVFStreamNew::Open
0c sqlmin!CQScanNew::OpenHelper
0d sqlmin!CQScanStreamAggregateNew::Open
0e sqlmin!CQueryScan::UncacheQuery
0f sqllang!CXStmtQuery::SetupQueryScanAndExpression
10 sqllang!CXStmtQuery::InitForExecute
11 sqllang!CXStmtQuery::ErsqExecuteQuery
12 sqllang!CXStmtCondWithQuery::XretExecute
13 sqllang!CExecStmtLoopVars::ExecuteXStmtAndSetXretReturn
14 sqllang!CMsqlExecContext::ExecuteStmts<1,1>
15 sqllang!CMsqlExecContext::FExecute
16 sqllang!CSQLSource::Execute
17 sqllang!process_request
18 sqllang!process_commands_internal
19 sqllang!process_messages
1a SqlDK!SOS_Task::Param::Execute
1b SqlDK!SOS_Scheduler::RunTask
1c SqlDK!SOS_Scheduler::ProcessTasks
1d SqlDK!Worker::EntryPoint
Note the CSTVFGenSeries::Init call. There's no corresponding free operation logged.
Anyone else seeing this?