The default memory allocation of SQL Server 2008 or R2 is 2147483647MB, which is almost infinite. The management strategy for system memory is as much as possible. SQLserver caches all processed SQL operations in memory so that it doesn't have to read from disk all the time. However, if you run SQL Server for a long time, the system memory is almost used, and you may report insufficient memory if you start other programs. At this time, the memory cache needs to be released. Generally I use the following two methods:
mssqlserver 2005 is also the largest 2147483647MB
- Very simple, open SQL Server Configuration Manager, and then restart SQL Server (MSSQLSERVER). Generally, the default instance is MSSQLServer. Of course, if you install other instances (instances), select the corresponding ones, such as MSSQLServer(SQLServLatin1), MSSQLServer( ARABIC).
This method is the simplest and most effective, but it can only temporarily clear the memory space occupied by the SQL Server cache. After a long time, SQL Server will also fill up the memory. And it is very important that this method cannot be used when the SQL server is connected, which will make the user who is using the SQL server temporarily unable to connect to the SQL server, and even cause program errors. And you as an administrator...
net stop mssqlserver net start mssqlserver
- The second method is more complicated, and I am not an expert in SQL Server, but I just learned some queries from the Internet:
DBCC FREEPROCCACHE
DBCC FREESESSIONCACHE
DBCC FREESYSTEMCACHE('All')
DBCC DROPCLEANBUFFERS These commands are used to clear stored procedure-related caches, session caches, system caches and all caches, but it should be noted that although these commands will clear The existing cache makes room for the new cache, but Sql server will not release the memory already occupied. Unfortunately, Sql Server does not provide any commands that allow us to release unused memory. So we can only force it to free memory by dynamically adjusting the physical memory settings available to Sql Server. We can also dynamically control through Sql Server Management Enterprise Manager. After connecting to the Enterprise Manager, open the properties panel of the Sql Server instance, find the memory settings, and change the maximum server memory usage - memory usage SELECT * FROM sys.dm_os_performance_counters WHERE counter_name IN ('Target Server Memory (KB)' ,'Total Server Memory (KB)') -- memory status DBCC MemoryStatus
--View the minimum and maximum memory
SELECT cfg.name AS [Name], cfg.configuration_id AS [ Number ], cfg.minimum AS [Minimum], cfg.maximum AS [Maximum], cfg.is_dynamic AS [Dynamic], cfg.is_advanced AS [Advanced], cfg.value AS [ConfigValue], cfg.value_in_use AS [RunValue], cfg.description AS [Description] FROM sys.configurations AS cfg --set min max memory sp_configure 'show advanced options', 1
go
sp_configure 'min server memory', 0RECONFIGURE GO sp_configure 'max server memory', 2147483647RECONFIGURE GO sp_configure 'max server memory', 256RECONFIGURE GO sp_configure 'show advanced options', 0
CREATE proc [dbo].reclaimmemory --Forcibly release memory as
begin
DBCC FREEPROCCACHE DBCC FREESESSIONCACHE DBCC FREESYSTEMCACHE('All') DBCC DROPCLEANBUFFERS
exec sp_configure 'max server memory', 256EXEC ('RECONFIGURE' )
WAITFOR DELAY '00:00:05'
EXEC sp_configure 'max server memory', 2147483647EXEC ('RECONFIGURE' )GO
end
--use example
/*
reclaimmemory
*/
The above paragraph can generally release the cache, (note that sometimes quotation marks cannot be typed in English quotation marks in word documents, so it is best to copy them into Notepad and edit them), but sometimes they are not very useful. Because SQLserver will not release memory because the Cache (cache) is released, occupying the pit is not necessarily XX. This command will only prevent SQL Server from continuing to occupy new memory, and it is okay to execute it regularly. The key is to also free up memory.
Through the following Query, you can see the memory occupied by the current server
SELECT * FROM sys.dm_os_performance_counters
WHERE counter_name IN ('Target Server Memory (KB)','Total Server Memory (KB)')
Target Server Memory (KB) and Total Server Memory (KB) literally mean the memory size occupied by the target and the current SQL Server.
EXEC sp_configure 'show advanced options', 1
GO
EXEC sp_configure 'max server memory', 256
EXEC ('RECONFIGURE' )
WAITFOR DELAY '00:00:05'
EXEC sp_configure 'max server memory', 2147483647
EXEC ('RECONFIGURE' )
GO
EXEC sp_configure 'show advanced options', 0
GO
In fact, my use of these sentences is not very effective. After a long time, there may be insufficient memory.
******
In general my management approach is:
- Set the maximum memory usage immediately after installing SQL Server
EXEC sp_configure 'show advanced options', 1 -- this sentence is to open advanced options
GO
EXEC sp_configure 'max server memory', 9216 -- set the maximum memory to 9G, our server memory is 16G, leaving 7G is enough
EXEC ('RECONFIGURE' )
GO
EXEC sp_configure 'show advanced options', 0 -- remember to turn off advanced options when you're done
GO
- After a while, if it doesn't work, do it
DBCC FREEPROCCACHE
DBCC FREESESSIONCACHE
DBCC FREESYSTEMCACHE('All')
DBCC DROPCLEANBUFFERS
Clearing the cache is also a headache. I don't know when it's appropriate. Let's just leave it alone. I'm not an expert. If there is a big problem, the machine will not work. Or write a Procedure and execute it regularly with a job.
No way, SQLServer is too domineering, the above method is not a foolproof solution, it is recommended to put SQLServer aside and use it alone.
Finally click the "OK" button and you're done!