Sql-server – SQL server Memory components

memorysql-server-2012

I am trying to understand different memory components in sql,so far my research lead me understand below ,If SO DBAs can confirm my understanding/add something if i am missing,i will be really gratefull

Total components which use memory in SQL can be found through below query,were each distinct clerk means a component.

select * from sys.dm_os_memory_clerks

SQL dynamically calculates buffer pool memory +memory used for storing plans using max memory settings.This is the part which confuses me .so i am taking one scenario to explain in detail

Test case:
Suppose my SQL is having 64 GB ram and i have capped max server setting to 58 GB,then below are the events that occur

  1. Buffer Pool plus query plans will be able to use maximum of 58 GB ram
  2. Remaining 6 GB of RAM will be used to for OS and other SQL components

So my questions are like below

  1. Since sql server calculates number of locks during startup,will the memory used by locks fall into 6 GB category ?
  2. Suppose i have executed a query which requires more memory ,will this memory be allocated from 6 Gb or from buffer pool (58 Gb ) memory or will this be spilled to disk ?
  3. There are different memory clerks all shown above in screenshot,will they fall in 6 GB category ?
  4. In 64 Bit systems,does Sql still calculate memtoleave option or with the exception of few memory clerks ,everything falls under
    58 GB category ?

  5. Further largest consumer of these clerks would be buffer pool and
    query plans. If other clerks face memory pressure or need more
    memory,memory allocation will come through buffer pool clerk in
    terms of stolen pages if they pages flushed to disk to accomodate
    this request.

Below are total references i had

–links in question too

Best Answer

To start with you have SQL Server 2012 and memory architecture changed significantly in this version there is only one memory allocator (yes there is VAS allocator as well but I still would say there is only one 'Any Page' allocator) and this does Almost All memory allocations. Here is what BOL has to say. Please read this definition clearly

Max server memory controls SQL Server memory allocation, including the buffer pool, compile memory, all caches, qe memory grants, lock manager memory, and CLR memory (basically any “clerk” as found in dm_os_memory_clerks). Memory for thread stacks, Memory heaps, linked server providers other than SQL Server, or any memory allocated by a “non SQL Server” DLL is not controlled by max server memory.


For your question

Total components which use memory in SQL can be found through below query,were each distinct clerk means a component.

From above definition please note that not all memory allocations are tracked by clerks. But for practical purposes you can safely assume sys.dm_os_memory_clerks is place to look for memory breakup as used by various components.Every component that allocates a significant amount of memory must create its own memory clerk and allocate all its memory by using the clerk interfaces. Frequently, components create their corresponding clerks at the time SQL Server is started.

Please note clerks only track memory allocated to various components they never do allocation of SQL Server memory.

Buffer Pool plus query plans will be able to use maximum of 58 GB ram

Not only buffer pool and query plans but almost all components which require memory will now be catered from max server memory setting. Specially SQLCLR, memory to this is now allocated from max server memory setting previously in SQL Server 2008 R2 and before memory was allocated from outside max server memory setting.

Since sql server calculates number of locks during startup,will the memory used by locks fall into 6 GB category ?

No memory will be allocated by Max server memory setting(will fall under 58G category) not from outside this limit. I am not using word buffer pool as from SQL Server 2012 buffer pool is only consumer not allocator of memory so its better to use max server memory

Suppose i have executed a query which requires more memory ,will this memory be allocated from 6 Gb or from buffer pool (58 Gb ) memory or will this be spilled to disk ?

Well it depends on what components require memory. Please read the definition on top if resource that requires memory is Thread stack, memory heap, Third Party linked server provider the memory would be allocated from outside max server memory limit otherwise from inside. When query requires more memory Lazywriter would start ageing out unused pages and internal threads would ask various memory consumers to free some memory. This would create new space in buffer pool and SQL Server can bring would bring new pages into the memory.

There are different memory clerks all shown above in screenshot,will they fall in 6 GB category ?

You have not added screenshot but when you query sys.dm_os_memory_clerks whatever clerks information come they all track memory allocated to various components and would fall under max server memory limit category (58 G limit category).

In 64 Bit systems,does Sql still calculate memtoleave option or with the exception of few memory clerks ,everything falls under 58 GB category ?

NO THERE IS NO MEMORYTOLEAVE IN 64 BIT. Please read This Blogs.msdn article for more details.

Further largest consumer of these clerks would be buffer pool and query plans. If other clerks face memory pressure or need more memory,memory allocation will come through buffer pool clerk in terms of stolen pages if they pages flushed to disk to accomodate this request.

No it is not exactly like that. Yes in SQL Server 2012 as well largest consumer of memory is buffer pool. Stolen memory is memory utilized by SQL Server to do miscellaneous tasks NOT REALTED to SQL Server database. These other Miscellaneous task might include sorting, has joins etc. So in case of memory pressure it might or might not take stolen memory depending on memory pressure.