Fundamentals 9 min read

Understanding Memory Management: From OS to MySQL

This article explores memory management concepts from both operating system and MySQL perspectives, covering virtual vs physical memory, process memory usage analysis using tools like top and /proc, and experimental verification of memory allocation behaviors.

Tencent Database Technology
Tencent Database Technology
Tencent Database Technology
Understanding Memory Management: From OS to MySQL

This comprehensive article explores memory management concepts from two perspectives: operating system fundamentals and MySQL-specific memory usage. The article begins by establishing the importance of memory as a critical resource for program execution, particularly for databases like MySQL where efficient memory usage directly impacts performance.

From the operating system perspective, the article introduces key memory concepts including virtual memory space versus resident memory space. Virtual memory is allocated through functions like mmap, malloc, and new, but doesn't necessarily occupy physical memory until accessed. Resident memory represents actual physical memory usage. The article demonstrates how to analyze memory usage using tools like the top command, which shows VIRT (virtual memory) and RES (resident memory) metrics for processes.

The article then delves into more detailed memory analysis using the /proc/[pid]/status file, which provides comprehensive information about a process's memory consumption including VmSize (total virtual memory), VmRSS (resident memory), VmExe (code segment size), VmData (data segment size), and VmLib (shared library size). The size command is introduced as a way to statically analyze executable files to understand their memory layout, showing text (code), data (initialized global variables), and bss (uninitialized global variables) segments.

A detailed explanation of Linux process memory layout is provided, showing how different memory segments are organized in virtual address space, starting from the code segment at 0x400000 and extending upward. The article explains that many virtual memory allocations don't immediately consume physical memory - only when data is actually read or written do page faults occur, triggering the mapping of virtual to physical memory.

The second major section presents experimental code to verify memory concepts. The C++ code demonstrates various memory allocation scenarios including local variables on the stack, malloc-allocated heap memory, and mmap-allocated memory. The experiments show that uninitialized local variables don't consume physical memory, while initialized ones do. Both malloc and mmap allocations consume physical memory when initialized.

The article concludes by emphasizing memory's critical role in MySQL performance and mentions that the next article will cover MySQL-specific memory management. References to Linux documentation and tools are provided for further reading.

performance optimizationmemory managementLinuxMySQLOperating SystemsVirtual MemorySystem Administrationprocess memoryresident memory
Tencent Database Technology
Written by

Tencent Database Technology

Tencent's Database R&D team supports internal services such as WeChat Pay, WeChat Red Packets, Tencent Advertising, and Tencent Music, and provides external support on Tencent Cloud for TencentDB products like CynosDB, CDB, and TDSQL. This public account aims to promote and share professional database knowledge, growing together with database enthusiasts.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.