{"id":1766,"date":"2016-01-19T16:30:00","date_gmt":"2016-01-19T14:30:00","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/?p=1766"},"modified":"2016-01-19T19:03:37","modified_gmt":"2016-01-19T17:03:37","slug":"stop-using-task-manager-to-check-sqls-memory-usage","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2016\/01\/19\/stop-using-task-manager-to-check-sqls-memory-usage\/","title":{"rendered":"Stop using Task Manager to check SQL&#8217;s memory usage!"},"content":{"rendered":"<p>There\u2019s two fairly common questions I see on the forums around SQL Server\u2019s memory usage. Either the question asks why SQL\u2019s using too much memory, or why it\u2019s using too little.<\/p>\n<p>Too much memory isn\u2019t usually a real problem, it\u2019s often due to max server memory being left at its default of 2048TB, along with a lack of understanding of how SQL uses memory.<\/p>\n<p>Too little memory used is also often not a real problem, rather it\u2019s usually from using the wrong tools to check SQL Server\u2019s memory usage.<\/p>\n<p>Let\u2019s start by looking at an example.<\/p>\n<p>This particular server has 16 GB of memory, and SQL Server\u2019s max server memory is set to 10GB. Since the last restart of the instance, I\u2019ve run SELECT * FROM .. against every table in a 30GB database. That should have warmed the cache up nicely.<\/p>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/01\/MemoryTaskManager.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"MemoryTaskManager\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/01\/MemoryTaskManager_thumb.png\" alt=\"MemoryTaskManager\" width=\"484\" height=\"324\" border=\"0\" \/><\/a><\/p>\n<p>Err, what? SQL Server\u2019s not even using 100MB? I\u2019ve just read 30GB of data and SQL Server\u2019s not even using 1% of it\u2019s allowed memory!!!<\/p>\n<p>Or is it?<\/p>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/01\/TotalServerMemory.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"TotalServerMemory\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/01\/TotalServerMemory_thumb.png\" alt=\"TotalServerMemory\" width=\"484\" height=\"68\" border=\"0\" \/><\/a><\/p>\n<p>A look at perfmon shows a completely different picture. Total and target server memory are both at 10GB. So why is Task Manager showing such a low figure?<\/p>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/01\/LockedPagesInErrorLog.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"LockedPagesInErrorLog\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/01\/LockedPagesInErrorLog_thumb.png\" alt=\"LockedPagesInErrorLog\" width=\"484\" height=\"100\" border=\"0\" \/><\/a><\/p>\n<p>The service account that SQL\u2019s running under has been granted the Lock Pages in Memory permission. This means that SQL\u2019s not using the normal Windows memory routines to allocate memory.<\/p>\n<p>Normally, SQL Server and other Windows applications allocate memory using the VirtualAlloc API call. This allocates virtual memory which is pageable. When SQL Server has been given the Lock Pages in Memory permission, it doesn\u2019t use VirtualAlloc to allocate memory. Instead it uses the API call AllocateUserPhysicalPages. Memory allocated with this API call is not pageable, it has to remain in physical memory.<\/p>\n<p>Task Manager\u2019s memory counters (the Working Set ones) only show memory that\u2019s been allocated using VirtualAlloc. Hence, when SQL Server has the Lock Pages in Memory permission and is allocating most of its memory using AllocateUserPhysicalPages, its memory usage in Task Manager will appear to be extraordinarily low. As far as I recall, in that case it\u2019s only the non-buffer memory which is still allocated with VirtualAlloc, and that\u2019s just things like the thread stacks, the CLR memory, backup buffers, and similar. It\u2019s not the buffer pool. The buffer pool gets allocated with AllocateUserPhysicalPages.<\/p>\n<p>If I remove the Lock Pages in Memory and re-run the test, Task Manager shows completely different values<\/p>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/01\/MemoryTaskManager_NoLockedPages.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border: 0px;\" title=\"MemoryTaskManager_NoLockedPages\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/01\/MemoryTaskManager_NoLockedPages_thumb.png\" alt=\"MemoryTaskManager_NoLockedPages\" width=\"484\" height=\"324\" border=\"0\" \/><\/a><\/p>\n<p>Now the buffer pool is being allocated with VirtualAlloc and so Task Manager shows the full 10GB of memory usage.<\/p>\n<p>In summary, Task Manager can show a completely incorrect value for SQL Server\u2019s memory usage if the SQL service account has the Lock Pages in Memory permission. This can lead to a lot of wasted time if it is concluded that Task Manager is correct and SQL Server is using little memory.<\/p>\n<p>Rather leave Task Manager alone and use perfmon and the DMVs to check what SQL Server\u2019s memory allocation actually is. They\u2019ll both be correct whether Lock Pages are being used or not.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There\u2019s two fairly common questions I see on the forums around SQL Server\u2019s memory usage. Either the question asks why SQL\u2019s using too much memory, or why it\u2019s using too little. Too much memory isn\u2019t usually a real problem, it\u2019s&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2016\/01\/19\/stop-using-task-manager-to-check-sqls-memory-usage\/\">(Read more)<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New Blog Post: Stop using Task Manager to check SQL's memory usage!","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[29,15,16],"tags":[],"class_list":["post-1766","post","type-post","status-publish","format-standard","hentry","category-internals","category-sql-server","category-syndication"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-su","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/1766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=1766"}],"version-history":[{"count":4,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/1766\/revisions"}],"predecessor-version":[{"id":1790,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/1766\/revisions\/1790"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=1766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=1766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=1766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}