DBA Scripts: Get Memory Configuration and Usage

🔧Part of the DBA-Tools Project, copy/paste SQL Server scripts and health checks.In: Server & Configuration

Max Server Memory is the single most important memory setting on any SQL Server instance, and it’s astonishing how often it’s left unconfigured. Without a cap, SQL Server will happily consume nearly all available RAM for its buffer pool, leaving too little for the OS, and on a box running other services, that ends in memory pressure and paging for everyone.

This script reports the configured min and max server memory limits alongside actual physical memory and current SQL Server memory consumption, so you can see in one query whether the configuration matches the hardware.


Why Memory Configuration and Usage Matters

SQL Server’s buffer pool is what keeps frequently-read data pages in memory instead of going back to disk for every query. How much memory it’s allowed to use directly affects:

  • Buffer cache hit ratio, and therefore how much of your workload is served from memory versus disk
  • Whether the OS and any other services on the box (monitoring agents, backup software, a second instance) have enough memory to run without contention
  • Plan cache size, which shares the same memory budget as the buffer pool

An unconfigured Max Server Memory (left at the default, effectively unlimited) is one of the most common findings on servers nobody has tuned since installation.


When to Run This Script

  • Routine SQL Server health checks
  • After a hardware change, VM resize, or memory reallocation
  • Investigating OS-level memory pressure or paging on a shared server
  • Reviewing a server you’ve just inherited or migrated

The Script

Run the following script against your SQL Server instance.

✓ Verified
  • Tested on: SQL Server 2025 (RTM CU8), Windows lab instance
  • Last verified: 2026-09-01 (re-run against the lab instance, build 17.0.4075.5)
  • Permissions: VIEW SERVER STATE
  • Safety: read-only, impact low
/*
Script Name : Get-MemoryConfigurationAndUsage
Category    : configuration-and-environment
Purpose     : Show configured memory limits alongside current SQL Server memory consumption.
Author      : Peter Whyte (https://sqldba.blog/dba-scripts-get-memory-configuration-and-usage/)
Requires    : VIEW SERVER STATE
HealthCheck : Yes
*/
-- SAFE:ReadOnly
-- IMPACT:Low
SET NOCOUNT ON;

SELECT
    (SELECT value_in_use FROM sys.configurations WHERE name = 'min server memory (MB)') AS min_server_memory_mb,
    (SELECT value_in_use FROM sys.configurations WHERE name = 'max server memory (MB)') AS max_server_memory_mb,
    CAST(osi.physical_memory_kb / 1024.0 / 1024 AS DECIMAL(10,2))                      AS server_physical_memory_gb,
    pm.physical_memory_in_use_kb / 1024                                                 AS sql_memory_in_use_mb,
    pm.large_page_allocations_kb / 1024                                                 AS large_page_allocations_mb,
    pm.locked_page_allocations_kb / 1024                                                AS locked_page_allocations_mb,
    pm.total_virtual_address_space_kb / 1024                                            AS total_virtual_address_mb,
    CAST(osi.committed_kb / 1024.0 AS DECIMAL(12,2))                                   AS sql_committed_mb,
    osi.sqlserver_start_time                                                             AS sql_start_time
FROM sys.dm_os_process_memory AS pm
CROSS JOIN sys.dm_os_sys_info AS osi;

The script combines configured memory limits from sys.configurations with actual memory usage from sys.dm_os_process_memory and hardware totals from sys.dm_os_sys_info, returning one row for the instance.


How To Run From The Repo

Clone DBA Tools, initialize and run the script:

# Clone dba-tools repo:
git clone https://github.com/peterwhyte-lgtm/dba-tools

# Initialize environment:
cd dba-tools
.\Initialize-Environment.ps1

# Check configured memory limits against actual usage:
.\run.ps1 Get-MemoryConfigurationAndUsage

# To run against a remote sql server:
.\run.ps1 Get-MemoryConfigurationAndUsage -ServerInstance SQLSERVER01

This script lives in the repo at:


Example Output

SQL Server max server memory set to 2048 MB against 7.78 GB of physical RAM, with 65 MB in use, returned by the Get-MemoryConfigurationAndUsage script in SSMS

Run against the lab instance. It reports 7.78GB of physical memory, Max Server Memory set to 2,048MB, and 65MB in the SQL Server process working set at the moment of the reading. min_server_memory_mb comes back as 16 because the script reads value_in_use; the configured value on the same instance is 0. Worth knowing before anyone records 16 as a setting somebody chose.


Understanding the Results

max_server_memory_mb
The ceiling on the buffer pool, read from value_in_use. Microsoft documents the default as 2,147,483,647 MB, which is no practical cap at all, and recommends setting it explicitly (Server memory configuration options).Act when this still reads 2147483647. An uncapped instance will claim memory the operating system needs and everything else on the box pays for it.
min_server_memory_mb
The floor SQL Server will not release memory below once it has climbed there. The script reads value_in_use, which reported 16 on the lab instance while the configured value read 0. Read the two together before calling a number a change.
server_physical_memory_gb
Total RAM the operating system can see, from sys.dm_os_sys_info. It is here so you can compare it against the cap in one glance rather than two queries.Act when the cap is close to the whole box. Leave room for the operating system, for anything else installed, and for the parts of SQL Server that sit outside the cap.
sql_memory_in_use_mb
The working set of the SQL Server process right now, from sys.dm_os_process_memory. This is what the process actually holds, not what it is allowed to hold.Act when this sits at the cap on a busy instance, or far below it on one you believe is under pressure. Either way the cap and the workload disagree.
large_page_allocations_mb
Memory allocated as large pages. Zero on most instances; it is only non zero where large page support has been turned on deliberately.
locked_page_allocations_mb
Memory held under the Lock Pages in Memory privilege. Zero means the privilege is not in use by the service account, which is the normal reading on a default install.
total_virtual_address_mb
The size of the process virtual address space. On 64 bit it is enormous by design and is not a ceiling worth acting on. It is included so a reader does not mistake it for one elsewhere.
sql_committed_mb
Memory committed by the SQL Server memory manager, from sys.dm_os_sys_info. It is a different measure from the working set above and the two do not have to agree.
sql_start_time
When the engine last started. Every other value on this row describes the instance as it is now, but this is the context for whether it has had time to warm up.

How to Fix Memory Configuration

-- Set Max Server Memory (example: 6GB on an 8GB box, leaving 2GB for the OS)
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'max server memory (MB)', 6144;
RECONFIGURE;

Takes effect immediately for new memory grants, no restart required, though SQL Server won’t release already-allocated memory instantly if you’re lowering the cap.

A starting point for standalone instances: leave 4GB or 10% of total RAM (whichever is larger) for the OS, and cap SQL Server at the rest. On a server also running SSAS, SSRS, a monitoring agent, or a second SQL instance, leave more headroom.


Best Practices

  • Always set Max Server Memory explicitly. Never leave it at the default on a production instance.
  • Re-check after any hardware change; a memory cap sized for the old server is rarely correct on the new one.
  • On a shared box, size the cap around every service that needs memory, not just SQL Server.

Microsoft’s reference covers sys.configurations, sys.dm_os_process_memory and sys.dm_os_sys_info in full.


Related Scripts

You may also find these scripts useful:


Frequently Asked Questions

What happens if Max Server Memory is left at the default?

SQL Server can consume nearly all available RAM, leaving little for the OS and any other processes on the box. On a dedicated SQL Server, this is sometimes tolerated; on a shared server, it’s a common cause of memory pressure elsewhere.

Does lowering Max Server Memory immediately free up RAM?

Not necessarily right away. SQL Server releases memory back gradually, not instantly, when the cap is lowered.


Summary

Memory configuration is one of the first things worth checking on any SQL Server instance, and one of the cheapest to fix once you know what’s wrong. An unconfigured Max Server Memory is a silent risk until the day something else on the box needs RAM SQL Server has already claimed.

Run this script as part of routine health checks, and always after any hardware or memory change, to confirm the configuration still matches reality.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *