Where the open source database community meets: Use code PERCONA75 and secure your spot for Percona Live.  Register

Best Practices for Configuring Optimal MySQL Memory Usage

May 3, 2016
Author
Peter Zaitsev
Share this Post:

Configuring Optimal MySQL Memory Usage
In this blog post, we’ll discuss best practices for configuring optimal MySQL memory usage.

Correctly configuring memory is critical for MySQL performance and stability. Default settings in MySQL 5.7 use very little memory, which is inefficient—but over-allocation can cause instability or crashes.

Key rule: MySQL should never cause the operating system to swap. Even small amounts of swapping activity can severely degrade performance.

Some swap usage is fine (for idle OS processes), but active swapping—visible in the si and so columns of vmstat—should be avoided.

Example: No Significant Swapping

Example: Heavy Swapping

If you’re using Percona Monitoring and Management, you can monitor swap activity directly.

Swap spikes over ~1MB/sec or sustained activity indicate memory misconfiguration.

Understanding MySQL Memory Usage

MySQL memory usage includes:

  • Global buffers
  • Per-connection buffers
  • Uncontrolled allocations (e.g., stored procedures)

Because of this complexity, it’s best to monitor actual usage via VSZ:

The 5th column shows VSZ (~11GB here).

Recommendation: Keep mysqld VSZ under ~90% of system memory.

How Much Memory to Allocate

Typically, MySQL should use no more than 90% of system memory to leave room for the OS and other processes.

Use less if:

  • Other applications share the server
  • OS caching is needed (e.g., MyISAM)
  • Workloads rely heavily on temporary files

Memory allocation considerations:

  • Workload memory: ~1–10GB depending on concurrency
  • Memory fragmentation: +10% overhead possible
  • Global buffers (e.g., buffer pool, caches)

Main components:

  • innodb_buffer_pool_size
  • key_buffer_size
  • query_cache_size
  • table_open_cache

Note: InnoDB buffer pool uses an additional 5–10% overhead.

Example Configuration

System: 16GB RAM

  • Allocate ~14.4GB to MySQL (90%)
  • Reserve 1GB for connections/workload
  • Reserve 0.4GB for misc buffers
  • Remaining ~13GB for buffer pool

Final recommendation:

Operating System Considerations

Swap

Keep swap enabled (minimum ~4GB or 25% RAM):

  • Allows OS to offload unused memory
  • Prevents OOM crashes

Reduce swap usage tendency:

OOM Killer

Prevent MySQL from being killed first:

NUMA

On multi-socket systems:

Or use:

More Resources

Posts

Webinars

Presentations

Free eBooks

Tools



Download Percona Server for MySQL Today!

0 0 votes
Article Rating
Subscribe
Notify of
guest

7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Fadi El-Eter (itoctopus)

Hi Peter,

What an excellent post!

About the swap, we typically set it to the exact size of the RAM. So, if a server has 64GB of RAM, then the swap is also 64GB. The thing is with SSDs, the swap is now more efficient than ever. Do you think that’s too much, given the fact that we have a generous amount of disk space? (Note: We did have a client who set the swap to 0, and the moment MySQL ran out of memory, the whole server crashed, so I see your point about the necessity of the swap).

About “innodb_buffer_pool_size” – you said to set it at around 12GB on a MySQL dedicated server with 16GB of RAM. Now, assuming an application with a heavy write activity, will there be a performance hit? I am assuming that the innodb_buffer_pool_size works the same way as the query_cache_size, which causes a performance hit when there is a table change because of invalidation.

doublemarket
9 years ago

Thank you for the useful post!

Do we have a way to know the overhead of the InnoDB buffer pool to retain additional data structure?

And since this is good, I translated this into Japanese https://yakst.com/ja/posts/3983.
Please let me know if it’s a problem.

Hamoon Mohammadian Pour

I think InnoDB Fulltext Index has different strategics to using Memory and If someone use Fulltext on Big table, he should control fultext variables to avoid eat Memory

Moll
8 years ago

Nice post, Peter!

I think making MySQL a less likely candidate to be killed by the OOM killer should be done with cautious though. Typically, on the dedicated database machines MySQL will be consuming about 90% of the memory and no other applications should be running there except for the operating system. Killing an OS process won’t release so much memory and it might corrupt the filesystem (I had a case where memory pressure corrupted the system bus).
In other words, killing MySQL is not the worst scenario in all cases!

Rajesh
Rajesh
7 years ago

Hi, I have a dual core with 256 GB virtual MySQL running on it. At times the machine runs very slow even to process a simple count query, Any reason…

Eimantas
Eimantas
6 years ago

Stroed procedures/functions and triggeres – are couse of high memory usage (all affected MySQL, Percona, MariaDB)
scripts and description here: https://jira.percona.com/projects/PS/issues/PS-6800

Any ideas how to control it? Config tips?

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved