Advertisement
joachip

Dynamically limit MSSQL memory max size to a reasonable size

Feb 6th, 2019
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.73 KB | None | 0 0
  1. -- If your SQL server runs on the same machine as many other things, but tends to steal too much memory, run this script every month or week to set the max amount of memory to a large but more reasonable size.
  2.  
  3. EXEC sp_configure 'show advanced options', 1;
  4. GO
  5.  
  6. DECLARE @physmem INT = (SELECT total_physical_memory_kb FROM sys.dm_os_sys_memory);
  7. DECLARE @availmem INT = (SELECT available_physical_memory_kb FROM sys.dm_os_sys_memory);
  8. DECLARE @suggested_sql_memory_mb INT = (@availmem*3 + @physmem) / 7 / 1024;
  9. SELECT @physmem/1024/1024 AS [Physical memory, GB], @availmem/1024/1024 AS [Available, GB], @suggested_sql_memory_mb/1024 AS [Suggested max memory, GB];
  10. EXEC sp_configure 'max server memory', @suggested_sql_memory_mb;
  11. RECONFIGURE;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement