MySQL内存管理,内存分配器和操作系统的示例分析

发布时间:2021-01-08 14:06:39

来源:亿速云

阅读:79

作者:小新

这篇文章主要介绍MySQL内存管理,内存分配器和操作系统的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

当用户使用任何软件(包括MySQL)碰到内存问题时,我们第一反应就是内存泄漏。正如这篇文章所示,其实并不总是这样。

这篇文章阐述一个关于内存的bug。

All Percona Support customers are eligible for bug fixes, but their options vary. For example, Advanced+ customers are offered a HotFix build prior to the public release of software with the patch. Premium customers do not even have to use Percona software: we may port our patches to upstream for them. But for Percona products all Support levels have the right to have a fix.

所有percona所支持的客户都有获得bug修复的资格,但他们也有不同的选择。比如,vip客户在软件补丁正式发布之前就可以获得hotfiix版本,高级客户甚至不需要使用percona的软件,我们也可以为他们把补丁推到上游。但对于与percona产品来说,所有支持等级都有权得到bug修复。

Even so, this does not mean we will fix every unexpected behavior, even if we accept that behavior to be a valid bug. One of the reasons for such a decision might be that while the behavior is clearly wrong for Percona products, this is still a feature request.

即便如此,这并不意味着我们会修复所有的意外情况,即使我们接受这种情况为一个有效bug。做出这样的决定的原因之一可能是这个意外情况虽然很明确是错误的,但对于percona产品本身来说确实一个产品需求

作为学习案例的一个bug

A good recent example of such a case is PS-5312 – the bug is repeatable with upstream and reported at bugs.mysql.com/95065

最近一个很好的案例是 PS-5312——这个bug可在上游复现并被记录在bugs.mysql.com/95065。

This reports a situation whereby access to InnoDB fulltext indexes leads to growth in memory usage. It starts when someone queries a fulltext index, grows until a maximum, and is not freed for quite a long time.

这个报告阐述了一种情况,当访问InnoDB的全文索引的时候会导致内存使用量增长。这种情况出现在一些全文索引的查询,内存会持续增长直到达到最大值,并且很长时间不会释放。

Yura Sorokin from the Percona Engineering Team investigated if this is a memory leak and found that it is not.

来自Percona工程团队的Yura Sorokin研究表明,这种情况并不属于内存泄漏范畴。

When InnoDB resolves a fulltext query, it creates a memory heap in the function fts_query_phrase_search This heap may grow up to 80MB. Additionally, it has a big number of blocks ( mem_block_t ) which are not always used continuously and this, in turn, leads to memory fragmentation.

当InnoDB解析一个全文查询时,它会在fts_query_phrase_search函数中创建一个内存堆,这个堆可能增长到80M。另外,这个过程还会使用到大量非连续块(mem_block_t)进而产生的内存碎片。

In the function exit , the memory heap is freed. InnoDB does this for each of the allocated blocks. At the end of the function, it calls free() which belongs to one of the memory allocator libraries, such as malloc or jemalloc. From the mysqld point of view, everything is done correctly: there is no memory leak.

在函数出口,这些内存堆会被释放。InnoDB会为其分配的每一个块做这个操作。在函数执行结束时,调用一个内存分配器库中的free()操作,比如malloc或者jemalloc。从MySQL本身来看,这都是没问题的,不存在内存泄漏。

However while free() should release memory when called, it is not required to return it back to the operating system. If the memory allocator decides that the same memory blocks will be required soon, it may still keep them for the mysqld process. This explains why you might see that mysqld still uses a lot of memory after the job is finished and all de-allocations are done.

然而,free()函数被调用时确实应该释放内存,但不需要将其返回给操作系统。如果内存分配器发现这些内存块马上还需要被用到,则会将他们保留住继续用于mysqld进程。这就解释了为什么mysqld在完成工作及释放内存都结束后还会占用大量内存。

This in practice is not a big issue and should not cause any harm. But if you need the memory to be returned to the operating system quicker, you could try alternative memory allocators, such as jemalloc. The latter was proven to solve the issue with PS-5312.

这个在实际生产中并不是一个大问题,按道理不应该造成任何事故。但是如果你需要更快地将内存返回给操作系统,你可以尝试非传统的内存分配器,类似jemallolc。它被证明可以解决PS-5312的问题。

Another factor which improves memory management is the number of CPU cores: the more we used for the test, the faster the memory was returned to the operating system. This, probably, can be explained by the fact that if you have multiple CPUs, then the memory allocator can dedicate one of them just for releasing memory to the operating system.

另一个改善内存管理的因素是cpu内核数量:在测试中,cpu核数越多,内存返回给操作系统的速度会越快。这可能是你拥有多个CPU,而其中一个可专门用作内存分配器释放内存给操作系统。

The very first implementation of InnoDB full text indexes introduced this flaw. As our engineer Yura Sorokin found:The very first 5.6 commit which introduces Full Text Search Functionality for InnoDB WL#5538: InnoDB Full-Text Search Support – https://dev.mysql.com/worklog/task/?id=5538

Implement WL #5538 InnoDB Full-Text Search Support, merge – https://github.com/mysql/mysql-server/commit/b6169e2d944 – also has this problem.

正如我们的工程师Yura Sorokin所发现的一样,下面两点阐述了InnoDB全文索引的早期实现引入了这个缺陷:5.6版本MySQL最早对InnoDB WL全文索引功能引入的介绍:#5538: InnoDB全文搜索支持 – https://dev.mysql.com/worklog/task/?id=5538

实现WL #5538 InnoDB全文搜索支持与合并 - https://github.com/mysql/mysql-server/commit/b6169e2d944 - 也存在同样的问题问题

修复方法

We have a few options to fix this:Change implementation of InnoDB fulltext index

Use custom memory library like jemalloc

Both have their advantages and disadvantages.

我们有两种方法来修复这个问题:

1.修改InnoDB全文索引的实现

2.使用自定义内存库,例如jemalloc

这两种方法都有各自的优缺点。

Option 1 means we are introducing an incompatibility with upstream, which may lead to strange bugs in future versions. This also means a full rewrite of the InnoDB fulltext code which is always risky in GA versions, used by our customers.

方法1 意味着我们引入了与软件上游不兼容性的风险,这可能会导致新版本中出现未知的错误。也意味着彻底重写InnoDB全文索引部分代码,这在用户们使用的GA版本中是有风险的。

Option 2 means we may hit flaws in the jemalloc library which is designed for performance and not for the safest memory allocation.

方法2 则意味着我们可能会命中一些jemalloc库中专门为性能设计但不是最安全的内存分配的bug。

So we have to choose between these two not ideal solutions.

Since option 1 may lead to a situation when Percona Server will be incompatible with upstream, we prefer option 2and look forward for the upstream fix of this bug.

因此我们不得不在这两个并不完美的方法中选择一个。

鉴于方法一可能导致percona服务与上游的不兼容,我们更倾向于用方法二来解决问题,并期待着上游修复这个bug。

结论

If you are seeing a high memory usage by the mysqld process, it is not always a symptom of a memory leak. You can use memory instrumentation in Performance Schema to find out how allocated memory is used. Try alternative memory libraries for better processing of allocations and freeing of memory. Search the user manual for LD_PRELOADto find out how to set it up at these pages here and here.

如果发现mysqld进程占用内存很高,并不代表一定是内存泄漏。我们可以在Performance Schema中使用内存检测来了解进程是如何使用已分配的内存。也可以尝试替换内存库来更好地处理内存分配与释放。关于LD_RELOAD如何配置,请查阅MySQL用户手册对应页面 mysqld-safe和using-system。

以上是“MySQL内存管理,内存分配器和操作系统的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

内存分配器 mysql_MySQL内存管理,内存分配器和操作系统的示例分析相关推荐

  1. php程序内存空间,php如何管理内存

    内存管理一般会包括以下内容: 是否有足够的内存供我们的程序使用: 如何从足够可用的内存中获取部分内存: 对于使用后的内存,是否可以将其销毁并将其重新分配给其它程序使用.(推荐学习:PHP编程从入门到精 ...

  2. [内存管理]内存池pool库

    pool库概述 如果之前学过操作系统的内存管理机制和内存分配算法等知识,那么就了解"内存池"的概念. 简单地说,内存池预先分配了一块大的内存空间,然后就可以在其中使用某种算法实现高 ...

  3. C语言(记录)——内存相关_2:内存的编址与管理

    本文是基于嵌入式的C语言 --------------------------------------------------------------------------------------- ...

  4. Linux内存管理slub分配器

    背景 Kernel版本:4.14 ARM64处理器,Contex-A53,双核 使用工具:Source Insight 3.5, Visio 1. 概述 之前的文章分析的都是基于页面的内存分配,而小块 ...

  5. 内存分配器 mysql_聊MySQL内存管理,内存分配器,操作系统

    推荐(免费):mysql视频教程 当用户在使用任何软件(包括MySQL)时遇到内存问题,我们的第一反应就是内存泄漏.正如本文所示,情况并非总是如此. 本文描述了一个关于内存的bug. 所有Percon ...

  6. [侯捷 C++内存管理] 标准分配器实现

    [侯捷 C++内存管理] 标准分配器实现 文章目录 [侯捷 C++内存管理] 标准分配器实现 VC6 标准分配器之实现 VC6 malloc() VC6 allocator BC5 标准库分配器之实现 ...

  7. linux内存分配器类型,内核早期内存分配器:memblock

    原标题:内核早期内存分配器:memblock 本文转载自Linux爱好者 本文来自 程雪涛的自荐投稿 Linux内核使用伙伴系统管理内存,那么在伙伴系统工作前,如何管理内存?答案是memblock. ...

  8. go 是常驻内存吗_图解 Go 内存分配器

    原标题:图解 Go 内存分配器 作者 | Ankur Anand 译者 | 闫亮 内存分配器一直是性能优化的重头戏,其结构复杂.内容抽象,涉及的数据结构繁多,相信很多人都曾被它搞疯了.本文将从内存的基 ...

  9. php从内存中获取源码_【PHP7源码分析】PHP内存管理

    作者: 顺风车运营研发团队 李乐 第一章 从操作系统内存管理说起 程序是代码和数据的集合,进程是运行着的程序:操作系统需要为进程分配内存:进程运行完毕需要释放内存:内存管理就是内存的分配和释放: 1. ...

最新文章

  1. Python 日期时间函数
  2. 持续集成工具Hudson安装实例
  3. RedHat6.2 x86手动配置LNMP环境
  4. 【华为_数通】常用命令备忘
  5. CDA Day1-3 Excel公式常用函数跟课学习
  6. 电子信息专业实习报告与总结
  7. 怎样在vue中使用jquery
  8. Uncaught TypeError: $(...).modal is not a function
  9. MacOS破解WiFi(WPA、WPA2)
  10. 给JAVA初学者的建议(转载治phphot的一个牛人给java初学者的建议)
  11. 2022全新玖五社区系统源码V9.8版
  12. 计算机绘图中级,《计算机绘图中级教程》1.doc
  13. 原生js提供的视频画中画api
  14. VC2008下使用OpenSSL 1.0.0g(免编译)
  15. 网络层协议和数据链路层协议
  16. Java memory stream 内存流
  17. 支付宝支付提示“提示系统繁忙,请稍后再试(ALI69)”
  18. (四)MySQL学习笔记——多表设计、多表查询、多表查询练习题
  19. 忽然厌倦了学画画怎么办
  20. 电脑被黑客远程入侵了,该怎么办啊

热门文章

  1. halcon学习 预处理
  2. OpenWrt IPv6配置
  3. [POI2010]CHO-Hamsters
  4. 应用程序无法正常启动(0x000007b),请单击确定关闭应用程序,已解决
  5. 服务器 网卡芯片,网卡芯片特写
  6. thinkpad x240s ubuntu下wifi断网bug解决方案
  7. 【转】 ManagementClass,ManagementObject 的使用
  8. Life of a Packet in Kubernetes - Calico网络进阶(注解版)
  9. 初探Netty:Netty原理、核心组件、数据容器以及运行机制
  10. 2022软考高项十大领域知识整理(一)--项目整体管理、范围管理