The error occurs when a query or connection tries to allocate more memory than MariaDB's configured limits allow.
MariaDB calculates a worst-case memory footprint for each connection. If the product of join_buffer_size
, sort_buffer_size
, read_buffer_size
, and other per-thread buffers exceeds max_connections * key_buffer_size
, the server aborts the query and raises this error.
Run SHOW VARIABLES LIKE '%_buffer_size'
and SHOW GLOBAL STATUS LIKE 'Max_used_connections'
. Compare the values against total RAM.This highlights which buffers are oversized for your workload.
Key variables are join_buffer_size
, sort_buffer_size
, read_buffer_size
, read_rnd_buffer_size
, tmp_table_size
, and max_heap_table_size
. Lowering them or using session-level overrides prevents runaway memory allocation.
Yes. Issue SET SESSION join_buffer_size = 2M
(or similar) before heavy queries.Session changes apply immediately and do not affect other users.
Edit my.cnf
under the [mysqld]
section:
join_buffer_size = 2M
sort_buffer_size = 2M
tmp_table_size = 256M
max_heap_table_size = 256MRestart MariaDB to apply. Size each buffer so that max_connections × (buffer_sum)
is less than 70-80 % of RAM.
Large ORDER BY
, multi-table JOIN
s, and implicit in-memory temporary tables cause spikes.Rewrite queries to add selective WHERE
clauses, proper indexes, or break them into smaller steps.
Before running an analytics join between Orders
, OrderItems
, and Products
, set buffers for the session:
SET SESSION join_buffer_size = 1M;
SET SESSION sort_buffer_size = 1M;
SELECT c.name, SUM(oi.quantity) AS items
FROM Customers c
JOIN Orders o ON c.id = o.customer_id
JOIN OrderItems oi ON o.id = oi.order_id
GROUP BY c.name;
The query finishes without triggering the memory error.
Monitor Max_used_memory
regularly, cap max_connections
realistically, and always size buffers conservatively.Use EXPLAIN
to ensure queries leverage indexes and avoid full scans.
Over-allocating buffers globally: Setting join_buffer_size=64M
looks harmless but multiplies by every thread. Keep defaults low and tune per session.
Ignoring temporary table limits: If tmp_table_size
is smaller than needed, MariaDB writes to disk instead of failing. Balance performance and safety; set it high enough yet within RAM limits.
Enable the Performance Schema’s memory_summary_by_thread_by_event_name
table to pinpoint which allocations spike during problem periods.
.
No. MariaDB safeguards prevent accidental OOM kills. Always tune buffers instead of disabling protections.
More RAM provides headroom but poor buffer sizing can still exhaust memory. Tuning remains essential even on larger servers.
Yes. Adding LIMIT reduces result sets, allowing queries to finish within smaller buffers. Combine with proper indexes for best effect.