翻译纯属个人学习所用,原文版权属于原作者Vadim。原文链接
---------------------------
There are many questions how InnoDB allocates memory. I’ll try to give some explanation about the memory allocation at startup.
对于IonoDB的内存分配,有人存在不少疑问。我下面会试着解释一下innodb内存分配的入门知识。
Some important constants:
一些重要的常量:
NBLOCKS=count of block in innodb_buffer_pool = innodb_buffer_pool_size / 16384
NBLOCKS = innodb_buffer_pool的分块数 = innodb_buffer_pool_size / 16384
OS_THREADS= if (innodb_buffer_pool_size >= 1000Mb) = 50000
else if (innodb_buffer_pool_size >= 8Mb) = 10000
else = 1000 (it’s true for *nixes, for Windows there is a bit another calculation for OS_THREADS)
So InnoDB uses:(详细的参数意义可以查看MySQL手册)
- innodb_buffer_pool_size(原文为innodb_buffer_pool,疑为笔误) 指定InnoDB索引和数据的缓存池大小,如果是数据库专用服务器,推荐设置为物理内存的70%-80%
- innodb_additional_mem_pool_size 指定InnoDB用来存储数据字典和其他内部数据结构的内存池大小
- innodb_log_buffer_size 推荐设置为4M。大的日志缓冲允许事务运行时不需要将日志保存入磁盘而只到事务被提交,如果有大的事务处理,可以再稍微增加一点儿,一般不超过8M
- adaptive index hash, size= innodb_buffer_pool_size / 64 (原文为innodb_buffer_pool,疑为笔误) 所谓adaptive index hash(自适应索引哈希)是指MySQL会分析应用对索引的访问模式,有选择性的自动建立内存中的哈希索引,提高索引访问的效率。但是MySQL在实现这一特性时并没有充分考虑到并发性能问题,导致MySQL在多核机器上面性能下降。涉及到复杂的数据结构和算法,我也不懂:(有时间再分析一下
- system dictionary hash, size = 6 * innodb_buffer_pool_size / 512 系统字典哈希,不知道做什么用的
- memory for sync_array, which is used for syncronization primitives, size = OS_THREADS * 152 太专业,不懂是同步什么信息的
- memory for os_events, which are also used for syncronization primitives, OS_THREADS * 216 太专业,不懂是同步什么信息的
- and memory for locking system, size=5 * 4 * NBLOCKS 系统出现锁时需要的内存
So the final formula for innodb:
所以,最后innodb使用内存的计算公式是:
innodb_buffer_pool_size + innodb_log_buffer_size + innodb_additional_mem_pool_size + 812 / 16384 * innodb_buffer_pool_size + OS_THREADS * 368
For simplicity we can use: 简单一点儿我们可以这样算:
812 / 16384 * innodb_buffer_pool_size ~~ innodb_buffer_pool_size / 20
and OS_THREADS*368 = 17.5MB if innodb_buffer_pool > 1000MB
= 3.5MB if > 8MB
For example if you have innodb_buffer_pool_size=1500M, innodb_additional_mem_pool_size = 20M, innodb_log_buffer_size = 8M, InnoDB will allocate = 1500M + 20M + 8M + 1500/20M + 17.5M = 1620.5M.
以我们的数据库为例,
innodb_buffer_pool_size = 1324M,(这个值设置的偏低,服务器内存为4G)
innodb_additional_mem_pool_size = 16M,innodb_log_buffer_size = 8M,
那么InnoDB使用的内存就是 1324M + 16M + 8M + 1324/20M + 17.5M = 1431.7M
Take the additional memory into account when you are planning memory usage for your server.
当你规划服务器内存使用时,记得把额外的于数据库无关的内存占用算上。