MySQL Server Memory Usage

Every so often people ask me the question how should they estimate memory consumption by MySQL Server in given configuration. What is the formula they could use.

The reasons to worry about memory usage are quite understandable. If you configure MySQL Server so it uses too small amount of memory it will likey perform suboptimally. If you however configure it so it consumes too much memory it may be crashing , failing to execute queries or make operation to swap seriously slowing down. On now legacy 32bit platforms you could also run out of address space so that had to be watched as well.
Having said so, I do not think looking for the secret fomula to compute your possible memory usage is the right approach to this problem. The reasons are – this formula is very complex nowadays and what is even more important “theoretically possible” maximum it provides have nothing to do with real memory consumptions. In fact typical server with 8GB of memory will often run with maximum theoretical memory usage of 100GB or more. Furthermore there is no easy “overcommit factor” you can use – it really depends on application and configuration. Some applications will drive server to 10% of theoretical memory consumptions others only to 1%.

So what could you do instead ? First take a look at global buffers which are allocated at start and always where – these are key_buffer_size, innodb_buffer_pool_size, innodb_additional_memory_pool_size, innodb_log_buffer_size, query_cache_size. If you’re using MyISAM seriously you can also add the size of Operation System cache you would like MySQL to use for your table. Take this number add to it number of memory Operation System and other applications need, add might be 32MB more for MySQL Server code and various small static buffers. This is memory which you can consider used when you just start MySQL Server. The rest of memory is available for connections. For exampe with 8GB server you might have everything listed adding up to 6GB, so you have 2GB left for your threads.

Each thread connecting to MySQL server will needs its own buffers. About 256K is allocated at once even if thread is idle – they are used by default thread stack, net buffer etc. If transaction is started some more space can add up. Running small queries might only barely increase memory consumption for given thread, however if table will perform complex operations such as full table scans, sorts, or need temporary tables as much as read_buffer_size, sort_buffer_size, read_rnd_buffer_size, tmp_table_size of memory might be allocated. But they are only allocated upon the need and freed once given stage of query is done. Some of them are allocated as single chunk at once others, for example tmp_table_size is rather maximum amount of memory MySQL will allocate for this operation. Note it is more complicated than once may think – multiple buffers of the same type might be allocated for exampe to handle subqueries. For some special queries memory usage might be even larger – bulk inserts may allocate bulk_insert_buffer_size bytes of memory if done to MyISAM tables. myisam_sort_buffer_size used for ALTER TABLE, OPTIMIZE TABLE, REPAIR TABLE commands.

For OLTP applications with simple queries memory consumption is often less than 1MB per thread with default buffers, and you really do not need to increase per thread buffers unless you have complex queries. Sorting 10 rows will be as fast with 1MB sort buffer as with 16MB (actually 16MB might be even slower but it is other story).

Another approach you may take is to come up with amount of memory you want MySQL Server to consume at peak. This can be easily computed by memory needed for OS, File Cache and other applications. For 32bit envinronment you also should keep 32bit limits into account and probably limit “mysqld” size to about 2.5GB (exact number depens on a lot of factors). Now you can use “ps aux” to see VSZ – Virtual Memory allocated by MySQL process. You can also look at “Resident Memory” but I find it less helpful as it may down because of swapping – not what you would like to see. Monitor how the value changes so you know memory requirements with current settings and increase/decrease values appropriately.

Some may say, Hey we want to have 100% guarantee our server will never run out of memory, no matter which queries or users will decide to run. Unfortunately this is as much close to impossible to be impractical. Here is why:

List of rarely considered MySQL Server Memory Requirements

Thread buffers can be allocated more than once for each thread. Consider for example subqueries – each layer may need its own read_buffer,sort_buffer, tmp_table_size etc

Many variabes can be set per connection. So you can’t relay on global values if developers may use their local values to run some queries.

There can be mutiple key caches. Multiple key caches can be created to accomodate query executions

Query Parsing and optimization needs memory. This is usually small to be ignored but certain queries can have very large memory requrement for this step, especially specially crafted ones.

Prepared statements and Cursors. Single connection may have many prepared statements and cursors. Their number finally can be limited but each of them still can have very large memory consumption

Innodb Table Cache. Innodb has its own table cache in which meta data about each table accessed from the start is stored. It is never purged and may be large if you have a lot of tables. It also means user having CREATE TABLE privilege should be able to run MySQL server out of memory

MyISAM buffers. MyISAM may allocate buffer which is large enough to contain largest record in the given table which is held until table is closed.

Federated Storage Engine. This may have unbound memory requirements retriving result sets from remove queries.

Blobs may require 3x time of memory. This is important if you’re deaing with large Blobs (your max_allowed_packet is large) Processing of 256MB of blob may require 768MB of memory.

Storage Engines. In general storage engines may have their own per thread or global memory allocations which are not tuned as buffers. Watch for these especially now with many storage engines being released for MySQL by various parties.

I do not pretend this to be complete list. On the contrary I’m quite sure I’ve missed something (drop me a note if you have something to add). But the main point is – there are a lot of memory consumers out where and trying to find peak possible usage for each is impractical – so my advice would be measure what you get in practice and how memory consumption reacts to changing various variables. For example you may find out increasing sort_buffer_size from 1MB to 4MB and 1000 max_connections increases peak memory consumption just 30MB not 3000MB as you might have counted.

Related

Peter managed the High Performance Group within MySQL until 2006, when he founded Percona. Peter has a Master's Degree in Computer Science and is an expert in database kernels, computer hardware, and application scaling.

24 Comments

DELL R710, Memory :16GB, OS : Ubuntu server 11.04.
I use Mysql server version 5.5. some table using engine MYISAM and other is INNODB. till now use for both of that type is running well. but when many user acces my web, this server become very very slow.

I want to add about InnoDB memory. InnoDB allocates additional memory for internal needs (adaptive hash index, etc) which you should take into account. My investigations show it is about
innodb_buffer_pool_size / 20 + ~20MB

We have a really fantastics dating site serving 8000 logged in users. However the service goes down too often to call it stable. We have a montoring tool that states that we often hit the “max_connections” threshold (650) connections.
Cpu is fine (about 100% of 400% available, MPM and dual core reports 4 processors).

We have 6Gb of physical memory available but since we run on 32 bit we cannot turn up the innodb_buffer_pool higher than 1.8G right? 2.4 kernel (cannot change to 2.6 because of internal politics). Tmp area is mounted on RAM disk.

We only have innodb in the app.

The application is designed in a way that if allowed we will have something like 1000+ db connections since every user clicks on a link, refreshes his/her browser every 10 secs or so. We cache alot but still we have db problems.

a. The thing that I really, really would like to know is howto track down where the bottleneck is… I’ve read Peter Z two guides but cannot really convert the gained knowledge to solve our situation.

Lesser questions but probably as well important.
b. The question really is how do you determine the memory consumption per connection in average?
This leads to the next question
c. Can we have a limit of 600 connections when every connection consumes X mb. If the answer to ‘b’ is 2mb then the total amount of memory would be something like 1800 + 2*600 = 3G right? To much to handle for 32 bit?

Some stats at writing time (18:37 friday) System copes fine with these figures.
Queries/sec: 265
Traffic: 1Mb/sec
Connections:259

Hello,
I have a 64GB server, innodb buffer pool set to 40GB or less, use innodb_flush_method = O_DIRECT, 2X8GB innodb log, and has 30000 connections with 20 thread pool worker threads. It ran out of free memory and got heavy swap at the same time OS cache is 12GB. What’s the possible reason the redhat linux 5 can’t use the 12GB cache, but use swap instead?
Thanks.

How much VSZ do you get for MySQL in this case? When you have 30000 connections there can be significant amount of memory allocated per connection, which can also be tuned by adjusting various per thread buffers

We are having a problem with CPU high usage, its is going up to 98%. We have a DELL server with 2 – 4 cores CPUs means total 8 cores and 32 GB RAM. We are using Percona 5.5 and our most of tables are innodb, size of database 3 GB:

Please let me know if we are having any wrong value or we can do some settings

Problem i have is OS runs out of memory and kills mysql. I have noticed recently mysql using all connections whcih does not make sense as the site is not busy. It gets used more often during lunch times, which is when apache consumes avg over 100mb per process.

Hi sharif, thanks for the question! However, Peter wrote this post 9 years ago so I doubt he’ll see it to respond. The best place to ask this is on our MySQL discussion forums here: https://www.percona.com/forums/