<p class="intro">由于Sql Server对于系统内存的管理策略是有多少占多少,除非系统内存不够用了(大约到剩余内存为4M左右), Sql Server才会释放一点点内存。所以很多时候,我们会发现运行Sql Server的系统内存往往居高不下。 护卫 神 教程整理 这些内存一般都是Sql Server运</p> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">由于Sql Server对于系统内存的管理策略是有多少占多少,除非系统内存不够用了(大约到剩余内存为4M左右), <br style="word-wrap: break-word; "> Sql Server才会释放一点点内存。所以很多时候,我们会发现运行Sql Server的系统内存往往居高不下。<font color="#ffffff" style="word-wrap: break-word; ">护卫<u style="word-wrap: break-word; ">神</u>教程整理</font> <br style="word-wrap: break-word; "> 这些内存一般都是Sql Server运行时候用作缓存的,例如你运行一个select语句, <br style="word-wrap: break-word; "> 那么Sql Server会将相关的数据页(Sql Server操作的数据都是以页为单位的)加载到内存中来, <br style="word-wrap: break-word; "> 下一次如果再次请求此页的数据的时候,就无需读取磁盘了,大大提高了速度。这类的缓存叫做数据缓存。 <br style="word-wrap: break-word; "> 还有一些其他类型的缓存,如执行存储过程时,Sql Server需要先编译再运行,编译后的结果也会缓存起来, <br style="word-wrap: break-word; "> 下一次就无需再次编译了。</font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <span style="font-family: Verdana; font-size: 14px; line-height: 21px; ">这里有网友专门写了一个工具,用以实现这Sql Server内存占用过多时候释放内存,截图如下:</span><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <img alt="" style="max-width:90%" src="http://www.68idc.cn/help/uploads/allimg/120918/14025B011-0.gif" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; cursor: pointer; " style="max-width:90%"><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <strong style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">SQLserver 内存释放工具下载地址</strong><span style="font-family: Verdana; font-size: 14px; line-height: 21px; ">:</span>猛击我下载!<br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <span style="font-family: Verdana; font-size: 14px; line-height: 21px; ">以下是软件实现的大概思路:</span><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">如果这些缓存已经不需要了,那么我们可以调用以下几个DBCC管理命令来清理这些缓存: </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">DBCC FREEPROCCACHE <br style="word-wrap: break-word; "> DBCC FREESESSIONCACHE <br style="word-wrap: break-word; "> DBCC FREESYSTEMCACHE('All') <br style="word-wrap: break-word; "> DBCC DROPCLEANBUFFERS </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">这几个命令分别用来清除存储过程相关的缓存、会话缓存、系统缓存以及所有所有缓存 </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">但是需要注意的是,这几个命令虽然会清除掉现有缓存,为新的缓存腾地方, <br style="word-wrap: break-word; "> 但是Sql server并不会因此释放掉已经占用的内存。无奈的是,Sql Server <br style="word-wrap: break-word; "> 并没有提供任何命令允许我们释放不用到的内存。因此我们只能通过动态调整 <br style="word-wrap: break-word; "> Sql Server可用的物理内存设置来强迫它释放内存。 </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">我们也可以通过Sql Server Management企业管理器进行动态控制。 <br style="word-wrap: break-word; "> 连接到企业管理器之后打开Sql Server实例的属性面板, <br style="word-wrap: break-word; "> 找到内存设置,改变其中的最大服务器内存使用即可 </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">--内存使用情况 <br style="word-wrap: break-word; "> SELECT * FROM sys.dm_os_performance_counters <br style="word-wrap: break-word; "> WHERE counter_name IN ('Target Server Memory (KB)','Total Server Memory (KB)') </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">-- 内存状态 <br style="word-wrap: break-word; "> DBCC MemoryStatus </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">--查看最小最大内存 <br style="word-wrap: break-word; "> SELECT <br style="word-wrap: break-word; "> cfg.name AS [Name], <br style="word-wrap: break-word; "> cfg.configuration_id AS [Number], <br style="word-wrap: break-word; "> cfg.minimum AS [Minimum], <br style="word-wrap: break-word; "> cfg.maximum AS [Maximum], <br style="word-wrap: break-word; "> cfg.is_dynamic AS [Dynamic], <br style="word-wrap: break-word; "> cfg.is_advanced AS [Advanced], <br style="word-wrap: break-word; "> cfg.value AS [ConfigValue], <br style="word-wrap: break-word; "> cfg.value_in_use AS [RunValue], <br style="word-wrap: break-word; "> cfg.description AS [Description] <br style="word-wrap: break-word; "> FROM <br style="word-wrap: break-word; "> sys.configurations AS cfg </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">--设置最小最大内存 </font><font color="#ffffff" style="word-wrap: break-word; ">护卫<u style="word-wrap: break-word; ">神</u>教程整理</font><span style="font-family: Verdana; font-size: 14px; line-height: 21px; "> </span><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">sp_configure 'show advanced options', 1 </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">go <br style="word-wrap: break-word; "> sp_configure 'min server memory', 0 <br style="word-wrap: break-word; "> RECONFIGURE <br style="word-wrap: break-word; "> GO </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">sp_configure 'max server memory', 2147483647 <br style="word-wrap: break-word; "> RECONFIGURE <br style="word-wrap: break-word; "> GO </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">sp_configure 'max server memory', 256 <br style="word-wrap: break-word; "> RECONFIGURE <br style="word-wrap: break-word; "> GO <br style="word-wrap: break-word; "> sp_configure 'show advanced options', 0 </font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">-----------------------------------------------------------------------------------------------<br style="word-wrap: break-word; "> CREATE proc [dbo].reclaimmemory --强制释放内存</font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">as<br style="word-wrap: break-word; "> begin</font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">DBCC FREEPROCCACHE <br style="word-wrap: break-word; "> DBCC FREESESSIONCACHE <br style="word-wrap: break-word; "> DBCC FREESYSTEMCACHE('All') <br style="word-wrap: break-word; "> DBCC DROPCLEANBUFFERS</font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">exec sp_configure 'max server memory', 256 <br style="word-wrap: break-word; "> EXEC ('RECONFIGURE' )<br style="word-wrap: break-word; "> WAITFOR DELAY '00:00:05'<br style="word-wrap: break-word; "> EXEC sp_configure 'max server memory', 2147483647 <br style="word-wrap: break-word; "> EXEC ('RECONFIGURE' )<br style="word-wrap: break-word; "> GO</font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">end<br style="word-wrap: break-word; "> --使用示例<br style="word-wrap: break-word; "> /*<br style="word-wrap: break-word; "> reclaimmemory</font><br style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; "> <font face="Verdana" style="word-wrap: break-word; font-family: Verdana; font-size: 14px; line-height: 21px; ">*/</font>