Categories: Contributing | Development | MySQLUniversity

MySQL memory handling and memory handling in Falcon

← Back to MySQL University main page

Contents

[edit] MySQL Memory Handling

[edit] Mechanisms

[edit] System Heap

When working with system heap allocation, please use MySQL convenience wrappers:

Implemented in mysys/my_malloc.c

 gptr my_malloc(unsigned int size, myf my_flags)

 void my_free(gptr ptr, myf my_flags);

#define MY_ZEROFILL     32      /* my_malloc(), fill array with zero */
#define MY_ALLOW_ZERO_PTR 64    /* my_realloc() ; zero ptr -> malloc */
#define MY_FREE_ON_ERROR 128    /* my_realloc() ; Free old ptr on error */
#define MY_HOLD_ON_ERROR 256    /* my_realloc() ; Return old ptr on error */
#define MY_FAE          8       /* Fatal if any error */
#define MY_WME          16      /* Write message on error */


These functions should always be used because:

Convenience macros:

Aslo available:

See the source code of mysys/my_malloc.c

Do not use plain 'malloc' and 'free', because they don't provide any of the essential features above.

Life time properties of system heap:

[edit] MEM_ROOT block allocator

Allocates memory in blocks of 4K (or other parameterizable size).

Life time properties of allocation:

In this case you must know that you won't need the memory after it's freed when you use these memory roots.

[edit] Query Cache allocator
[edit] Storage Engine mechanisms

Like QC allocator, are independent on the mechanisms employed by sql/ layer.

[edit] What do you do if you got a NULL pointer?

Image:Decision.jpg

[edit] Allocation centers

Allocation center is a pre-defined allocation area whose life time is controlled by the server runtime.

Allocation center life cycle: Image:Transition.jpg

Examples of allocation centers:

TABLE::record[0,1] and TABLE::field array.

in case of a correlated subqurery - many times per query).

Most important allocation centers:

[edit] How to choose the right allocation center

use this allocation center, since use will lead to memory hogging.

See also: Media:Query_arena_states.pdf



The Falcon memory manager

Falcon replaces the new operator with its own implementation, using pool-based memory allocator: MemMgr. The definitions of MemMgr are in MemMgr.h and the method code is in MemMgr.cpp.

I. Overview

    a.	Pool based
         i.	Record memory pool
         ii.	All other pool
    b.	Small block allocator
         i.	Blocking allocate
         ii.	Non-blocking release
              c.	Large block allocator
         i.	Blocking allocate
         ii.	Blocking release
    d.	Large vs. small policy set differently for different pools
         i.	General pool large is > 1024
         ii.	Record pool large is > 0

II. Memory block structure

    a.	Header
         i.	Common
              1.	pool
              2.	length (> 0 large, < 0 small)
              3.	for debug line and file
         ii.	Small block header
              1.	Common
              2.	Next block of same size
         iii.	Large block
              1.	Common
              2.	Prior and next in pool
              3.	Prior and next in size
              4.	Prior and next twins
    b.	Requested space
         i.	Address returned
         ii.	Rounded up to 16 byte boundary
    c.	Unused tail

III. Debug

    a.	Block initialization
         i.	‘C’ allocated
         ii.	‘E’ released
         iii.	‘D’ guard bytes
    b.	File and line of allocation

IV. Small block pool management

    a.	Not released or recombined
    b.	Maintained in lists of same sized blocks
    c.	Vector of list heads created a pool creation
    d.	Allocation
         i.	Seize pool small block  mutex
         ii.	If  existing block, grab block at top of list
         iii.	Else if space in reserve hunk, allocate block from hunk
         iv.	Else allocate new hunk with malloc
         v.	Release pool mutex
    e.	Release – Interlocked compare / swap of list head

V. Large block management

    a.	Block links
         i.	Physical next and prior
         ii.	Free next and prior by size
         iii.	Free next and prior of same size (twins)
    b.	Large block allocation
         i.	Seize pool large block mutex
         ii.	Walk ‘size’ list 
              1.	If a large enough free block exists
                   a.	Remove it from the size list
                   b.	If the tail is smaller than a large block keep it
                   c.	Else place tail in the size list, fix next next and prior
              2.	Allocate new hunk to pool and use it as above
         iii.	Release large block mutex
    c.	Large block release
         i.	Seize large block mutex
         ii.	If next physical block is free, combine with it
         iii.	If  prior physical block is free, combine with it
         iv.	If all blocks are combined, release pool
         v.	Link into size & twin lists
         vi.	Release large block mutex

VI. Straight malloc

    a.	Allocating hunks to pools
    b.	Memory allocation analysis

[edit] Questions & Answers

Retrieved from "http://forge.mysql.com/wiki/MySQL_memory_handling_and_memory_handling_in_Falcon"

This page has been accessed 1,325 times. This page was last modified 13:42, 21 September 2007.

Find

Browse
MySQLForge
Main Page
Current events
Recent changes
Random page
Help
Edit
Edit this page
Editing help
This page
Discuss this page
Post a comment
Printable version
Context
Page history
What links here
Related changes
My pages
Special pages
New pages
File list
Statistics
Bug reports
More...