SQL Server Memory Internals 김 종구 고객기술지원부 한국마이크로소프트.

Slides:



Advertisements
Similar presentations
Memory.
Advertisements

Part IV: Memory Management
The Linux Kernel: Memory Management
File Systems.
© Neeraj Suri EU-NSF ICT March 2006 Budapesti Műszaki és Gazdaságtudományi Egyetem Méréstechnika és Információs Rendszerek Tanszék Zoltán Micskei
Chapter 14 Chapter 14: Server Monitoring and Optimization.
Memory Management.
CS 333 Introduction to Operating Systems Class 18 - File System Performance Jonathan Walpole Computer Science Portland State University.
Informationsteknologi Friday, November 16, 2007Computer Architecture I - Class 121 Today’s class Operating System Machine Level.
Silberschatz, Galvin and Gagne  Operating System Concepts Segmentation Memory-management scheme that supports user view of memory. A program.
Main Memory. Background Program must be brought (from disk) into memory and placed within a process for it to be run Main memory and registers are only.
Chapter 8: Main Memory.
Virtual Memory By: Dinouje Fahih. Definition of Virtual Memory Virtual memory is a concept that, allows a computer and its operating system, to use a.
SQL Server memory architecture and debugging memory Issues
 Demand Technology Software, Inc. Memory Leaks Demand Technology 1020 Eighth Avenue South, Suite 6, Naples, FL phone: (941) fax: (941)
Windows 2000 Memory Management Computing Department, Lancaster University, UK.
 Demand Technology Software, Inc. 32-bit Virtual Memory Constraints in Windows: an Update Mark Friedman Demand Technology 1020 Eighth Avenue South,
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
Chapter Oracle Server An Oracle Server consists of an Oracle database (stored data, control and log files.) The Server will support SQL to define.
Operating Systems Chapter 8
Chapter 8 Memory Management Dr. Yingwu Zhu. Outline Background Basic Concepts Memory Allocation.
8.4 paging Paging is a memory-management scheme that permits the physical address space of a process to be non-contiguous. The basic method for implementation.
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
Memory Management 1 Tanenbaum Ch. 3 Silberschatz Ch. 8,9.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
Windows Memory Architecture 井民全製作. A Process ’ s Virtual Address Space Every Process has its own private virtual address 32-bits processes  4 GB address.
1 Address Translation Memory Allocation –Linked lists –Bit maps Options for managing memory –Base and Bound –Segmentation –Paging Paged page tables Inverted.
CS 149: Operating Systems March 3 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Chapter 4 Memory Management Virtual Memory.
Computer Systems Week 14: Memory Management Amanda Oddie.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Main Memory. Chapter 8: Memory Management Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation Example: The.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Virtual Memory Hardware.
SQL Server Memory Architecture Sumit Sarabhai Microsoft Corp.
Paging (continued) & Caching CS-3013 A-term Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Swap Space and Other Memory Management Issues Operating Systems: Internals and Design Principles.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo 1 Memory management & paging.
CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16 pages of 4k bytes each. Your program thinks it has.
TOP 10 Thinks you shouldn’t do with/in your database
FILE SYSTEM IMPLEMENTATION 1. 2 File-System Structure File structure Logical storage unit Collection of related information File system resides on secondary.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Hands-On Microsoft Windows Server 2008 Chapter 7 Configuring and Managing Data Storage.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
Memory Management.
Session 3 Memory Management
Chapter 8: Main Memory.
Dynamics AX Performance
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy 11/12/2018.
Operating System Concepts
Page Replacement.
Windows CE Memory Management
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 8 11/24/2018.
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 12/1/2018.
Memory Management-I 1.
Main Memory Background Swapping Contiguous Allocation Paging
Lecture 3: Main Memory.
Unit OS5: Memory Management
CSCE 313 – Introduction to UNIx process
CSE 451: Operating Systems Autumn 2005 Memory Management
Transaction Log Internals and Performance David M Maxwell
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 4/5/2019.
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
COMP755 Advanced Operating Systems
CSE 542: Operating Systems
Presentation transcript:

SQL Server Memory Internals 김 종구 고객기술지원부 한국마이크로소프트

목 차 몸풀기 – Windows Memory Management Basic : Virtual Memory & AWE 실 전 – SQL Server Memory Internals 마무리 – SQL PASS 2003 PSS Service Center Labs : Troubleshooting Memory

몸풀기 Windows Memory Management Basic : Virtual Memory & AWE

사전지식 Inside Microsoft Windows 2000, 3rd Ed. Chap.7 Memory Management (esp. pp ) Programming Application for Microsoft Windows, 4 th Ed. Chap. 13 Windows Memory Architecture

메모리 공간 (Address Space) Physical Memory 공간 - 하드웨어에 의해 지원됨 - 0 ~ MAXsys 까지의 주소 Logical Memory 공간 - Process 가 보는 자신의 주소 - 0 ~ MAXprog 까지의 주소 Program 은 Logical Address 만 볼 수 있음. - Physical Address 는 볼 수 없음.

동적 Program 주소변경 Program 은 실행 시 주소가 동적으로 변경

SWAPPING Process 는 Memory 에서 저장장소 (Backing Storage) 로 잠시 swap 되었다가 실행 시 다시 메모리로 Load 될 수 있음.

Virtual Memory(1) User Logical Memory 를 Physical Memory 와 분리 - 실행 시 program 의 일부만이 메모리에 있다. - 따라서 Logical Address Space 는 Physical Address Space 보다 훨씬 클 수 있다. - Page 가 SWAP 될 수 있어야 한다.

Virtual Memory (2) 사용자에게 Memory 의 Physical 한 측면을 모두 감춤. - Memory 는 2 n Byte 만큼의 Virtual Address Space 를 가짐. - Virtual Address Space 의 일부분만이 Physical Memory 에 존재함.

Paging 원리 - Process 의 Physical Address Space 는 연속적이지 않을 수 있음. - Physical Memory 를 4K 단위의 frame 으로 나눔. - Logical Memory 를 4K 단위의 page 로 나눔. - n page 의 Program 을 수행하려면, n 개의 frame 이 필요함. - 모든 free frame 에 대한 추적이 필요함. - page 와 frame 이 1:1 로 mapping 됨. - page 는 logical address 에서 연속적이나 physical address 의 frame 은 임의의 위치에 존재함.

Page 와 frame 의 mapping

Virtual Address Translation Page 테이블 통해 virtual page 와 physical frame 을 mapping 함.

Page Fault Memory 상에 없는 Page 를 참조할 때 발생

Heaps(1) Process 가 생성 시 1MB 가 기본적으로 생성됨. 여러 개의 작은 데이터 block 을 할당하는데 유용함. ( 예 - linked list, tree 관리 ) Allocation granularity(64KB) 및 page boundary(4KB) 를 무시하고 필요한 만큼만 메모리를 할당함. 하지만 할당 및 해제가 느린 단점이 있음. Physical storage 에 대한 commit 과 decommit 에 대한 제어를 잃어버림.

Heap(2) A Process’s Default Heap - Windows Function 이 이 공간을 사용함 예 ) application 의 ANSI 문자열을 UNICODE 문자열로 변환 시 변환 문자 저장 Thread 들의 Heap allocation 은 serialize 됨 - 한번에 하나의 thread 만이 Heap memory 를 allocation 할 수 있음. - corruption 방지 추가적인 Heap 생성이 가능 (HeapCreate)

Shared Memory Process 들과 OS 사이에 메모리를 공유하는 것 예 ) 두 개의 process 가 동일한 DLL 을 공유하는 경우

Address Windowing Extensions (AWE) 32bit process 에게 제한된 주소 공간 (2G /3G) 을 초과하는 physical memory 를 할당하고 접근할 수 있도록 하는 방법 3 단계를 통해 AWE 를 이용 - 사용할 physical memory 할당 - physical memory 를 접근할 수 있는 window 역할을 할 virtual address space 지역 생성 - 생성된 window 를 통해 physical memory 접근

Address Windowing Extensions (AWE)

실 전 SQL Server Memory Internals - 이 세션은 PASS 2003 에서 SQL Server Escalation Engineer 인 Ken Henderson 에 의해 발표된 내용입니다.

329/SQL Server Memory Internals Ken Henderson/Author Keith Elmore/Microsoft* 2003 PASS Community Summit November 11-14, 2003 *This presentation is based on internal training developed by Keith Elmore of Microsoft

Goals Describe the SQL Server memory management architecture Provide a brief overview of the implementation of this architecture. Teach you how to figure out how to solve memory-related problems

Agenda Windows memory management review Memory usage and SQL Server processes Memory usage by non-SQL Server processes Monitoring memory usage Troubleshooting memory-related errors

Memory Management Review

Virtual vs. physical memory Windows provides virtual memory services Committed virtual memory is backed by some type of physical storage – usually the page file or physical memory Windows handles translations between virtual memory and physical storage 32-bit processes have a 4GB virtual memory address space divided between kernel- and user-accessible areas

Address Space Application Operating System 0xFFFFFFFF 0x x xC Application or OS (/3GB or /USERVA switch)

Reserved vs. Committed Free – not used Reserved – address space set aside for later use; no physical storage used Committed – physical storage is being used to store something

Allocation Granularity Determines the smallest memory reservation Depends on the operating system Is 64KB on 32-bit Windows

System Page Size Determines the smallest memory allocation Depends on the processor architecture Is 4KB on I386

xp_sysinfo Custom xproc from my last book Uses Win32 GetSystemInfo API to retrieve useful system info Returns the system page size, allocation granularity, etc. exec xp_sysinfo PageSize AllocationGranularity NumberOfProcessors ProcessorType Intel

Types of Win32 memory Virtual memory – a range of addresses that can be committed or reserved and be noncontiguous Heap – a (typically) small contiguous memory region Shared memory – can share memory between processes and allow files to be treated as memory

Virtual Memory VirtualAlloc -- only Win32 allocator that differentiates between reserving and committing Reserve a memory region (multiples of 64 KB) Commit some or all of a previously reserved region (multiples of 4 KB) Can reserve and commit separately or at the same time

Heaps HeapAlloc - used to allocate (typically) small chunks from the heap manager, which manages one or more regions that it allocated from virtual memory Each process has a default process heap (GetProcessHeap) Applications can create their own heaps (HeapCreate)

Shared Memory Two uses: Allows multiple processes to share memory Allows a file to be treated as memory CreateFileMapping and MapViewOfFile set it up Used by SQL Server’s shared memory NetLib to avoid the network stack

Other allocators C++ new operator COM IMalloc, CoTaskMemAlloc All allocators eventually resolve to VirtualAlloc

C++ new operator The default Microsoft C++ implementation simply calls HeapAlloc A class can overload new to do whatever it requires This operator is almost never used within SQL Server

IMalloc / CoTaskMemAlloc IMalloc is a standard interface for COM- based code to allocate memory CoTaskMemAlloc is semantically identical to IMalloc::Alloc SQL Server uses an interface derived from IMalloc to manage internal memory allocations

Review 2-3 GB memory available to application Memory can be free, reserved or committed All Win32 allocators eventually end up using Virtual* APIs to do their work

SQL Server Buffer Pool

Buffer Pool Size SQL Server calculates max buffer pool size and reserves it at startup The computation is based on physical memory Lowering max server memory does not change the amount of memory reserved To change the reservation size, increase MemToLeave (and shrink the maximum BPool size) via the -g parameter

Buffer Pool Allocations Up to 32 different regions can be allocated to set up the buffer pool The BPool maintains parallel arrays to track the start of each region and the number of pages in it, respectively SQL Server maintains a bitmap that tracks the committed pages in the BPool SQL Server maintains an array of structures (“BUFs”) that track allocated pages

Finding the BPool The BPool is exposed in a simple global variable that you can find with WinDbg Demo: Finding BPool

Buffer Pool & AWE Windows 2000 and later Involves mapping/unmapping a “view” of physical memory pages into the process virtual address space Limited amount of “visible” buffers at any given time (only what can be mapped into the process address space) May have to throw away procedure plans to clear space for mapping buffers

Review SQL Server dynamically adjusts the amount of memory it uses There is limited virtual address space left after the buffer pool reserves its maximum AWE helps some, but we’re still limited by virtual address space

Memory Allocation by SQL Server Processes

Memory Managers Connection – e.g., PSS and SRVPROC Query Plan – compiled plans/execution plans Optimizer – metadata and tree structures Utility – e.g., SQLTRACE, log manager, etc. General – e.g., locks OS (Reserved) “Manager” “No Fail” Low Memory “Manager”

Direct Calls to VirtualAlloc/HeapAlloc Transaction Log Backup Profiler (7.0) NetLibs

Callbacks to Allocate Memory srv_alloc – (typically used by xprocs) callback routine that allocates from general pool or from OS buffers, depending on size MSXML

Lazywriter Two purposes: Tries to keep a minimum number of free buffers (freeing dirty buffers requires I/O) Keeps enough physical memory free on the machine to avoid paging Sweeps across BUFs to “age” them Many things that it can’t remove Demo: Watching Lazywriter in action

Review Five core memory managers OS memory “manager” for allocations greater than 8 KB; low memory mgr for emergencies Some places still do direct VirtualAlloc srv_alloc -- callback into memory manager Lazywriter’s role in keeping memory available

Memory and External Components

Linked Servers Typically run in process Might be able to run out-of-process Even if run out-of-process, there are still some memory requirements to load the marshalling stubs

COM Objects Default sp_OACreate syntax creates objects in process Can force out of process even if app changes can’t be made (Q198891) Marshalling required if out of process All in process allocations use address space from MemToLeave

Extended Stored Procs Address space used for the DLL Memory allocations may come from buffer pool (srv_alloc = 8 KB, standard HeapAlloc or VirtualAlloc) Can be run from another server (Q243428)

Review External consumers of SQL Server memory: Linked servers COM objects (sp_OA) Extended stored procs The first two always use MemToLeave; the third may use buffer pool if using srv_alloc

Monitoring Memory Usage

Performance Counters Process Object Virtual Bytes – reserved virtual memory Private Bytes – committed virtual memory SQL Server:Buffer Partition Free list statistics SQL Server:Buffer Manager Stolen, Target, Total SQL Server:Memory Manager Memory manager statistics, etc.

DBCC MEMORYSTATUS Documented in the KB (Q271624) Same info is produced for certain errors PSS may ask you to run this in certain situations

vmmap, vmstat, tlist Command line tools vmmap and vmstat are variants of Richter-based internal tools that PSS may ask you to run vmmap dumps a row describing the attributes of each block of virtual memory vmstat displays summary info on the count, min, max and avg sizes of the different types tlist shows either a list of processes or details about threads/modules in a specific process

Review Performance Counters DBCC MEMORYSTATUS vmmap, vmstat, tlist

Troubleshooting

WARNING: Clearing procedure cache to free contiguous memory Allocation from OS Memory manager Normally out of address space in MemToLeave Start by capturing PerfMon, DBCC MEMORYSTATUS, and the errorlog May need a userdump

WARNING: Failed to [reserve | commit] contiguous memory Shortage of contiguous address space in MemToLeave (or physical memory on server)

Error Memory allocation from ODS component Issue may be in BPool or MemToLeave—need more information to troubleshoot further May require a userdump to effectively troubleshoot

Error 17802/Unable to spawn xxx thread Address space is exhausted or heavily fragmented What is loaded in the process? Are there leaks? May require a userdump to effectively troubleshoot

Error 701 -y701 is useful here to generate a stack dump when the error occurs (dbcc dumptrigger also works) Check sp_configure settings (e.g., Open Objects) Demo: -y and dumptrigger

Error 7399 Shortage of memory in the MemToLeave area Could be load related (OLE-DB provider is using lots of memory) and require a larger –g value May be a leak in the provider

Error 8645 Caused by many users running queries with sort or hash operators Goal is to find who is consuming all the workspace memory May be an indication that these queries are picking a bad plan or need some optimization

Further Reading Inside Windows 2000 (MS Press, 2000) by David Solomon and Mark Russinovich Programming Applications for Windows (MS Press, 1999) by Jeffrey Richter Inside SQL Server 2000 (MS Press, 2000) by Kalen Delaney The Guru’s Guide to SQL Server Architecture and Internals (Addison-Wesley, 2003) by Ken Henderson The BOL and various whitepapers on

Updates Server: privatenews.microsoft.com Account name: privatenews\servicecenter Password: PASS

마무리 SQL PASS 2003 PSS Service Center Labs

Troubleshooting memory By using 4 Memory Senarios

Important Resources SQL Server Books Online “Monitoring with SQL Profiler” SQL Server Books Online “Memory Architecture” KB Article “INF: General Performance Troubleshooting Using SQL Profiler Traces” KB Article “INF: SQL Server 7.0 and SQL Server 2000 Logging and Data Storage KB Article “INF: SQL Server Memory Usage” KB Article “HOW TO: Move Extended Stored Procedures Out of Process” KB Article “HOW TO: Troubleshoot SQL Server Error 8645”

© 2004 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.