Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to free up memory used by idle SQL Server databases?

Tags:

sql-server

We have a SQL Server with a few hundred databases. Many of the databases on that server are used only a few times per week. We have a problem where SQL Server sometimes becomes very slow. When that happens, memory usage indicates 99% and paging happens. In order to work around the problem we scheduled a restart of the SQL service every night to unload all those databases and free up memory.

What is the correct way of freeing up memory used by idle databases without taking the SQL Service down? We would like to automate the unloading of any database that was not accessed in the last 30 minutes.

Note: I'm looking for a solution that applies to SQL 2005. However, if there's a feature in SQL 2008 to do that I'd like to know about it.

like image 625
Sylvain Avatar asked Apr 26 '11 18:04

Sylvain


People also ask

How do I free up SQL 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.

Why is SQL Server taking up so much memory?

SQL Server is designed to use all the memory on the server by default. The reason for this is that SQL Server cache the data in the database in RAM so that it can access the data faster than it could if it needed to read the data from the disk every time a user needed it.

Is it normal for SQL Server to use all memory?

SQL Server is using all of the memory. No matter how much memory you put in a system, SQL Server will use all it can get until it's caching entire databases in memory and then some. This isn't an accident, and there's a good reason for it.

How much memory should I leave for SQL Server OS?

Reserve 4GB from the first 16GB of RAM and then 1GB from each additional 8GB of RAM for the operating system and other applications. Configure the remaining memory as the maximum server memory allocated for the Microsoft SQL Server buffer pool.


1 Answers

SQL Server will free automatically all memory that can be freed and will avoid paging. If you encounter paging then the 99% memory is in use, is not available to be freed. You need to investigate how is the memory used, it is likely external components like sp_oa_xxx created object or distributed queries. Start by investigating the memory consumers, look at sys.dm_os_memory_clerks and read on How to use the DBCC MEMORYSTATUS command to monitor memory usage on SQL Server 2005.

As a side note, you already have the means to automatically close databases that are not is use: alter database <dbname> set auto_close on:

AUTO_CLOSE: When set to ON, the database is shut down cleanly and its resources are freed after the last user exits. The database automatically reopens when a user tries to use the database again.

If you host hundreds of databases that are used seldom then AUTO_CLOSE is exactly what you're looking for.

like image 134
Remus Rusanu Avatar answered Nov 11 '22 17:11

Remus Rusanu