33

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!

15
  • 2
    What version are you using? This is a known problem up to SQL Server 2008 R2. I am not sure about 2012. I will provide an answer shortly...
    – MoonKnight
    Jun 25, 2013 at 7:00
  • 6
    "This is clearly an issue right?" - no. And it's not a "problem" as Killercam phrases it. SQL Server is designed to grab memory and keep hold of it, unless the OS signals that its running short. Which is better in the long run - SQL Server processes each query, incurring any/all I/O costs of loading data into memory, but then neatly throws all of that data out again, or it keeps it hanging around in case future queries will benefit from the data already having been loaded? Jun 25, 2013 at 7:05
  • 3
    Agreed. But in many scenarios I know for sure that the query won't be used again. I am looking to release memory in such cases. Thanks for your response. Jun 25, 2013 at 7:07
  • 4
    The point is, this is what the server is designed to do. It grabs memory from the OS when it needs more, but then it starts managing that memory, deciding what to put in there, etc. And unless the OS signals low memory, it will not give it back. Jun 25, 2013 at 7:12
  • 2
    Definitely agree. But what do I do if I do want to release memory programatically? Jun 25, 2013 at 7:14

4 Answers 4

30

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.

4
  • So there is no option to release memory used by a query right? Jun 25, 2013 at 8:27
  • 1
    No. You would have to restart the service to force the release. The only way to restrict the consumption is as above. I have not started work with 2012 and this is something that may have been addressed (although I doubt it, as SQL Server is mainly used on a server based transact basis, and as the discussion above rightly established - here this behaviour is not an issue). If you are running the SQL service from an application, perhaps you could force the release of memory by using OS resources temporarily - however, I would not recommend this and have not attempted it...
    – MoonKnight
    Jun 25, 2013 at 8:37
  • 2
    I am facing a similar issue.. however once the SQL server memory usage reaches to its peak.. the newer queries got timeout error and there is no way except restarting mssql server. What could be wrong here? And how to fix it?
    – Krunal
    Feb 22, 2014 at 5:14
  • @Krunal - Were you able to figure it out? Jan 4, 2017 at 17:53
6

I too faced same issue mentioned above. But running below query in releasing the RAM memory but in less than 5 hour the RAM memory is getting occupied. So again i have to forcefully free the RAM memory.

EXEC sys.sp_configure N’show advanced options’, N’1' RECONFIGURE WITH OVERRIDE
GO
EXEC sys.sp_configure N’max server memory (MB)’, N’2048'
GO
RECONFIGURE WITH OVERRIDE
GO
EXEC sys.sp_configure N’show advanced options’, N’0' RECONFIGURE WITH OVERRIDE
GO

Then run following:

2.

EXEC sys.sp_configure N’show advanced options’, N’1' RECONFIGURE WITH OVERRIDE
GO
EXEC sys.sp_configure N’max server memory (MB)’, N’6144'
GO
RECONFIGURE WITH OVERRIDE
GO
EXEC sys.sp_configure N’show advanced options’, N’0' RECONFIGURE WITH OVERRIDE
GO
0
4

I don't think there is a way to force SQL Server to free memory . However you can limit memory usage.

sp_configure 'max server memory', <memory_size MB>
reconfigure

MSDN

0
1

Old question, but figured i'd add my two cents. Mostly a riff on what was in the above answer using dsql to automatically roll back to the previous value after shrinking the memory amount. It's ugly, but it works.

IF OBJECT_ID(N'tempdb..##globaltemp') IS NOT NULL
BEGIN
    DROP TABLE ##globaltemp
END

SELECT TOP 1 value
INTO ##globaltemp
FROM sys.configurations
WHERE NAME LIKE '%server memory%'
ORDER BY NAME
OPTION (RECOMPILE);

EXEC sys.sp_configure N'max server memory (MB)'
    , N'1024'
GO

RECONFIGURE
WITH OVERRIDE
GO

DECLARE @dsql AS VARCHAR(20)

SELECT @dsql = cast(value AS NVARCHAR(20))
FROM ##globaltemp

EXEC sys.sp_configure N'max server memory (MB)'
    , @dsql
GO

RECONFIGURE
WITH OVERRIDE
GO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.