SQL Server not releasing memory after query executes

后端 未结 5 1543
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-08 07:14

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

相关标签:
5条回答
  • 2020-12-08 07:29

    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 讨论(0)
  • 2020-12-08 07:33

    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 讨论(0)
  • 2020-12-08 07:37

    SQL is a high level declarative language. It is not intended to allow you to get involved in the detail of such things as memory management as you would if you were writing code in a lower level Programming language such as C. I think you will find that if it needs that memory freed up for another task it will quickly let it go.

    0 讨论(0)
  • 2020-12-08 07:46

    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.

    0 讨论(0)
  • 2020-12-08 07:48

    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
    
    0 讨论(0)
提交回复
热议问题