SQL Server is not releasing memory after Daily Load - sql-server

We have a SQL Server 2012 Enterprise Edition, 128GB of RAM, Windows 2008R2. The SQL Server job runs every day at 3 AM and takes 5 hrs to load data into the database. During this process, SQL Server utilizes 123GB (max memory allocated).
After the job completes, SQL Server is not releasing the RAM.
Queried memory utilization where buffer pool shows 97GB. Users don't access database during this time. I restarted SQL Server services to bring RAM down. I didn't find a correct answer related to this issue. Why is it not releasing the RAM? How can we bring RAM utilization down?
SQL Server Job -> SSIS package -> Import data from Mysql to SQL Server database
Thanks

This is by design once SQL Server uses memory, it keeps hold of it and does not release it back to OS.
Your Task Manager may show all/nearly all memory used by SQL Server but if you want to see how much memory SQL Server is actually using you can use the following query.
SELECT (physical_memory_in_use_kb/1024) AS Memory_usedby_Sqlserver_MB
FROM sys.dm_os_process_memory;

By design, SQL Server holds on to the RAM that is has allocated. Much of the RAM is used for the buffer pool. The buffer pool is a cache that holds database pages in memory for fast retrieval.
If SQL Server were to release some memory, and someone were to run a query that requests it right afterwards, the query would have to wait for expensive physical I/O to produce the data. Therefore, SQL Server tries to hold as much memory as possible (and as configured) for as long as possible.
The RAM settings here specify the min server memory and the max server memory. Careful setting of the max memory setting allows room for other processes to run. The article quotes a complicated formula for determining how much room to leave:
From the total OS memory, reserve 1GB-4GB to the OS itself.
Then subtract the equivalent of potential SQL Server memory allocations
outside the max server memory control, which is comprised of stack
size 1 * calculated max worker threads 2 + -g startup parameter 3 (or
256MB by default if -g is not set). What remains should be the
max_server_memory setting for a single instance setup.
In our servers, we usually just wing it and set the max memory option to several GB below the total physical memory. This leaves plenty of room for the OS and other applications.
If SQL Server memory is over the min server memory, and the OS is under memory pressure, SQL Server can release memory until it is at the min server memory setting.
Reference: Memory Management Architecture Guide.
One of the primary design goals of all database software is to
minimize disk I/O because disk reads and writes are among the most
resource-intensive operations. SQL Server builds a buffer pool in
memory to hold pages read from the database. Much of the code in SQL
Server is dedicated to minimizing the number of physical reads and
writes between the disk and the buffer pool. SQL Server tries to reach
a balance between two goals:
Keep the buffer pool from becoming so big that the entire system is low on memory.
Minimize physical I/O to the database files by maximizing the size of the buffer pool.
When SQL Server is using memory dynamically, it queries the system
periodically to determine the amount of free memory. Maintaining this
free memory prevents the operating system (OS) from paging. If less
memory is free, SQL Server releases memory to the OS. If more memory
is free, SQL Server may allocate more memory. SQL Server adds memory
only when its workload requires more memory; a server at rest does not
increase the size of its virtual address space.
...
As more users connect and run queries, SQL Server acquires the
additional physical memory on demand. A SQL Server instance continues
to acquire physical memory until it either reaches its max server
memory allocation target or Windows indicates there is no longer an
excess of free memory; it frees memory when it has more than the min
server memory setting, and Windows indicates that there is a shortage
of free memory.
As other applications are started on a computer running an instance of
SQL Server, they consume memory and the amount of free physical memory
drops below the SQL Server target. The instance of SQL Server adjusts
its memory consumption. If another application is stopped and more
memory becomes available, the instance of SQL Server increases the
size of its memory allocation. SQL Server can free and acquire several
megabytes of memory each second, allowing it to quickly adjust to
memory allocation changes.
If, for some reason:
You absolutely MUST have that memory back
You know you do not need it for a while
You are willing to pay a penalty for virtual memory allocation and physical I/O to retrieve data from disk the next time you need that memory
Then you can temporarily reconfigure the database max server memory setting to a lower value. This can be done through the SSMS user interface, or you can use an sp_configure 'max server memory' followed by reconfigure to make the changes programatically.

Full disclosure: I did not try it myself.
You should not try it on your production environment before testing it somewhere else.
This is from a DBA answer:
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'max server memory', 4096;
GO
RECONFIGURE;
GO
4096 should be replaced by the value that you find acceptable as the minimum.
Should be followed by a similar command to increase the memory back to your original maximum.

Related

SSIS Server Maintenance Job ends up using all of server memory

For some reason the SSIS Server Maintenance Job ends up having the SQL Server instance use all available server memory after a few runs (it runs every midnight). When that happens, my SSIS packages no longer have memory to run in and start swapping on disk which leads to unacceptable execution times or at worst a total hang.
So far I've been resetting the SQL Server service through Configuration Manager every morning, but that's not a viable long term solution. I have not set maximum memory limit for the SQL Server instance. Would that help? If not, what can I do?
Server information: Azure VM, 32 GB ram, no other purpose for the server than running SSIS.
You should always set a maximum memory limit for SQL Server instances.
A simple rule of thumb is to leave 4GB or 10% of total memory free, whichever is larger, and tweak as necessary.
If your SQL Server instance is running as a VM, then you also need to set a memory reservation at the host for your VM. Otherwise, the host's 'balloon memory manager' might kick in and steal memory back from your instance.
Ref:
Server memory configuration options
Understanding Memory Resource Management in VMware

Does SQL Server automatically allocate memory to all instances?

I have a single instance of SQL Server on my server machine, with 80% of the memory reserved to it. This is configured in the SQL Server max memory settings.
If I create an additional instance, will it use the reserved memory, or will it attempt to use the unallocated 10%?
Simply put, is the reserved memory used by SQL Server instance specific?
Max and Min Server Memory are NOT reservations. It does not work the same way as e.g. VmWare with reservations.
On startup, the instance only uses a minimum amount of memory - it does not grab the Min Server Memory amount immediately.
In normal use, your instance will continue to use more memory as required until it reaches the Max Server Memory limit - but only if there is no external memory pressure signalled by Windows. If the system requests more memory, SQL Server will release this but will not release if it would drop below Min Server Memory.
Therefore if you are going to install more than 1 instance - you need to be careful in regards to the Min Server Memory and not set this so high that the sum of all Min Server Memory amounts plus the amount needed by the OS and other programs is more than the total physical memory available. Also think about what is realistic for the Max Server memory - as one instance increases memory uses it may request other instances to release some memory back - possibly impacting performance of other instances.
You'll need to monitor what your instance actually needs - counters such as Page Life Expectancy can help give you an idea of how long pages stay in memory after loaded (i.e. is there memory pressure) which will help you identify the impact of lowering Max Server Memory for that instance.
The performance characteristics of each database will affect what you configure.
In saying that, in these days of virtualisation - I would tend to avoid multiple instances per server and simply spin up another VM for my new instance. That way I can have more control over the actual physical reservations for memory - but I still need to be aware of operations like ballooning if I over-allocate.

A significant part of SQL Server process memory has been paged out

I have 512 GB of memory on my physical box, out of which 85% is dedicated to SQL Server. I'm starting to get this message in error log. When this happens, SQL Server closes connection to other processes or users. Any guidance on what should I do here? Nothing runs on the server at this time when this happens. Any guidance would be much appreciated.
A significant part of SQL Server process memory has been paged out. This may result in a performance degradation.
Duration: 602 seconds. Working set (KB): 3860628, committed (KB): 342039316, memory utilization: 1%.
You can prevent Windows from paging by granting the Lock Pages in Memory OS privilege to the SQL Server Service Account, or Per-Service SID.
See:
https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/enable-the-lock-pages-in-memory-option-windows
This will cause SQL Server to bypass the Windows Virtual Memory manager, and directly allocate physical memory.
Beware that when you do this SQL Server will only respond to OS memory pressure slowly, and so other processes needing memory may not be able to run. SO it's important to set Max Server Memory appropriately.
David

How to reduce Physical Memory usage in SQL Server

I have 32 GB physical memory server. When I am starting my server its taking 18gb memory when the server and SQL Server 2008 R2 will up. But after few hours SQL Server will be taking up 23gb or more cached size going 4939 or more. What is the cause of this problem and how can I see which queries making this problem?`
SQL loves memory, it'll use what it needs to, especially when caching data. The very nature of caching data is using memory. If your concerned about leaving a little memory for the server or other processes running on your server, then set a max memory amount to SQL.

SQL SERVER 2008 - Memory Leak while storing Millions of records

I need some help very badly.
I'm working on a project where a bulk of data is entered all the time. It's a reporting software.
10 Million records in an average is stored per day and it could keep on increasing as users increase.
As of now, SQL SERVER CONSUMES 5gb of RAM on the task manager. I have an 8GB ram on my server now.
How do other enterprises manage such situations?
SQL Server uses memory efficiently and takes as much as it can. It's also usually clever enough to release memory when needed.
Using 5GB means:
SQL Server is configured to 5GB or SQL Server has simply reserved this memory during normal usage
It's left 3GB because it doesn't need to use it
Nothing is wrong... and I'd probably configure the SQL Server max mem to 6.5GB...
Late addition: Jonathan Kehayias blog entry
SQL Server typically uses as much memory as it can get it's hands on, as it then stores the more frequently accessed data in memory to be more efficient, as disk access is slower then memory access.
So nothing is wrong with it using 5gb of memory.
To be honest, it's leaving 3gb of memory for other applications and the operating system, so there might not be anything wrong with this. (If this is all that server is designed to do.).
To configure the memory limit, do the following:
In SQL Server Enterprise manager, right click on the server name, and go to properties.
Click on the Memory option
Reduce the maximum server memory to what you think is appropriate.
Click ok.
I highly doubt that this is in fact a memory leak. The increase of SQL Server's memory usage is by design, simply because it caches a lot of stuff (queries, procedures).
What you will most likely see is that if the available memory that is still left runs low, SQL server will 'flush' its memory, and you would see in fact that memory will be freed in the end.

Resources