Troubleshooting Memory Overload
Scenarios
Memory overload mainly occurs in two scenarios:
- Scenario 1: Memory usage increases slowly and continuously, eventually causing out of memory (OOM) errors.
- Scenario 2: Memory usage increases sharply in a short period, eventually causing OOM errors.
Possible Causes
Scenario 1 (continuous slow memory rise leading to OOM)
- Setting memory parameters to excessively large values results in high session-level memory usage globally or per connection. The usage varies with executed SQL statements.
Run the following command to query global memory allocation:
```plaintext show variables where variable_name in ( 'innodb_buffer_pool_size','query_cache_size' ); ```
Run the following command to query session-level private memory allocation:
```plaintext show variables where variable_name in ( 'read_buffer_size','read_rnd_buffer_size','sort_buffer_size','join_buffer_size','binlog_cache_size','tmp_table_size' ); ```
Adjust these parameters to keep memory usage within a stable, reasonable range (75% to 90% without large fluctuations).
- Setting client idle timeout parameters to excessively large values causes long-lived connections that cannot be released.
Check client idle timeout parameters, such as minEvictableIdleTimeMillis and maxEvictableIdleTimeMillis for the Druid connection pool. For example, if the client idle timeout is set to 60s but the connection sends SQL requests every second and never becomes idle, the connection will remain as a long-lived connection that will not be released.
During use, a long-lived connection may experience changes in the amount of data it accesses or in the length of SQL text it receives, causing its private memory block to expand. The memory allocation policy reuses the memory block and does not proactively return it to the operating system. It is released only when the connection is closed. As a result, each long-lived connection may occupy a large memory block (up to 1 GB), eventually leading to OOM issues.
- The instance has many large-text stored procedures, causing memory usage to increase slowly and remain unreleased after the stored procedures are executed.
When a stored procedure is created for the first time, it is parsed and compiled into an execution plan and stored in the database. If a connection executes a stored procedure with large text, the compiled stored procedure is stored in the connection's sp_head:mem_root memory block. Due to its precompiled nature, this memory block is released only when the connection closes. If different stored procedures are invoked continuously without releasing connections, memory usage increases over time.
Scenario 2 (sudden memory spike leading to OOM)
- Oversized SQL text causes excessive memory consumption during lexical analysis as well as high network buffer usage.
During lexical and syntax parsing of SQL text, RDS for MySQL stores the keywords and data from the entire SQL query in corresponding memory structures. When SQL text is too large, it consumes significant memory, causing a rapid increase in memory usage and triggering an OOM error.
The typical types of large SQL text include:
- Type 1: Large SQL text caused by excessive values in IN clauses
select column from table where column2 in (a1,a2,a3,a4,a5,a6...aN);
- Type 2: Large SQL text caused by inserting too many rows in a single INSERT statement
insert into large_data_table values(1,'Jack'),(2,'Tom'),...(500000,'xxx')
- Type 3: Single INSERT statement involving long text fields (for example, LONGTEXT, TEXT, BLOB, or JSON)
insert into large_text_table values(1, 'xxxx...'),(2, 'xxx...');
- Type 1: Large SQL text caused by excessive values in IN clauses
- The SQL result set contains extremely large single rows, causing instantaneous high memory consumption.
When RDS for MySQL returns results, it reads and sends the results in batches based on the net_buffer_size. However, the minimum unit of the buffer is a single row. If a row is very large, for example, containing longtext, text, blob, or json fields, memory usage may rise sharply and trigger OOM issues.
SQL example:
select * from large_text_table limit 1;
Impact on Workloads
An OOM error may cause the database to crash and typically results in connection interruptions lasting under 1 minute.
Handling Suggestions
- Preventive measures before overload occurs
- Check whether idle timeout parameters in client drivers and connection pools are set appropriately.
- Check for long-text SQL statements in workloads. These statements are typically generated by code. You are advised to set a maximum length for SQL text to block such statements.
- Use application logic instead of stored procedures whenever possible.
- Emergency mitigation measures during overload
For scenarios with slow, continuous memory usage increase, terminate long-lived sessions to free up memory. For details, see Managing Real-Time Sessions.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot