How to Diagnose and Fix MySQL Error 1037: “Out of memory; restart server and try again (needed %d bytes)”

Encountering Error 1037 in MySQL indicates that the server has run out of memory while attempting to perform an operation. This error can be triggered by various factors, and resolving it requires a systematic approach to identify and address the root cause. Here, we’ll explore different scenarios that can lead to this error and provide guidance on how to resolve them.

Scenario 1: Insufficient System Memory

The most straightforward cause is that the physical memory available on the server is insufficient for the MySQL process’s needs.

Check the system’s memory usage using tools like top, htop, or free -m on Linux, or Task Manager on Windows.

Close unnecessary applications to free up memory, or add more physical memory (RAM) to the server.

Scenario 2: Configuration Limits

MySQL configuration settings, such as buffer_pool_size for InnoDB, might be set too high for the available system memory, or other memory-related settings are too low.

Inspect the MySQL configuration file (my.cnf or my.ini) for memory-related settings.

Adjust the configuration to more conservative values that match your system’s memory capacity. For instance:

innodb_buffer_pool_size = 256M

After updating the configuration, restart the MySQL server.

Scenario 3: Memory Leaks

A memory leak in MySQL or other applications running on the server can exhaust memory over time.

Monitor memory usage over time to identify if there’s a gradual increase without release.

Update to the latest version of MySQL and other software to ensure any known memory leaks are patched. If the issue persists, consider profiling the application to find and fix the memory leak.

Scenario 4: Large or Complex Queries

Running large or complex queries can consume a significant amount of memory.

Review the queries that were running at the time of the error, especially those involving large datasets or complex joins.

Optimize the queries to be more efficient, reduce the size of the result set, or increase the join_buffer_size and sort_buffer_size settings in small increments.

join_buffer_size = 4M
sort_buffer_size = 2M

Scenario 5: Inadequate Swap Space

If the server runs out of physical memory, it can use swap space as an overflow. Insufficient swap space can lead to out-of-memory errors.

Check the swap space usage with swapon -s on Linux.

Increase the swap space on the server. For example, on a Linux system, you can create a new swap file:

dd if=/dev/zero of=/swapfile bs=1M count=1024
mkswap /swapfile
swapon /swapfile

Scenario 6: Concurrent Connections and Threads

A high number of concurrent connections or threads can consume more memory than available.

Review the max_connections and thread_cache_size settings in MySQL.

Adjust these settings to match the server’s memory capacity and workload.

max_connections = 150
thread_cache_size = 8


MySQL Error 1037 is a clear indication of memory-related issues. By diagnosing the problem through memory usage inspection, configuration review, and query analysis, you can identify the cause and implement the appropriate fix. Always remember to monitor the server’s performance after making changes and to make incremental adjustments to avoid overcommitting memory resources.

Regularly updating your MySQL server and optimizing your system’s performance can also help prevent this error. If you’re consistently facing memory issues, it may be time to consider scaling up your server’s hardware or optimizing your application’s architecture.

For more in-depth information on memory management and performance tuning in MySQL, the official MySQL Documentation is an excellent resource.

Leave a Comment