Come on 64bit so we can leave the mem….

Published Jan 15 2019 12:22 PM 568 Views
Microsoft
First published on MSDN on Aug 26, 2009

I recently saw a question the other day about some errors that indicate memory pressure for the SQL Server Engine and how it might be related to the infamous “MemToLeave” for SQL Server. The problem with this situation was that the errors were happening on a 64bit version of SQL Server. So…I thought it made sense to make sure and dispel any questions or myths about “MemToLeave’ and its relationship to 64bit SQL Server.

Ready for the big answer?

There is no “MemToLeave” for the 64bit version of the SQL Server Engine!

Let me explain why….

This concept has to do with virtual address space (VAS) memory and not physical (or virtual memory) for the computer. We created the concept of “MemToLeave” memory for the virtual address space of a 32bit SQLSERVR.EXE because the virtual address space of a 32bit process in Windows is a limited resource. (2Gb by default, up to 3Gb with 4GT tuning options, and 4Gb in WoW64). And since the VAS is limited, the designers of the engine felt that by default the buffer pool should reserve as much of the VAS it could at startup (provided the amount of physical memory is 2Gb or higher. No sense reserving a big portion of the VAS if the amount of RAM is less than the max VAS for the process). So… since we are reserving most of the VAS for the Buffer Pool, we realized we can’t “take it all”. Why? Memory may be needed in the VAS for other “things” such as:

  • Thread Stacks – When a thread is created, reserved memory is required for a thread stack (this is why you might seen some type of error like 17189 SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems. This error typically means the thread stack could not be reserved)
  • Heaps – Default Windows heap and any other heap created by a DLL
  • SQL Server Multi-Page Allocations (MPA) – The engine itself may allocate memory outside the Buffer Pool because the size required is bigger than a SQL page which is 8Kb.
  • Any other DLL that needs to use VirtualAlloc

What are the other “things” besides thread stacks and SQL MPA? Extended Stored Procedures, Linked Server Providers, COM objects, and host of other edge cases where DLLs get loaded in the process space of SQL Server.

The actual term “MemToLeave” comes from an internal variable name in the code that refers to the total amount of memory we “leave around” for these “things”.

The general algorithm at server startup calls for us to reserve this space using VirtualAlloc() and then after reserving the space for the buffer pool, we free this reservation. Now we have “left it” for these other “things” to reserve and commit memory. The general algorithm for the amount to “leave” is:

thread stack size * ‘max worker threads’ + “the value of –g startup parameter” (measured in Mb)

  • The thread stack size is platform dependent (for example 512Kb on x86 and 2Mb on x64)
  • ‘max worker threads’ when set to 0 is dynamic and based on number of CPUs. See BOL for details
  • The default for –g is 256 (which stands for 256Mb)

By now I hope you can guess that this algorithm and code are not needed for 64bit SQL Engine systems because the virtual address space for 64bit Windows applications are not as limited as with 32bit. In theory, the VAS for 64bit applications is 16 Exabytes .but in practical terms for Windows it is 8TB. (you should also note that the physical memory limit of Windows Server 2008 is 2TB so it is not even possible today to address the maximum VAS for a 64bit Windows application). Because of this, SQL Server doesn’t need to make any special VAS reservation at startup.  In fact, on 64bit SQL Server systems, the engine doesn’t reserve a large amount of memory for the buffer pool as with 32bit. So there is no need to leave any VAS space around since we don’t reserve most of it. You might be fooled on a x64 machine with 2Gb or greater if you monitor the “Virtual Bytes” of SQLSERVR.EXE right after startup. On my laptop with 3Gb of physical RAM, my virtual bytes was close to this number of 3Gb. But that is not the engine reserving space at startup. That is just the natural growth of the Buffer Pool when it allocates memory for overhead structures at startup such as SQLOS and lock manager. When we “grow” the Buffer Pool we reserve space in large, small number of blocks instead of small, larger number of blocks.

I hope this can help you and other bust the myth that “MemToLeave” exists on the x64 versions of SQL Server. It only applies to 32bit versions of SQL Server (even those 32bit versions running in Wow64).

Bob Ward
Microsoft


%3CLINGO-SUB%20id%3D%22lingo-sub-315825%22%20slang%3D%22en-US%22%3ECome%20on%2064bit%20so%20we%20can%20leave%20the%20mem%E2%80%A6.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-315825%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Aug%2026%2C%202009%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EI%20recently%20saw%20a%20question%20the%20other%20day%20about%20some%20errors%20that%20indicate%20memory%20pressure%20for%20the%20SQL%20Server%20Engine%20and%20how%20it%20might%20be%20related%20to%20the%20infamous%20%E2%80%9CMemToLeave%E2%80%9D%20for%20SQL%20Server.%20The%20problem%20with%20this%20situation%20was%20that%20the%20errors%20were%20happening%20on%20a%2064bit%20version%20of%20SQL%20Server.%20So%E2%80%A6I%20thought%20it%20made%20sense%20to%20make%20sure%20and%20dispel%20any%20questions%20or%20myths%20about%20%E2%80%9CMemToLeave%E2%80%99%20and%20its%20relationship%20to%2064bit%20SQL%20Server.%3C%2FP%3E%0A%20%20%3CP%3EReady%20for%20the%20big%20answer%3F%3C%2FP%3E%0A%20%20%3CP%3E%3CSTRONG%3E%20There%20is%20no%20%E2%80%9CMemToLeave%E2%80%9D%20for%20the%2064bit%20version%20of%20the%20SQL%20Server%20Engine!%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%20%20%3CP%3ELet%20me%20explain%20why%E2%80%A6.%3C%2FP%3E%0A%20%20%3CP%3EThis%20concept%20has%20to%20do%20with%20virtual%20address%20space%20(VAS)%20memory%20and%20not%20physical%20(or%20virtual%20memory)%20for%20the%20computer.%20We%20created%20the%20concept%20of%20%E2%80%9CMemToLeave%E2%80%9D%20memory%20for%20the%20virtual%20address%20space%20of%20a%2032bit%20SQLSERVR.EXE%20because%20the%20virtual%20address%20space%20of%20a%2032bit%20process%20in%20Windows%20is%20a%20limited%20resource.%20(2Gb%20by%20default%2C%20up%20to%203Gb%20with%204GT%20tuning%20options%2C%20and%204Gb%20in%20WoW64).%20And%20since%20the%20VAS%20is%20limited%2C%20the%20designers%20of%20the%20engine%20felt%20that%20by%20default%20the%20buffer%20pool%20should%20reserve%20as%20much%20of%20the%20VAS%20it%20could%20at%20startup%20(provided%20the%20amount%20of%20physical%20memory%20is%202Gb%20or%20higher.%20No%20sense%20reserving%20a%20big%20portion%20of%20the%20VAS%20if%20the%20amount%20of%20RAM%20is%20less%20than%20the%20max%20VAS%20for%20the%20process).%20So%E2%80%A6%20since%20we%20are%20reserving%20most%20of%20the%20VAS%20for%20the%20Buffer%20Pool%2C%20we%20realized%20we%20can%E2%80%99t%20%E2%80%9Ctake%20it%20all%E2%80%9D.%20Why%3F%20Memory%20may%20be%20needed%20in%20the%20VAS%20for%20other%20%E2%80%9Cthings%E2%80%9D%20such%20as%3A%3C%2FP%3E%0A%20%20%3CUL%3E%0A%20%20%20%3CLI%3EThread%20Stacks%20%E2%80%93%20When%20a%20thread%20is%20created%2C%20reserved%20memory%20is%20required%20for%20a%20thread%20stack%20(this%20is%20why%20you%20might%20seen%20some%20type%20of%20error%20like%20%3CEM%3E%2017189%20%3C%2FEM%3E%20%3CEM%3E%20SQL%20Server%20failed%20with%20error%20code%200xc0000000%20to%20spawn%20a%20thread%20to%20process%20a%20new%20login%20or%20connection.%20Check%20the%20SQL%20Server%20error%20log%20and%20the%20Windows%20event%20logs%20for%20information%20about%20possible%20related%20problems.%20%3C%2FEM%3E%20This%20error%20typically%20means%20the%20thread%20stack%20could%20not%20be%20reserved)%3C%2FLI%3E%0A%20%20%20%3CLI%3EHeaps%20%E2%80%93%20Default%20Windows%20heap%20and%20any%20other%20heap%20created%20by%20a%20DLL%3C%2FLI%3E%0A%20%20%20%3CLI%3ESQL%20Server%20Multi-Page%20Allocations%20(MPA)%20%E2%80%93%20The%20engine%20itself%20may%20allocate%20memory%20outside%20the%20Buffer%20Pool%20because%20the%20size%20required%20is%20bigger%20than%20a%20SQL%20page%20which%20is%208Kb.%3C%2FLI%3E%0A%20%20%20%3CLI%3EAny%20other%20DLL%20that%20needs%20to%20use%20VirtualAlloc%3C%2FLI%3E%0A%20%20%3C%2FUL%3E%0A%20%20%3CP%3EWhat%20are%20the%20other%20%E2%80%9Cthings%E2%80%9D%20besides%20thread%20stacks%20and%20SQL%20MPA%3F%20Extended%20Stored%20Procedures%2C%20Linked%20Server%20Providers%2C%20COM%20objects%2C%20and%20host%20of%20other%20edge%20cases%20where%20DLLs%20get%20loaded%20in%20the%20process%20space%20of%20SQL%20Server.%3C%2FP%3E%0A%20%20%3CP%3EThe%20actual%20term%20%E2%80%9CMemToLeave%E2%80%9D%20comes%20from%20an%20internal%20variable%20name%20in%20the%20code%20that%20refers%20to%20the%20total%20amount%20of%20memory%20we%20%E2%80%9Cleave%20around%E2%80%9D%20for%20these%20%E2%80%9Cthings%E2%80%9D.%3C%2FP%3E%0A%20%20%3CP%3EThe%20general%20algorithm%20at%20server%20startup%20calls%20for%20us%20to%20reserve%20this%20space%20using%20VirtualAlloc()%20and%20then%20after%20reserving%20the%20space%20for%20the%20buffer%20pool%2C%20we%20free%20this%20reservation.%20Now%20we%20have%20%E2%80%9Cleft%20it%E2%80%9D%20for%20these%20other%20%E2%80%9Cthings%E2%80%9D%20to%20reserve%20and%20commit%20memory.%20The%20general%20algorithm%20for%20the%20amount%20to%20%E2%80%9Cleave%E2%80%9D%20is%3A%3C%2FP%3E%0A%20%20%3CP%3Ethread%20stack%20size%20*%20%E2%80%98max%20worker%20threads%E2%80%99%20%2B%20%E2%80%9Cthe%20value%20of%20%E2%80%93g%20startup%20parameter%E2%80%9D%20(measured%20in%20Mb)%3C%2FP%3E%0A%20%20%3CUL%3E%0A%20%20%20%3CLI%3EThe%20thread%20stack%20size%20is%20platform%20dependent%20(for%20example%20512Kb%20on%20x86%20and%202Mb%20on%20x64)%3C%2FLI%3E%0A%20%20%20%3CLI%3E%E2%80%98max%20worker%20threads%E2%80%99%20when%20set%20to%200%20is%20dynamic%20and%20based%20on%20number%20of%20CPUs.%20See%20BOL%20for%20details%3C%2FLI%3E%0A%20%20%20%3CLI%3EThe%20default%20for%20%E2%80%93g%20is%20256%20(which%20stands%20for%20256Mb)%3C%2FLI%3E%0A%20%20%3C%2FUL%3E%0A%20%20%3CP%3EBy%20now%20I%20hope%20you%20can%20guess%20that%20this%20algorithm%20and%20code%20are%20not%20needed%20for%2064bit%20SQL%20Engine%20systems%20because%20the%20virtual%20address%20space%20for%2064bit%20Windows%20applications%20are%20not%20as%20limited%20as%20with%2032bit.%20In%20theory%2C%20the%20VAS%20for%2064bit%20applications%20is%2016%20%3CEM%3E%20Exabytes%20%3C%2FEM%3E%20.but%20in%20practical%20terms%20for%20Windows%20it%20is%208TB.%20(you%20should%20also%20note%20that%20the%20physical%20memory%20limit%20of%20Windows%20Server%202008%20is%202TB%20so%20it%20is%20not%20even%20possible%20today%20to%20address%20the%20maximum%20VAS%20for%20a%2064bit%20Windows%20application).%20Because%20of%20this%2C%20SQL%20Server%20doesn%E2%80%99t%20need%20to%20make%20any%20special%20VAS%20reservation%20at%20startup.%26nbsp%3B%20In%20fact%2C%20on%2064bit%20SQL%20Server%20systems%2C%20the%20engine%20doesn%E2%80%99t%20reserve%20a%20large%20amount%20of%20memory%20for%20the%20buffer%20pool%20as%20with%2032bit.%20So%20there%20is%20no%20need%20to%20leave%20any%20VAS%20space%20around%20since%20we%20don%E2%80%99t%20reserve%20most%20of%20it.%20You%20might%20be%20fooled%20on%20a%20x64%20machine%20with%202Gb%20or%20greater%20if%20you%20monitor%20the%20%E2%80%9CVirtual%20Bytes%E2%80%9D%20of%20SQLSERVR.EXE%20right%20after%20startup.%20On%20my%20laptop%20with%203Gb%20of%20physical%20RAM%2C%20my%20virtual%20bytes%20was%20close%20to%20this%20number%20of%203Gb.%20But%20that%20is%20not%20the%20engine%20reserving%20space%20at%20startup.%20That%20is%20just%20the%20natural%20growth%20of%20the%20Buffer%20Pool%20when%20it%20allocates%20memory%20for%20overhead%20structures%20at%20startup%20such%20as%20SQLOS%20and%20lock%20manager.%20When%20we%20%E2%80%9Cgrow%E2%80%9D%20the%20Buffer%20Pool%20we%20reserve%20space%20in%20large%2C%20small%20number%20of%20blocks%20instead%20of%20small%2C%20larger%20number%20of%20blocks.%3C%2FP%3E%0A%20%20%3CP%3EI%20hope%20this%20can%20help%20you%20and%20other%20bust%20the%20myth%20that%20%E2%80%9CMemToLeave%E2%80%9D%20exists%20on%20the%20x64%20versions%20of%20SQL%20Server.%20It%20only%20applies%20to%2032bit%20versions%20of%20SQL%20Server%20(even%20those%2032bit%20versions%20running%20in%20Wow64).%3C%2FP%3E%0A%20%20%3CP%3EBob%20Ward%20%3CBR%20%2F%3E%20Microsoft%3C%2FP%3E%0A%20%20%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-315825%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Aug%2026%2C%202009%20I%20recently%20saw%20a%20question%20the%20other%20day%20about%20some%20errors%20that%20indicate%20memory%20pressure%20for%20the%20SQL%20Server%20Engine%20and%20how%20it%20might%20be%20related%20to%20the%20infamous%20%E2%80%9CMemToLeave%E2%80%9D%20for%20SQL%20Server.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-315825%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Ememory%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESQL%20OS%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Jan 15 2019 12:22 PM
Updated by: