Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server not releasing memory after query executes

I think I have a basic question here that many might have encountered. When I run a query in SQL Server it will load in memory all the data it needs for query execution (for example, if there is a join then it would load the necessary data from those two tables) but when the query finishes executing the memory consumed by SQL Server is not released.

I noticed this because a few days back I was analyzing a query that takes up a lot of tempdb space. When I used to run the query it would (by the end of execution) consume upto 25 GB of RAM. This 25 GB RAM would not be released unless I restarted the MSSQLSERVER service.

How do you guys do SQL Server memory management? This is clearly an issue right?

I would also like to hear if you do something specific to clear the memory used up by a single query.

Thanks in advance!

like image 397
Kumar Vaibhav Avatar asked Jun 25 '13 06:06

Kumar Vaibhav


People also ask

Why SQL Server does not release memory?

SQL Server is indeed designed to request as much RAM as possible which will not be released unless this memory is explicitly required by the operating system. I think the best approach is to limit the amount of RAM the server can use which will allow the OS to have a set amount of resources to use no-matter-what.

How do you release a memory in SQL?

dm_os_process_memory; A solution is to drop max server memory for the SQL Server and increase it again to force SQL Server to release unused but allocated memory.

How do you link two tables in SQL?

SQL JOIN. A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.


1 Answers

SQL Server is indeed designed to request as much RAM as possible which will not be released unless this memory is explicitly required by the operating system. I think the best approach is to limit the amount of RAM the server can use which will allow the OS to have a set amount of resources to use no-matter-what. To set this How to configure memory options using SQL Server Management Studio:

Use the two server memory options, min server memory and max server memory, to reconfigure the amount of memory (in megabytes) managed by the SQL Server Memory Manager for an instance of SQL Server.

  1. In Object Explorer, right-click a server and select Properties.
  2. Click the Memory node.
  3. Under Server Memory Options, enter the amount that you want for Minimum server memory and Maximum server memory.

You can also do it in T-SQL using the following commands (example):

exec sp_configure 'max server memory', 1024 reconfigure 

To restrict the consumption to 1GB.

Note: the above is not going to limit all aspects of SQL Server to that amount of memory. This only controls the buffer pool and the execution plan cache. Things like CLR, Full Text, the actual memory used by the SQL Server .exe files, SQL Agent, extended stored procedures, etc. aren't controlled by this setting. However these other things typically don't need all that much memory, it's the buffer pool and the execution plan cache which need the bulk of the memory.

I hope this helps.

like image 57
MoonKnight Avatar answered Sep 30 '22 13:09

MoonKnight