From: MARK CALLAGHAN Date: October 17 2008 4:43pm Subject: memory allocation architecture in MySQL List-Archive: http://lists.mysql.com/internals/36022 Message-Id: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Is the memory management model documented for MySQL? I want to know what APIs exist to allocate memory in the context of a user connection. Are there heaps for global, connection duration and statement duration allocation? And if there are, how are they accessed? I am curious because of a bug in MySQL 5.0 -- http://bugs.mysql.com/bug.php?id=40097. A per-THD variable references stack-allocated memory used by a String instance. I found this, but it doesn't describe what/where MEM_ROOT instances exist in the context of a connection (THD). http://forge.mysql.com/wiki/MySQL_memory_handling_and_memory_handling_in_Falcon This states there is a connection duration memory heap -- use sql_alloc() http://forge.mysql.com/wiki/MysysLibraryAndAlgorithms There appears to be a lot of allocation that does not use the per-connection MEM_ROOT. String (from sql/sql_string.{cc,h}) uses my_malloc and my_realloc to get memory. my_malloc and my_realloc don't use per-THD heaps like sql_alloc() and get memory directly from malloc(). Is there a goal to make all allocation done in the context of a user query allocated via the per-connection MEM_ROOT? It seems like a bad idea to allow memory references between objects from different allocation arenas -- things allocated from malloc() referencing things allocated from sql_alloc() and vice-versa. Finally, String provides 'operator new' that uses MEM_ROOT to allocate the String object from that MEM_ROOT. But if that String instance ever needs to reallocate memory for the contained string, it will get it from malloc() and not use the MEM_ROOT. Is there an unstated assumption that all uses of this won't need to allocate more memory? -- Mark Callaghan mdcallag@stripped