Databases 12 min read

Common MySQL OOM Scenarios and TDSQL‑C Memory Optimization Strategies

This article examines typical MySQL out‑of‑memory (OOM) situations in production, explains how to diagnose memory usage with Performance Schema and other tools, and presents a series of TDSQL‑C‑specific optimization techniques—including server‑parameter tuning, process‑list monitoring, cold‑page detection, buffer‑pool limits, and dynamic resizing—to mitigate OOM risks.

Tencent Database Technology
Tencent Database Technology
Tencent Database Technology
Common MySQL OOM Scenarios and TDSQL‑C Memory Optimization Strategies

Out‑of‑memory (OOM) errors occur when a MySQL instance exceeds its allocated memory, causing the process to be killed and resulting in brief unavailability. Because MySQL’s memory management is complex and performance schema is disabled by default, diagnosing OOM can be challenging.

Common OOM causes in production:

Large InnoDB data dictionary memory when many tables exist (e.g., >200k tables and 700k indexes), which is not counted in the buffer pool.

Memory spikes caused by large queries; performance schema can reveal which modules consume the most memory.

Prepared‑statement cache growth, especially with tools like sysbench that enable it by default.

Excessive connection count on small‑memory instances.

Oversized net buffers (e.g., 16 MB) due to large statements or long‑lived connections.

Kernel bugs leading to memory leaks, detectable with valgrind.

Typical diagnostic queries include:

# 1. Query memory usage per module
SELECT SUBSTRING_INDEX(event_name,'/',2) AS code_area,
       sys.format_bytes(SUM(current_alloc)) AS current_alloc
FROM sys.x$memory_global_by_current_bytes
GROUP BY SUBSTRING_INDEX(event_name,'/',2)
ORDER BY SUM(current_alloc) DESC;
# 2. Identify top connections by memory
SELECT thread_id, event_name, SUM_NUMBER_OF_BYTES_ALLOC
FROM performance_schema.memory_summary_by_thread_by_event_name
ORDER BY SUM_NUMBER_OF_BYTES_ALLOC DESC
LIMIT 20;
# 3. Detail of a specific connection
SELECT *
FROM performance_schema.threads
WHERE THREAD_ID = xxx;

Additional queries can retrieve per‑connection memory components such as SERVER_MEMORY_USED, INNODB_MEMORY_USED, PFS_MEMORY_USED, and OS_MEMORY_USED.

TDSQL‑C overview: TDSQL‑C is a MySQL‑compatible distributed database that separates compute and storage, providing high performance, low cost, large storage, low replication lag, rapid scaling, and serverless features. This architecture introduces extra memory consumption for log‑send buffers, remote page‑IO queues, and communication management.

Optimization measures for TDSQL‑C:

Server‑side parameter tuning (e.g., reducing innodb_log_buffer_size , innodb_ncdb_log_buffer_size , key_buffer_size , innodb_ncdb_wait_queue_size , innodb_ncdb_log_flush_events ) can lower memory usage without noticeable performance loss.

Expose information_schema.detail_processlist to quickly query per‑connection memory usage and aggregate totals.

Introduce innodb_hot_page_time and show coldpage status to count cold pages, helping users size the buffer pool appropriately.

Limit buffer‑pool growth with innodb_max_lru_pages_pct to prevent OOM even when usage is below 100%.

Improve dynamic buffer‑pool resizing to reduce performance jitter, especially when shrinking the pool, by minimizing the time the buffer‑pool mutex is held.

Experimental results on a 2c4g instance showed that after applying these tunings, memory consumption dropped by roughly 200 MB with no significant performance degradation, and dynamic resizing caused only brief QPS fluctuations.

Conclusion: Memory‑related OOM issues remain a major challenge for database systems. TDSQL‑C addresses these through comprehensive server‑parameter optimization, richer monitoring, cold‑page detection, buffer‑pool usage caps, and refined resizing algorithms, thereby enhancing stability and user experience.

memory-optimizationMySQLoomDatabase PerformanceTDSQL-C
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.