{"id":25455,"date":"2010-07-19T19:51:04","date_gmt":"2010-07-19T19:51:04","guid":{"rendered":"http:\/\/euvolution.com\/futurist-transhuman-news-blog\/audit-your-mysql-memory-usage\/"},"modified":"2010-07-19T19:51:04","modified_gmt":"2010-07-19T19:51:04","slug":"audit-your-mysql-memory-usage","status":"publish","type":"post","link":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/planetology\/audit-your-mysql-memory-usage.php","title":{"rendered":"Audit Your MySQL Memory Usage"},"content":{"rendered":"<p><img decoding=\"async\" class=\"profile\" src=\"http:\/\/euvolution.com\/futurist-transhuman-news-blog\/wp-content\/plugins\/wp-o-matic\/cache\/2ff70_mboehm.jpg\" alt=\"Matthew Boehm\" style=\"padding-left:10px; padding-right: 10px;\">Ever wonder why your MySQL server runs out of memory or your server starts swapping it like crazy? It could be that you are allowing too many connections or have a buffer that isn&rsquo;t being used. Here are some simple formulas you can use to determine how much memory your MySQL server can use.<\/p><p>All of these variable values can be seen by using &ldquo;<code>SHOW GLOBAL VARIABLES<\/code>&rdquo; at the MySQL client prompt.  They are given in bytes so you must convert to KB, MB or GB by dividing the value returned by 1024, 1048576 or 1073741824, respectively.<\/p><p>Remember: Each connection from your application is referred to as a thread by MySQL.<\/p><p><strong>Per-Thread Memory Use<\/strong> (The amount of memory a single connection can use):<\/p><p><code>read_buffer_size<\/code> + <code>read_rnd_buffer_size<\/code> + <code>sort_buffer_size<\/code> + <code>thread_stack<\/code> + <code>join_buffer_size<\/code><\/p><p>Example: 1048576 + 2097152 + 1048576 + 262144 + 131072 = 4587520 bytes<br>Divide that by 1048576 to get the usage in MB, and you find that each thread can use up to 4.375 MB of memory.<\/p><p>Now, take that amount and multiply by your <code>max_connections<\/code>, and you&rsquo;ll find the total potential memory usage. For this example, let&rsquo;s set <code>max_connections<\/code> to 350. The example server could use 1,531.25 MB or 1.49 GB if all 350 connections were in use at a given time. If we have 4GB of RAM in the server, that accounts for almost one third of our available memory.<\/p><p>And that&rsquo;s not all! There are several other &ldquo;global buffers&rdquo; that MySQL creates depending on which table engines you are using. The formula below assumes you have a mix of MyISAM and InnoDB tables and you are using the query cache:<\/p><p><strong>Base MySQL Memory Usage<\/strong><\/p><p><code>key_buffer_size<\/code> + <code>max_heap_table_size<\/code> + <code>innodb_buffer_pool_size<\/code> + <code>innodb_additional_mem_pool_size<\/code> + <code>innodb_log_buffer_size<\/code> + <code>query_cache_size<\/code><\/p><p>Example: 1073741824 + 33554432 + 2147483648 + 134217728 + 10485760 + 536870912 = 3936354304 bytes<br>To get the usage in GB, we divide by 1073741824 and see that 3.66 GB is being used!<\/p><p>Again, in a 4GB server, base MySQL &ndash; with no connections &ndash; can use 3.66GB, or about 90 percent of my server&rsquo;s physical memory. Yikes! When you see that, your first move should be to contact your sales rep to get more memory installed. We advise, for system stability, never to assign more than 80 percent of overall system memory to MySQL (or any process for that matter).<\/p><p>One other area to check for memory reduction is your client application code.  While the PHP manual says you don&rsquo;t have to explicitly call <code>mysql_close<\/code> or <code>mysqli_close<\/code>, we highly recommend it as a best practice because there&rsquo;s a known PHP bug that prevents connections from being properly closed on script termination. And if each connection to MySQL eats up a base amount of memory whether it&rsquo;s being used or not, your server will suffer for no reason.<\/p><p>-Matthew, CMDBA 5.0<\/p><div><h4>Related Posts:<\/h4><ul><li><a href=\"http:\/\/blog.theplanet.com\/2008\/11\/13\/mysql-explain-yourself\/\" rel=\"bookmark\">MySQL &ndash; EXPLAIN Yourself<\/a><\/li><li><a href=\"http:\/\/blog.theplanet.com\/2009\/05\/26\/mysql-update-from-hidden-select\/\" rel=\"bookmark\">MySQL &ndash; UPDATE from (Hidden) SELECT<\/a><\/li><li><a href=\"http:\/\/blog.theplanet.com\/2010\/06\/02\/unix-sysadmin-boot-camp-your-logs-and-you\/\" rel=\"bookmark\">UNIX Sysadmin Boot Camp: Your Logs and You<\/a><\/li><li><a href=\"http:\/\/blog.theplanet.com\/2010\/05\/26\/unix-sysadmin-boot-camp-bash\/\" rel=\"bookmark\">UNIX Sysadmin Boot Camp: bash<\/a><\/li><li><a href=\"http:\/\/blog.theplanet.com\/2010\/05\/24\/unix-sysadmin-boot-camp-ssh\/\" rel=\"bookmark\">UNIX Sysadmin Boot Camp: SSH<\/a><\/li><\/ul><\/div><p>\t<a rel=\"nofollow\" href=\"http:\/\/www.stumbleupon.com\/submit?url=http%3A%2F%2Fblog.theplanet.com%2F2010%2F07%2F19%2Faudit-your-mysql-memory-usage%2F&amp;title=Audit%20Your%20MySQL%20Memory%20Usage\" title=\"StumbleUpon\"><img decoding=\"async\" src=\"http:\/\/euvolution.com\/futurist-transhuman-news-blog\/wp-content\/plugins\/wp-o-matic\/cache\/2ff70_stumbleupon.png\" alt=\"StumbleUpon\" class=\"sociable-hovers\" style=\"padding-left:10px; padding-right: 10px;\"><\/a><br>\t<a rel=\"nofollow\" href=\"http:\/\/twitter.com\/home?status=Audit%20Your%20MySQL%20Memory%20Usage%20-%20http%3A%2F%2Fblog.theplanet.com%2F2010%2F07%2F19%2Faudit-your-mysql-memory-usage%2F\" title=\"Twitter\"><img decoding=\"async\" src=\"http:\/\/euvolution.com\/futurist-transhuman-news-blog\/wp-content\/plugins\/wp-o-matic\/cache\/2ff70_twitter.png\" alt=\"Twitter\" class=\"sociable-hovers\" style=\"padding-left:10px; padding-right: 10px;\"><\/a><br>\t<a rel=\"nofollow\" href=\"http:\/\/www.dzone.com\/links\/add.html?url=http%3A%2F%2Fblog.theplanet.com%2F2010%2F07%2F19%2Faudit-your-mysql-memory-usage%2F&amp;title=Audit%20Your%20MySQL%20Memory%20Usage\" title=\"DZone\"><img decoding=\"async\" src=\"http:\/\/euvolution.com\/futurist-transhuman-news-blog\/wp-content\/plugins\/wp-o-matic\/cache\/2ff70_dzone.png\" alt=\"DZone\" class=\"sociable-hovers\" style=\"padding-left:10px; padding-right: 10px;\"><\/a><br>\t<a rel=\"nofollow\" href=\"http:\/\/digg.com\/submit?phase=2&amp;url=http%3A%2F%2Fblog.theplanet.com%2F2010%2F07%2F19%2Faudit-your-mysql-memory-usage%2F&amp;title=Audit%20Your%20MySQL%20Memory%20Usage&amp;bodytext=Ever%20wonder%20why%20your%20MySQL%20server%20runs%20out%20of%20memory%20or%20your%20server%20starts%20swapping%20it%20like%20crazy%3F%20It%20could%20be%20that%20you%20are%20allowing%20too%20many%20connections%20or%20have%20a%20buffer%20that%20isn%27t%20being%20used.%20Here%20are%20some%20simple%20formulas%20you%20can%20use%20to%20determine%20h\" title=\"Digg\"><img decoding=\"async\" src=\"http:\/\/euvolution.com\/futurist-transhuman-news-blog\/wp-content\/plugins\/wp-o-matic\/cache\/2ff70_digg.png\" alt=\"Digg\" class=\"sociable-hovers\" style=\"padding-left:10px; padding-right: 10px;\"><\/a><br>\t<a rel=\"nofollow\" href=\"http:\/\/delicious.com\/post?url=http%3A%2F%2Fblog.theplanet.com%2F2010%2F07%2F19%2Faudit-your-mysql-memory-usage%2F&amp;title=Audit%20Your%20MySQL%20Memory%20Usage&amp;notes=Ever%20wonder%20why%20your%20MySQL%20server%20runs%20out%20of%20memory%20or%20your%20server%20starts%20swapping%20it%20like%20crazy%3F%20It%20could%20be%20that%20you%20are%20allowing%20too%20many%20connections%20or%20have%20a%20buffer%20that%20isn%27t%20being%20used.%20Here%20are%20some%20simple%20formulas%20you%20can%20use%20to%20determine%20h\" title=\"del.icio.us\"><img decoding=\"async\" src=\"http:\/\/euvolution.com\/futurist-transhuman-news-blog\/wp-content\/plugins\/wp-o-matic\/cache\/98e96_delicious.png\" alt=\"del.icio.us\" class=\"sociable-hovers\" style=\"padding-left:10px; padding-right: 10px;\"><\/a><br>\t<a rel=\"nofollow\" href=\"http:\/\/technorati.com\/faves?add=http%3A%2F%2Fblog.theplanet.com%2F2010%2F07%2F19%2Faudit-your-mysql-memory-usage%2F\" title=\"Technorati\"><img decoding=\"async\" src=\"http:\/\/euvolution.com\/futurist-transhuman-news-blog\/wp-content\/plugins\/wp-o-matic\/cache\/98e96_technorati.png\" alt=\"Technorati\" class=\"sociable-hovers\" style=\"padding-left:10px; padding-right: 10px;\"><\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Ever wonder why your MySQL server runs out of memory or your server starts swapping it like crazy? It could be that you are allowing too many connections or have a buffer that isn&rsquo;t being used. Here are some simple &hellip; <a href=\"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/planetology\/audit-your-mysql-memory-usage.php\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"limit_modified_date":"","last_modified_date":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[34],"tags":[],"class_list":["post-25455","post","type-post","status-publish","format-standard","hentry","category-planetology"],"modified_by":null,"_links":{"self":[{"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/posts\/25455"}],"collection":[{"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/comments?post=25455"}],"version-history":[{"count":0,"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/posts\/25455\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/media?parent=25455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/categories?post=25455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/tags?post=25455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}