SQL Server Memory Management

Source: Internet
Author: User
Introduction

Understanding the memory management of SQL Server is the basic for SQL Server troubleshooting and performance tuning.ArticleDescribes how SQL server manages memory.

 

Secondary storage ( Secondary storage )

For computers, the storage system is hierarchical. The closer the CPU, the better the speed, but the smaller the capacity (1 ). For example, the traditional computer storage architecture is close to the CPU in sequence: registers in the CPU, level 1 cache, level 2 cache, memory, and hard disk. However, a storage system farther away from the CPU will be an order of magnitude larger than the previous storage system. For example, a hard disk is generally an order of magnitude larger than its current memory.

Figure 1. Computer Storage System

 

Therefore, for SQL Server, the memory configured by the normal production system usually cannot load all data, so it involves Level 2 storage, that is, disks. As the final mechanical storage component of modern computer systems, the disk needs to move the head to read data (for details about the disk principle, refer to an article I wrote earlier ), in addition, because the data accessed by the database is usually randomly distributed in various locations on the disk, if frequent disk reading requires frequent head movement, this performance will be very low.

It can be seen from the computer body storage architecture that the computer must first read the memory for all operations on data on the hard disk, therefore, reducing access to disk I/O by taking advantage of the memory buffer will be the key to improving SQL server performance. This is also the starting point of this article.

 

SQL Server engine, a self-adjusting Engine

Because SQL Server has always been oriented to the small and medium-sized enterprise market, the SQL server storage engine is designed as a product that can be used without too many configurations, thus reducing deployment costs, however, this is why many people have always criticized Microsoft for making too few configurations open. There is almost no directly configurable space for how SQL server uses memory. The only available configuration is whether awe is used and the maximum or minimum memory occupied by the instance, as shown in figure 2.

Figure 2. SQL server controllable memory options

 

For specific SQL server memory usage, such as how much memory is allocated to the execution plan cache and how much buffer is allocated to the data, these cannot be adjusted through configuration. This is why many developers of other technologies are excited about the advantages of Microsoft's developers. In my opinion, although SQL Server provides few controllable configurations, however, in many cases, you can perform "indirect" configuration with familiarity with the principle. This also requires some understanding of the principles of windows.

 

How does SQL server use memory?

The SQL Server storage engine is a Windows process. Therefore, SQL server uses the same memory as other Windows processes and needs to apply for memory from windows. After applying for memory from windows, SQL server memory can be roughly divided into two parts: Buffer Pool memory (data page and idle page), non-buffer memory (thread, DLL, linked server, etc ). The buffer pool memory occupies most of the memory usage of SQL Server. The memory occupied by the buffer pool is also set in Figure 2. The memory occupied by sqlservr.exe may be larger than the maximum memory set in Figure 2.

Another point is that the memory usage of SQL Server is characterized by the amount of memory used and will not be released after it is used (unless the Windows Memory pressure is reported ). For example, the Development Server in my company shows memory usage when there is almost no load, as shown in 3.

Figure 3.memory usage of the SQL Server process

 

We can see that when the CPU load is 0, the memory occupies 13 Gb. This is because the memory applied to Windows from SQL Server has not been released.

The memory size that SQL server can use is determined by the following factors:

1. Physical memory size

2. Restrictions on the memory of the installed Windows Version (for example, Windows Server 2008 Standard Edition only supports 32 GB of maximum memory)

3. SQL Server is 32-bit or 64-bit

4.2 configure the memory usage of SQL Server

5. SQL Server version (for example, Express version can only use 1 GB of memory)

 

Layer-3 memory allocation for SQL Server OS

The memory allocation of SQL Server OS is divided into three levels, as shown in dependency 4.

Figure 4. SQL Server OS Memory dependency

 

Memory Node

The bottom layer is the memory node, which is used to transfer the allocated memory from windows to the SQL Server OS. Each SQL server instance usually has only one memory node. The size of a memory node depends on the hardware configuration of the NUMA architecture. We can see some memory node information through DBCC memorystatus, as shown in Figure 5.

Figure 5. view memory node Information

 

We can see that the applied memory size can be divided into two parts:

1. Apply for memory units smaller than or equal to 8 KB. These memories are used for caching. (Singlepage Allocator in figure 5)

2. Apply for a memory unit larger than 8 KB. These memories are called multi-page (or memtoleave) (multipage Allocator in Figure 5)

 

The reason why memtoleave is called memtoleave is that although most of the memory of SQL Server is used in the buffer zone, some continuous memory is required for SQL CLR, linked server, backup buffer, and other operations, when you start an instance, the 32-bit SQL Server retains a part of the continuous virtual IP address (VAS) for multipage allocator. The specific reserved quantity can be calculated using the following formula:

Reserved address = (number of CPU Cores-4) + 256) * 0.5 MB + 256 MB, usually around MB.

 

Memory clerk

Let's take a look at memory clerk, which is used to allocate memory and classify the memory allocated by allocate. The following statement can be executed in a simple way, as shown in 6.

Figure 6. Classification by memory clerk type

 

Note: As shown in figure 4, memory clerk is only part of the allocated memory, and the other part is the data cache (buffer pool)

 

Buffer Pool

Before you start to talk about the buffer pool, you should first talk about the virtual memory.

In Windows, each process has a virtual address space VAS (VAS). The 32-bit system is the power of 2 to the power of 32, that is, 4 GB, which is classified as two parts by windows, some are used in windows, and the other is the application.Program. The virtual memory is not the actual physical memory, but the ing of the physical memory. When the physical memory does not point to the content of the virtual memory, page-missing interruptions are generated and some pages are replaced with the memory, then read the required part from the hard disk to the memory. For this part, read an article I wrote earlier: Talking about the memory management of the operating system.

Therefore, when the buffer pool is used to buffer data pages, it will reduce access to disks when reading data in the future.

This buffer pool is the space occupied by setting the maximum and minimum Server Memory in Figure 2. This minimum value does not mean that SQL server can occupy so much memory at startup, but will not be released once the SQL Server Buffer Pool usage exceeds this value.

In DBCC memorystatus, we can see the buffer pool information, as shown in 7.

Figure 7. Buffer Pool Information

 

When an SQL server instance is started, the VAS address space reserved by the buffer pool depends on multiple factors: including the actual physical memory and the SQL Server is 32-bit or 64-bit (this limit is 32-bit and 4 GB, and a half is required for Windows and memtoleave space ), for the physical memory actually used by SQL Server, you can view it in the following statement, as shown in 8.

Figure 8. view the physical memory used by the buffer pool

 

The buffer pool constantly applies for memory as needed. If the buffer pool is required, the buffer pool will continuously consume the memory until windows notifies SQL server that the memory is too low. Otherwise, the buffer pool occupies the memory and will not be released.

In addition, the size of the page allocated by the buffer pool is the same as that of the SQL Server OS page, that is, 8192 bytes, when the other part of SQL Server needs to borrow memory from the "buffer pool", it can only be borrowed in 8 K units, and the physical memory in this part is not continuous, this sounds like the buffer pool memory management system, which can be understood as this, because the buffer pool directly uses the virtual or awe sqlos interface instead of using any SQL Server Page allocator.

Therefore, the memory occupied by SQL Server can be roughly estimated using this formula: Memory occupied by the buffer pool + Page occupied by the buffer pool + non-buffer pool memory allocated by multipageallocator, 9.

Figure 9. approximate estimation of memory occupied by SQL Server

 

 

Memory Object

A menory object is essentially a heap, which is allocated by PAGE allocator. You can use sys. view the dm_ OS _memory_objects DMV. This DMV shows the page_allocator_address column, which is the identifier of memory clerk, indicating the Memory Object allocated by the memory clerk.

 

Memory bottleneck of 32-bit SQL Server

According to some basic principles described earlier in this article, since 32-bit SQL server uses VAS for address allocation, the addressing space is limited to 4 GB, half of the 4 GB memory is allocated to windows, so that the buffer pool can only use 2 GB of memory, which makes 32-bit SQL server unusable even if there is excess physical memory.

One solution is to reduce the default 2g to 1g occupied by windows, so that the memory available for SQL Server can be changed to 3G. You can set the increaseuserva option by typing bcdedit/set in the command line of Windows Server 2008. The value is 3072 mb. For Windows Server 2003. add the/3 GB startup parameter to ini.

Another way is to use awe (Address WindowExtension) memory allocation. Awe uses physical address extension PAE to add four bits, so that the 32-bit CPU address range is increased to the power of 2's 36, that is, 64 GB. The problem of insufficient addressing range is basically solved.

 

Virtualalloc and allocateuserphysicalpages

Virtualalloc and allocateuserphysicalpages are the methods used by SQL Server to apply for memory from windows. By default, all memory required by SQL Server uses virtualalloc to apply for memory in windows. This kind of application is at the operating system level, that is, the corresponding virtual memory. This causes a problem. All memory allocated through virtualalloc can be replaced with virtual memory when Windows is facing memory pressure. This causes Io usage problems.

The memory applied for using allocateuserphysicalpages directly matches the underlying page table. Therefore, the memory applied for using this method will not be replaced by the memory. In the case of 32-bit SQL Server, by enabling awe to allocate memory, the data cache part of the buffer pool will use this function, while memtoleave and buffer pool memory (mainly execution plan cache) still passVirtualalloc for memory allocation.

Therefore, before enabling the memory allocation through awe, SQL server requires the corresponding permissions. Otherwise, an error is reported in the log, as shown in 10.

Figure 10. error message when awe is enabled but corresponding permissions are not enabled

 

You can set this permission for the account that starts SQL server in the Group Policy, as shown in Figure 11.

Figure 11. Lock page in memory)

 

64-bit SQL server problems

64-bit windows basically does not have the above memory problem, but you still need to note that by default, 64-bit SQL Server still uses virtualalloc for memory allocation, this means that all allocated memory will be replaced by pages when Windows is under pressure, which may cause jitter (buffer pool Church ), in this case, the pages in the SQL Server Buffer Pool are constantly exchanged into the hard disk, resulting in a large amount of Io usage (you can use sys. dm_exec_query_memory_grants: DMV to view and wait for memory queries). Therefore, 64-bit SQL server allocates the date page in the buffer pool through allocateuserphysicalpages to avoid this problem. Unlike 32-bit SQL Server, 64-bit SQL server does not need to enable awe. You only need to enable "lock page in memory" as shown in 11.

However, this exposes another problem because SQL Server locks the Memory Page. When Windows Memory is in a hurry, SQL Server cannot respond quickly to Windows Memory (of course, the non-data cache and memtoleave in the buffer pool are still acceptable, but they are often insufficient because the memory consumption is small compared with the data cache ), because SQL server features memory usage, it is very likely that Windows may be unstable or even crashed when it fails to respond to low Windows Memory. Therefore, after "lock page in memory" is enabled, we need to limit the memory usage of the SQL Server Buffer Pool. As mentioned in Figure 2 above, we will not detail it here.

Another problem is that when buffer poolallocates memory through allocateuserphysicalpages, the memory occupied by sqlservr.exe that we see in the task manager only contains the non-data cache part and memtoleave part in the buffer pool, instead of Data cache.exe, it seems that sqlservr.exe can only occupy several hundred megabytes of memory and dozens of GB of memory. In this case, check the SQL server: Memory Manager \ Total Server Memory counter in perfmon.exe to find the memory occupied by SQL Server.

 

Summary

This article describes the basic principles of SQL Server Memory Management and the memory usage of SQL Server. for SQL Server performance optimization, understanding the memory usage is a key part, many Io problems may be caused by memory.

Click here to download the PDF version of this article

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.