How It Works: SQL Server (NUMA Local, Foreign and Away Memory Blocks)

Published Jan 15 2019 02:53 PM 1,014 Views
Microsoft
First published on MSDN on Dec 13, 2012

Applies to: SQL 2005, 2008, 2008 R2, and SQL 2012 versions.

The NODE an operating system page, physically belongs to can be acquired using the QueryVirtualMemoryEx Windows API.  SQL Server uses this API to track locality of memory allocations.

This blog is a very high level view of SQL Server behavior but I think it provides a sufficient picture as to what is happening.

This tracking is important to performance because SQL Server makes reasonable attempts to use node, local memory whenever possible.   Access to memory on a remote node (remote memory/foreign memory) takes longer which can lead to, small, unwanted delays in query processing.

When a block of memory is allocated SQL Server looks at each operating system page and sorts it according the physical memory node assignments on the system.   There are a couple of states that the memory manager uses and in doing so the behavior varies.

Type Description
Local The memory is physically present on the same NUMA node as the SQL Server worker.
Away The memory is known to belong current node but allocated from a remote node and the memory manager is still in the GROWTH (often initial ramp-up phase.)   The memory is held on the away list and used as needed by SQL Server requires.
Taken Away The memory the workers on the local node allocated and placed on appropriate away buffers of remote nodes.
Foreign The memory is known to belong to a remote node and SQL Server has transitioned from the GROWTH phase because max server memory has been achieved.  The memory block is being used remotely.

There is a subtle distinction between Away and Foreign blocks related to ‘is SQL Server memory still growing’ or has ‘SQL Server memory reached the target memory level.’

The difference directly drives how and when SQL Server consumes the memory.   When SQL Server has not reached the max server memory the away buffers can be set aside on their appropriate nodes.  Let me try to explain better.

Allocate Memory

If(Memory is remote to the node)
Place on away list
Loop back and try to allocate another block
else
Use the local memory block

The reason to place blocks on an away list and not use them right away helps prevent a bad cycle with the operating system or lock into remote memory in the local node.   If SQL Server released the block it may go onto the operating system free list.   The very next call to allocate memory could return the same block to SQL Server, preventing forward process towards local memory allocation.

allocations on the local node can acquire away buffers from the nodes list and once SQL Server reaches the target memory level for the instance the memory manager transitions away buffers as well.  This transition signals that away buffers can be used by their assigned, physical nodes.   The memory continues to be balanced across all memory nodes as appropriate.   Only at this point are any pages, that can’t be directly returned to their local nodes, considered Foreign.

The foreign is memory known to belong to another node and being activity used outside its home node.

The following is a snippet of data from dbcc memorystatus showing the behavior.

Type Description
Away Committed The amount of memory allocated from ‘this’ physical memory node that is currently assigned to a remote memory node.
Taken Away Committed The amount of memory that ‘this’ node has set aside because it knows the memory block belongs to another node.

Knowing the Away Committed and Taken Away Committed values you can look across all memory nodes and understand the current remote vs local memory allocation pattern.

Once the memory manager transitions from GROWTH and the distribution of away blocks occur the output changes to show any foreign memory usage.   In this example only 32kb of memory remains foreign to node 1 after all balancing and memory block assignment is complete.

My reaction now is ‘BIG DEAL WHY DO I CARE?’

I pointed this out for a couple of reasons.

First, the away buffer counts are not part of the performance counters, only foreign memory counters.   To view the away buffer counts you need to capture dbcc memorystatus details.  You could also look at sys.dm_os_memory_nodes or other locations to calculate the difference in free list size, committed and target on the node to determine possible foreign or away block sizes and counts.

Second, during the GROWTH phase, the free and target values for a node with a large ‘Taken Away Committed’ count will be much larger than 1/nth of your memory nodes.  A great indicator that this node has a larger than expected, remote memory offering.

Third, a single query with larger than expected RAM usage may appear.  In my lab testing (256GB RAM system) a query that only needed 30GB of RAM would cause my SQL Server to use ~100GB of total memory.  ~70GB of the memory was on my away lists and not actively used by SQL Server to support the query.   This is expected because I told SQL Server it could use 225GB of memory (max server memory.)  Since SQL Server has not reached that limit putting buffers on the away list is not a problem.   As soon as SQL Server transitions from the GROWTH phase these buffers are readily available, mostly on their home, physical node.

Bob Dorr - Principal SQL Server Escalation Engineer

1 Comment
Microsoft

Thanks to share this information.  Is there a counter or wait type on foreign memory allocation attempt or spin?

%3CLINGO-SUB%20id%3D%22lingo-sub-317461%22%20slang%3D%22en-US%22%3EHow%20It%20Works%3A%20SQL%20Server%20(NUMA%20Local%2C%20Foreign%20and%20Away%20Memory%20Blocks)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-317461%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%20Dec%2013%2C%202012%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3E%3CSTRONG%3E%20Applies%20to%3A%20%3C%2FSTRONG%3E%20SQL%202005%2C%202008%2C%202008%20R2%2C%20and%20SQL%202012%20versions.%3C%2FP%3E%0A%20%20%3CP%3EThe%20NODE%20an%20operating%20system%20page%2C%20physically%20belongs%20to%20can%20be%20acquired%20using%20the%20%3CEM%3E%20QueryVirtualMemoryEx%20%3C%2FEM%3E%20Windows%20API.%26nbsp%3B%20SQL%20Server%20uses%20this%20API%20to%20track%20locality%20of%20memory%20allocations.%3C%2FP%3E%0A%20%20%3CP%3EThis%20blog%20is%20a%20very%20high%20level%20view%20of%20SQL%20Server%20behavior%20but%20I%20think%20it%20provides%20a%20sufficient%20picture%20as%20to%20what%20is%20happening.%3C%2FP%3E%0A%20%20%3CP%3EThis%20tracking%20is%20important%20to%20performance%20because%20SQL%20Server%20makes%20reasonable%20attempts%20to%20use%20node%2C%20local%20memory%20whenever%20possible.%26nbsp%3B%26nbsp%3B%20Access%20to%20memory%20on%20a%20remote%20node%20(remote%20memory%2Fforeign%20memory)%20takes%20longer%20which%20can%20lead%20to%2C%20small%2C%20unwanted%20delays%20in%20query%20processing.%3C%2FP%3E%0A%20%20%3CP%3EWhen%20a%20block%20of%20memory%20is%20allocated%20SQL%20Server%20looks%20at%20each%20operating%20system%20page%20and%20sorts%20it%20according%20the%20physical%20memory%20node%20assignments%20on%20the%20system.%26nbsp%3B%26nbsp%3B%20There%20are%20a%20couple%20of%20states%20that%20the%20memory%20manager%20uses%20and%20in%20doing%20so%20the%20behavior%20varies.%3C%2FP%3E%3CSTRONG%3E%20%3C%2FSTRONG%3E%3CTABLE%3E%0A%20%20%20%3CTBODY%3E%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CSTRONG%3E%20Type%20%3C%2FSTRONG%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CSTRONG%3E%20Description%20%3C%2FSTRONG%3E%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CSTRONG%3E%20Local%20%3C%2FSTRONG%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EThe%20memory%20is%20physically%20present%20on%20the%20same%20NUMA%20node%20as%20the%20SQL%20Server%20worker.%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CSTRONG%3E%20Away%20%3C%2FSTRONG%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EThe%20memory%20is%20known%20to%20belong%20current%20node%20but%20allocated%20from%20a%20remote%20node%20and%20the%20memory%20manager%20is%20still%20in%20the%20GROWTH%20(often%20initial%20ramp-up%20phase.)%26nbsp%3B%26nbsp%3B%20The%20memory%20is%20held%20on%20the%20away%20list%20and%20used%20as%20needed%20by%20SQL%20Server%20requires.%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CSTRONG%3E%20Taken%20Away%20%3C%2FSTRONG%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EThe%20memory%20the%20workers%20on%20the%20local%20node%20allocated%20and%20placed%20on%20appropriate%20away%20buffers%20of%20remote%20nodes.%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CSTRONG%3E%20Foreign%20%3C%2FSTRONG%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EThe%20memory%20is%20known%20to%20belong%20to%20a%20remote%20node%20and%20SQL%20Server%20has%20transitioned%20from%20the%20GROWTH%20phase%20because%20max%20server%20memory%20has%20been%20achieved.%26nbsp%3B%20The%20memory%20block%20is%20being%20used%20remotely.%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%3C%2FTBODY%3E%3C%2FTABLE%3E%0A%20%20%3CP%3EThere%20is%20a%20subtle%20distinction%20between%20Away%20and%20Foreign%20blocks%20related%20to%20%E2%80%98is%20SQL%20Server%20memory%20still%20growing%E2%80%99%20or%20has%20%E2%80%98SQL%20Server%20memory%20reached%20the%20target%20memory%20level.%E2%80%99%3C%2FP%3E%0A%20%20%3CP%3EThe%20difference%20directly%20drives%20how%20and%20when%20SQL%20Server%20consumes%20the%20memory.%26nbsp%3B%26nbsp%3B%20When%20SQL%20Server%20has%20not%20reached%20the%20max%20server%20memory%20the%20away%20buffers%20can%20be%20set%20aside%20on%20their%20appropriate%20nodes.%26nbsp%3B%20Let%20me%20try%20to%20explain%20better.%3C%2FP%3E%0A%20%20%3CBLOCKQUOTE%3E%0A%20%20%20%3CP%3EAllocate%20Memory%3C%2FP%3E%0A%20%20%20%3CP%3EIf(Memory%20is%20remote%20to%20the%20node)%20%3CBR%20%2F%3E%20Place%20on%20away%20list%20%3CBR%20%2F%3E%20Loop%20back%20and%20try%20to%20allocate%20another%20block%20%3CBR%20%2F%3E%20else%20%3CBR%20%2F%3E%20Use%20the%20local%20memory%20block%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%0A%20%20%3CP%3EThe%20reason%20to%20place%20blocks%20on%20an%20away%20list%20and%20not%20use%20them%20right%20away%20helps%20prevent%20a%20bad%20cycle%20with%20the%20operating%20system%20or%20lock%20into%20remote%20memory%20in%20the%20local%20node.%26nbsp%3B%26nbsp%3B%20If%20SQL%20Server%20released%20the%20block%20it%20may%20go%20onto%20the%20operating%20system%20free%20list.%26nbsp%3B%26nbsp%3B%20The%20very%20next%20call%20to%20allocate%20memory%20could%20return%20the%20same%20block%20to%20SQL%20Server%2C%20preventing%20forward%20process%20towards%20local%20memory%20allocation.%3C%2FP%3E%0A%20%20%3CP%3Eallocations%20on%20the%20local%20node%20can%20acquire%20away%20buffers%20from%20the%20nodes%20list%20and%20once%20SQL%20Server%20reaches%20the%20target%20memory%20level%20for%20the%20instance%20the%20memory%20manager%20transitions%20away%20buffers%20as%20well.%26nbsp%3B%20This%20transition%20signals%20that%20away%20buffers%20can%20be%20used%20by%20their%20assigned%2C%20physical%20nodes.%26nbsp%3B%26nbsp%3B%20The%20memory%20continues%20to%20be%20balanced%20across%20all%20memory%20nodes%20as%20appropriate.%26nbsp%3B%26nbsp%3B%20Only%20at%20this%20point%20are%20any%20pages%2C%20that%20can%E2%80%99t%20be%20directly%20returned%20to%20their%20local%20nodes%2C%20considered%20Foreign.%3C%2FP%3E%0A%20%20%3CP%3EThe%20foreign%20is%20memory%20known%20to%20belong%20to%20another%20node%20and%20being%20activity%20used%20outside%20its%20home%20node.%3C%2FP%3E%0A%20%20%3CP%3EThe%20following%20is%20a%20snippet%20of%20data%20from%20%3CEM%3E%20dbcc%20memorystatus%20%3C%2FEM%3E%20showing%20the%20behavior.%3C%2FP%3E%0A%20%20%3CTABLE%3E%0A%20%20%20%3CTBODY%3E%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CSTRONG%3E%20Type%20%3C%2FSTRONG%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CSTRONG%3E%20Description%20%3C%2FSTRONG%3E%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CSTRONG%3E%20Away%20Committed%20%3C%2FSTRONG%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EThe%20amount%20of%20memory%20allocated%20from%20%E2%80%98this%E2%80%99%20physical%20memory%20node%20that%20is%20currently%20assigned%20to%20a%20remote%20memory%20node.%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CSTRONG%3E%20Taken%20Away%20Committed%20%3C%2FSTRONG%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EThe%20amount%20of%20memory%20that%20%E2%80%98this%E2%80%99%20node%20has%20set%20aside%20because%20it%20knows%20the%20memory%20block%20belongs%20to%20another%20node.%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%3C%2FTBODY%3E%3C%2FTABLE%3E%0A%20%20%3CP%3EKnowing%20the%20Away%20Committed%20and%20Taken%20Away%20Committed%20values%20you%20can%20look%20across%20all%20memory%20nodes%20and%20understand%20the%20current%20remote%20vs%20local%20memory%20allocation%20pattern.%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67437i0EF3C8BF7DFC8CC6%22%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EOnce%20the%20memory%20manager%20transitions%20from%20GROWTH%20and%20the%20distribution%20of%20away%20blocks%20occur%20the%20output%20changes%20to%20show%20any%20foreign%20memory%20usage.%26nbsp%3B%26nbsp%3B%20In%20this%20example%20only%2032kb%20of%20memory%20remains%20foreign%20to%20node%201%20after%20all%20balancing%20and%20memory%20block%20assignment%20is%20complete.%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67439i9BE5A05316503765%22%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3E%3CSTRONG%3E%20My%20reaction%20now%20is%20%E2%80%98BIG%20DEAL%20WHY%20DO%20I%20CARE%3F%E2%80%99%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%20%20%3CP%3EI%20pointed%20this%20out%20for%20a%20couple%20of%20reasons.%3C%2FP%3E%0A%20%20%3CP%3EFirst%2C%20the%20away%20buffer%20counts%20are%20not%20part%20of%20the%20performance%20counters%2C%20only%20foreign%20memory%20counters.%26nbsp%3B%26nbsp%3B%20To%20view%20the%20away%20buffer%20counts%20you%20need%20to%20capture%20dbcc%20memorystatus%20details.%26nbsp%3B%20You%20could%20also%20look%20at%20sys.dm_os_memory_nodes%20or%20other%20locations%20to%20calculate%20the%20difference%20in%20free%20list%20size%2C%20committed%20and%20target%20on%20the%20node%20to%20determine%20possible%20foreign%20or%20away%20block%20sizes%20and%20counts.%3C%2FP%3E%0A%20%20%3CP%3ESecond%2C%20during%20the%20GROWTH%20phase%2C%20the%20free%20and%20target%20values%20for%20a%20node%20with%20a%20large%20%E2%80%98Taken%20Away%20Committed%E2%80%99%20count%20will%20be%20much%20larger%20than%201%2Fnth%20of%20your%20memory%20nodes.%26nbsp%3B%20A%20great%20indicator%20that%20this%20node%20has%20a%20larger%20than%20expected%2C%20remote%20memory%20offering.%3C%2FP%3E%0A%20%20%3CP%3EThird%2C%20a%20single%20query%20with%20larger%20than%20expected%20RAM%20usage%20may%20appear.%26nbsp%3B%20In%20my%20lab%20testing%20(256GB%20RAM%20system)%20a%20query%20that%20only%20needed%2030GB%20of%20RAM%20would%20cause%20my%20SQL%20Server%20to%20use%20~100GB%20of%20total%20memory.%26nbsp%3B%20~70GB%20of%20the%20memory%20was%20on%20my%20away%20lists%20and%20not%20actively%20used%20by%20SQL%20Server%20to%20support%20the%20query.%26nbsp%3B%26nbsp%3B%20This%20is%20expected%20because%20I%20told%20SQL%20Server%20it%20could%20use%20225GB%20of%20memory%20(max%20server%20memory.)%26nbsp%3B%20Since%20SQL%20Server%20has%20not%20reached%20that%20limit%20putting%20buffers%20on%20the%20away%20list%20is%20not%20a%20problem.%26nbsp%3B%26nbsp%3B%20As%20soon%20as%20SQL%20Server%20transitions%20from%20the%20GROWTH%20phase%20these%20buffers%20are%20readily%20available%2C%20mostly%20on%20their%20home%2C%20physical%20node.%3C%2FP%3E%0A%20%20%3CP%3EBob%20Dorr%20-%20Principal%20SQL%20Server%20Escalation%20Engineer%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-317461%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Dec%2013%2C%202012%20Applies%20to%3A%20SQL%202005%2C%202008%2C%202008%20R2%2C%20and%20SQL%202012%20versions.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-317461%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%3CLINGO-SUB%20id%3D%22lingo-sub-2179768%22%20slang%3D%22en-US%22%3ERe%3A%20How%20It%20Works%3A%20SQL%20Server%20(NUMA%20Local%2C%20Foreign%20and%20Away%20Memory%20Blocks)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2179768%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20to%20share%20this%20information.%26nbsp%3B%20Is%20there%20a%20counter%20or%20wait%20type%20on%20foreign%20memory%20allocation%20attempt%20or%20spin%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Version history
Last update:
‎Jan 15 2019 02:53 PM
Updated by: