MySQL Server Memory Usage general understanding and code

AID: 2112
  • Status: Published

3390 points

  • BytheGhost_k8
  • TypeGeneral
  • Posted on2009-12-16 at 04:47:58
Awards
  • Community Pick
A lot has been said about MySQL server memory usage and methods for calculating or estimating the same.

While going through the MySQL manual and the MySQL Server Memory Usage documents, I noted the following quick points regarding how MySQL memory uses can be computed.

This post will summarise my understanding and, at the end, will provide a code snippet for the same memory usage calculations.

We have global buffers which are allocated irrespective of connections as and when MySQL server is started. Along with that MySQL server allocates memory to each thread to perform respective tasks.

So the formula goes:

MySQL Server Memory Usage = Sum of Global Buffers + (number of Connection * Per thread memory variables).

Global buffers include:

  • key_buffer_size: key_buffer_size is the size of the buffer used for index blocks.

  • innodb_buffer_pool_size: The size in bytes of the memory buffer InnoDB uses to cache data and indexes of its tables.

  • innodb_additional_mem_pool_size: The size in bytes of a memory pool InnoDB uses to store data dictionary information and other internal data structures.

  • innodb_log_buffer_size: The size in bytes of the buffer that InnoDB uses to write to the log files on disk.

  • query_cache_size: The amount of memory allocated for caching query results.




Per thread variables include:

  • read_buffer_size: Buffer memory used for sequential table scan.

  • read_rnd_buffer_size: Memory used for random read buffer / sorting.

  • sort_buffer_size: Memory allocated for sorting, Group By, Order By.

  • join_buffer_size: The size of the buffer that is used for plain index scans, range index scans, and joins that do not use indexes and thus perform full table scans.

  • thread_stack: The stack size for each thread.

  • net_buffer_length: Connection buffer

  • max_allowed_packet: Up to this size net_buffer_length can grow.

  • tmp_table_size - temporary / hash tables in MySQL



Note that,

If size increases or if table have blob columns, instead of heap tables on-disk tables created.

Memories for variables read_buffer_size, sort_buffer_size, read_rnd_buffer_size, tmp_table_size are allocated as & when required. They are also de-allocated once the task is accomplished.

Of course this barely gives us an idea regarding the MySQL Server memory usage, but hopefully it gave you the basics of the concept!

This article is also available on my blog for further change or updation.

Note: For MySQL 5.1.* it will throw error 1064.
Consider a bug report. associated with the code.

Solution for MySQL 5.1+:
Changing the cursor declaration from
DECLARE CUR_GBLVAR CURSOR FOR SHOW GLOBAL VARIABLES;
to
DECLARE CUR_GBLVAR CURSOR FOR SELECT * FROM information_schema.GLOBAL_VARIABLES;
will make stored procedure work fine.

Below is a stored procedure to calculate the same estimates of memory consumption using Global Variables.
DELIMITER $$
 
DROP PROCEDURE IF EXISTS `my_memory` $$
CREATE PROCEDURE `my_memory` ()
BEGIN
 
DECLARE var VARCHAR(100);
DECLARE val VARCHAR(100);
DECLARE done INT;
#Variables for storing calculations
DECLARE GLOBAL_SUM DOUBLE;
DECLARE PER_THREAD_SUM DOUBLE;
DECLARE MAX_CONN DOUBLE;
DECLARE HEAP_TABLE DOUBLE;
DECLARE TEMP_TABLE DOUBLE;
 
#Cusor for Global Variables
DECLARE CUR_GBLVAR CURSOR FOR SHOW GLOBAL VARIABLES;

#If you're using mysql 5.1+ consider using below statement.
#comment above cursor declaration and uncomment following line.
#For more information refer: http://bugs.mysql.com/bug.php?id=49758
#DECLARE CUR_GBLVAR CURSOR FOR SELECT * FROM information_schema.GLOBAL_VARIABLES;


DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
 
SET GLOBAL_SUM=0;
SET PER_THREAD_SUM=0;
SET MAX_CONN=0;
SET HEAP_TABLE=0;
SET TEMP_TABLE=0;
 
OPEN CUR_GBLVAR;
 
mylp:LOOP
      FETCH CUR_GBLVAR INTO var,val;
  IF done=1 THEN
    LEAVE mylp;
  END IF;
    IF var in ('key_buffer_size','innodb_buffer_pool_size','innodb_additional_mem_pool_size','innodb_log_buffer_size','query_cache_size') THEN
    #Summing Up Global Memory Usage
      SET GLOBAL_SUM=GLOBAL_SUM+val;
    ELSEIF var in ('read_buffer_size','read_rnd_buffer_size','sort_buffer_size','join_buffer_size','thread_stack','max_allowed_packet','net_buffer_length') THEN
    #Summing Up Per Thread Memory Variables
      SET PER_THREAD_SUM=PER_THREAD_SUM+val;
    ELSEIF var in ('max_connections') THEN
    #Maximum allowed connections
      SET MAX_CONN=val;
    ELSEIF var in ('max_heap_table_size') THEN
    #Size of Max Heap tables created
      SET HEAP_TABLE=val;
    #Size of possible Temporary Table = Maximum of tmp_table_size / max_heap_table_size.
    ELSEIF var in ('tmp_table_size','max_heap_table_size') THEN
      SET TEMP_TABLE=if((TEMP_TABLE>val),TEMP_TABLE,val);
    END IF;
 
END LOOP;
CLOSE CUR_GBLVAR;
#Summerizing:
select "Global Buffers" as "Parameter",CONCAT(GLOBAL_SUM/(1024*1024),' M') as "Value" union
select "Per Thread",CONCAT(PER_THREAD_SUM/(1024*1024),' M')  union
select "Maximum Connections",MAX_CONN union
select "Total Memory Usage",CONCAT((GLOBAL_SUM + (MAX_CONN * PER_THREAD_SUM))/(1024*1024),' M') union
select "+ Per Heap Table",CONCAT(HEAP_TABLE / (1024*1024),' M') union
select "+ Per Temp Table",CONCAT(TEMP_TABLE / (1024*1024),' M') ;
 
END $$
DELIMITER ;

                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:

Select allOpen in new window

#Sample output:
mysql> call my_memory();
+---------------------+-------------------+
| Parameter           | Value             |
+---------------------+-------------------+
| Global Buffers      | 17.99609375 M     |
| Per Thread          | 3.7031173706055 M |
| Maximum Connections | 100               |
| Total Memory Usage  | 388.30783081055 M |
| + Per Heap Table    | 16 M              |
| + Per Temp Table    | 32 M              |
+---------------------+-------------------+

                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window

Asked On
2009-12-16 at 04:47:58ID2112
Tags

Mysql server

,

memory usage

Topic

MySQL Server

Views
2216

Comments

Expert Comment

by: mwvisa1 on 2009-12-17 at 10:22:05ID: 6887

Nice article, theGhost_k8.
Voted yes above!

Expert Comment

by: BToTheAToTheBABA on 2010-10-07 at 08:07:45ID: 20315

Hi,

 What does the output to call my_memory() implies ?

Or

 How to interpret the Results ?


ForEx)
This is my output. Is this Normal Or Fine ??

+---------------------+--------------+
| Parameter           | Value        |
+---------------------+--------------+
| Global Buffers      | 639 M        |
| Per Thread          | 701.265625 M |
| Maximum Connections | 100          |
| Total Memory Usage  | 70765.5625 M |
| + Per Heap Table    | 500 M        |
| + Per Temp Table    | 500 M        |
+---------------------+--------------+

                                        
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

Author Comment

by: theGhost_k8 on 2010-10-09 at 00:04:05ID: 20346

As said the stored procedure I wrote to estimate memory usage of mysql... Mean while yours are high values!!

Expert Comment

by: BToTheAToTheBABA on 2010-10-09 at 00:49:47ID: 20347


Is there any RECOMMENDED rule or Logic like ?????

for 1GB RAM
- Global Buffers Should Not be Lower Than 700MB
- Total Memory Usage Should Never exceed 1GB

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top MySQL Server Experts

  1. johanntagle

    286,814

    Guru

    6,000 points yesterday

    Profile
    Rank: Sage
  2. Ray_Paseur

    216,557

    Guru

    0 points yesterday

    Profile
    Rank: Savant
  3. DaveBaldwin

    119,595

    Master

    1,400 points yesterday

    Profile
    Rank: Genius
  4. angelIII

    61,340

    Master

    0 points yesterday

    Profile
    Rank: Elite
  5. mwvisa1

    57,185

    Master

    30 points yesterday

    Profile
    Rank: Genius
  6. HainKurt

    41,850

    0 points yesterday

    Profile
    Rank: Genius
  7. ralmada

    39,250

    0 points yesterday

    Profile
    Rank: Genius
  8. Roads_Roads

    33,080

    0 points yesterday

    Profile
    Rank: Genius
  9. arnold

    29,812

    0 points yesterday

    Profile
    Rank: Genius
  10. theGhost_k8

    29,785

    0 points yesterday

    Profile
    Rank: Sage
  11. Kdo

    29,682

    0 points yesterday

    Profile
    Rank: Genius
  12. bportlock

    26,604

    0 points yesterday

    Profile
    Rank: Genius
  13. jason1178

    23,574

    0 points yesterday

    Profile
    Rank: Genius
  14. maeltar

    23,236

    0 points yesterday

    Profile
    Rank: Guru
  15. StingRaY

    21,500

    0 points yesterday

    Profile
    Rank: Wizard
  16. smadeira

    19,968

    0 points yesterday

    Profile
    Rank: Wizard
  17. fundacionrts

    18,200

    0 points yesterday

    Profile
    Rank: Master
  18. gr8gonzo

    17,019

    0 points yesterday

    Profile
    Rank: Sage
  19. ChrisStanyon

    16,964

    0 points yesterday

    Profile
    Rank: Sage
  20. pratima_mcs

    16,614

    0 points yesterday

    Profile
    Rank: Genius
  21. TempDBA

    16,400

    0 points yesterday

    Profile
    Rank: Sage
  22. Sharath_123

    16,268

    0 points yesterday

    Profile
    Rank: Genius
  23. for_yan

    16,000

    0 points yesterday

    Profile
    Rank: Genius
  24. matthewspatrick

    15,800

    0 points yesterday

    Profile
    Rank: Savant
  25. AielloJ

    13,732

    0 points yesterday

    Profile
    Rank: Wizard

Hall Of Fame