search
HomeDatabaseMysql Tutorialvm.swappiness=0在最新内核中可能会OOM,MySQL被意外kill

请使用RHEL/CentOS 6.4及更新版本内核的MySQL同志们注意,vm.swappiness = 0的默认行为修改了,如果继续设置vm.swappiness = 0,有可能导致系统内存溢出,从而导致MySQL被意外kill掉。 在之前的《LINUX上MYSQL优化三板斧》中,我们建议大家把 vm.swappiness

请使用RHEL/CentOS 6.4及更新版本内核的MySQL同志们注意,vm.swappiness = 0的默认行为修改了,如果继续设置vm.swappiness = 0,有可能导致系统内存溢出,从而导致MySQL被意外kill掉。

在之前的《LINUX上MYSQL优化三板斧》中,我们建议大家把 vm.swappiness = 0 设置好。来尽量避免MySQL的服务器内存被交换出去。这样Linux在把内存交换出去时更偏向于将cache页交换出去,而不是将inactive页交换出去。详细描述请参考:http://www.woqutech.com/?p=1200。

经常有人会问, vm.swappiness = 0会不会导致Linux在有swap空间的时候也不交换出去,从而导致内存溢出(OOM)。参照《LINUX上MYSQL优化三板斧》介绍,我们知道,这个值只是一个Linux在判断是否交换内存(swap)的一个“倾向”参考值,而并不是说,设置为0以后,Linux就完全不会使用内存交换空间。

但是,在较新的内核中(2.6.32-303.el6及以后),vm.swappiness = 0 的默认行为修改掉了,这个说法不再成立。设置该参数vm.swappiess=0,有可能导致MySQL数据库所在的系统出现内存溢出。一般来说,MySQL数据库占用的内存是整个服务器中最大的,根据Linux的策略,它会会首先把MySQL给Kill掉(调整?/proc/(pidof -s mysqld)/oom_adj可以改变OOM时kill的优先级),从而导致应用故障等。

这个修改是在内核3.5-rc1中提交的,并且合并到了2.6.32-303.el6及之后的各个版本。先让我们来看看这个patch:

http://gitorious.ti.com/ti-linux-kernel/ti-linux-kernel/commit/fe35004fbf9eaf67482b074a2e032abb9c89b1dd?format=patch

From fe35004fbf9eaf67482b074a2e032abb9c89b1dd Mon Sep 17 00:00:00 2001

From: Satoru Moriya

Date: Tue, 29 May 2012 15:06:47 -0700

Subject: [PATCH] mm: avoid swapping out with swappiness==0

Sometimes we'd like to avoid swapping out anonymous memory. ?In

particular, avoid swapping out pages of important process or process

groups while there is a reasonable amount of pagecache on RAM so that we

can satisfy our customers' requirements.

OTOH, we can control how aggressive the kernel will swap memory pages with

/proc/sys/vm/swappiness for global and

/sys/fs/cgroup/memory/memory.swappiness for each memcg.

But with current reclaim implementation, the kernel may swap out even if

we set swappiness=0 and there is pagecache in RAM.

This patch changes the behavior with swappiness==0. ?If we set

swappiness==0, the kernel does not swap out completely (for global reclaim

until the amount of free pages and filebacked pages in a zone has been

reduced to something very very small (nr_free + nr_filebacked

watermark)).

Signed-off-by: Satoru Moriya

Acked-by: Minchan Kim

Reviewed-by: Rik van Riel

Acked-by: Jerome Marchand

Signed-off-by: Andrew Morton

Signed-off-by: Linus Torvalds

---

?mm/vmscan.c | ? ?6 +++---

?1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/vmscan.c b/mm/vmscan.c

index 67a4fd4..ee97530 100644

--- a/mm/vmscan.c

+++ b/mm/vmscan.c

@@ -1761,10 +1761,10 @@ static void get_scan_count(struct mem_cgroup_zone *mz, struct scan_control *sc,

?? * proportional to the fraction of recently scanned pages on

?? * each list that were recently referenced and in active use.

?? */

-?? ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1);

+?? ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);

?? ap /= reclaim_stat->recent_rotated[0] + 1;

-?? fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1);

+?? fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);

?? fp /= reclaim_stat->recent_rotated[1] + 1;

?? spin_unlock_irq(&mz->zone->lru_lock);

@@ -1777,7 +1777,7 @@ out:

????? unsigned long scan;

????? scan = zone_nr_lru_pages(mz, lru);

-????? if (priority || noswap) {

+????? if (priority || noswap || !vmscan_swappiness(mz, sc)) {

????????? scan >>= priority;

????????? if (!scan && force_scan)

???????????? scan = SWAP_CLUSTER_MAX;

--?

1.7.5

就像Satoru Moriya所说的那样,在之前的版本中,就算我们设置了swappiness=0并且RAM中还有pagecache,内核也可能会交换出部分匿名内存页。而为了“满足用户的需求”,这个patch修改了swappiness=0的行为,如果你设置swappiness=0,那么只有在(nr_free + nr_filebacked

在2.6.32-303.el6 RHEL/CentOS及更新版本的内核中,该patch就已经被合并进来:

* Mon Aug 27 2012 Jarod Wilson [2.6.32-303.el6]

...

- [mm] avoid swapping out with swappiness==0 (Satoru Moriya) [787885]

其他分发版本的Linux(比如Debian,Ubuntu)的版本中,请各位自己查阅一下,看看时候已经合并该patch。

RHEL/CentOS 6.3的内核版本是2.6.32-279,而RHEL/CentOS 6.4的内核为2.6.32-358,从这个版本开始,swappiness的行为就已经修改了,使用这个版本及之后版本的同志们需要特别注意一下。

解决的办法其实也很简单,

1、尽量保证Linux操作系统还有足够的内存

2、最新的内核,建议把vm.swappiness设置1

3、考虑设置 /proc/(pidof -s mysqld)/oom_adj为较小的值来尽量避免MySQL由于内存不足而被关闭。

参考:

https://github.com/torvalds/linux/blob/master/mm/vmscan.c

http://gitorious.ti.com/ti-linux-kernel/ti-linux-kernel/commit/fe35004fbf9eaf67482b074a2e032abb9c89b1dd?format=patch

http://www.mysqlperformanceblog.com/2014/04/28/oom-relation-vm-swappiness0-new-kernel/

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何在Ubuntu 22.04上安装Linux 内核 详细教程!如何在Ubuntu 22.04上安装Linux 内核 详细教程!Mar 01, 2024 pm 10:34 PM

在Ubuntu22.04上安装Linux内核可以按照以下步骤进行操作:更新系统:首先,确保你的Ubuntu系统是最新的,执行以下命令更新系统软件包:sudoaptupdatesudoaptupgrade下载内核文件:访问Linux内核官方网站()下载所需的内核版本。选择一个稳定版本并下载源代码文件(以.tar.gz或.tar.xz为扩展名),例如:wget解压文件:使用以下命令解压下载的内核源代码文件:tar-xflinux-5.14.tar.xz安装构建依赖:安装构建内核所需的工具和依赖项。执

Linux修改内核(kernel)启动顺序Linux修改内核(kernel)启动顺序Feb 23, 2024 pm 10:22 PM

Linux修改内核(kernel)启动顺序一、RHEL6/CentOS6修改内核启动顺序查看/etc/grub.conf文件以确定系统内核情况。根据文件显示,系统存在两个内核版本,分别为2.6.32-573.18.1.el6.x86_64和2.6.32-431.23.3.el6.x86_64。内核版本从上至下列出。在grub.conf文件中,可以通过调整default参数来决定系统启动时使用哪个内核版本。默认值为0,表示系统将启动最新的内核版本。值为0对应着grub.conf文件中列出的第一个内

某团面试:如果线上遇到了OOM,你该如何排查?如何解决?哪些方案?某团面试:如果线上遇到了OOM,你该如何排查?如何解决?哪些方案?Aug 23, 2023 pm 02:34 PM

OOM 意味着程序存在着漏洞,可能是代码或者 JVM 参数配置引起的。这篇文章和读者聊聊,Java 进程触发了 OOM 后如何排查。

骁龙处理器排名最新榜单骁龙处理器排名最新榜单Mar 22, 2024 am 11:33 AM

骁龙处理器一直以其出色的性能和稳定的表现赢得了广泛的认可,是手机和平板电脑等移动设备的首选处理器之一。随着技术的不断发展,骁龙处理器也在不断更新换代,每一代新品都会引起广泛关注。近日,一家权威机构发布了最新的骁龙处理器排名榜单,让人们对各款处理器的性能有了更清晰的了解。首先,根据榜单显示,骁龙8系列处理器一直是性能最强劲的处理器之一,在市场上备受追捧。最新发

面试官:如果线上遇到了OOM,该如何解决?面试官:如果线上遇到了OOM,该如何解决?Aug 17, 2023 pm 04:38 PM

​OOM 可以说是我们开发者最怕的问题之一,导致的原因基本上都是是代码或者 JVM 参数配置引起的。这篇文章和读者聊聊,Java 进程触发了 OOM 后如何排查。

Linux内核主函数解析与分析Linux内核主函数解析与分析Mar 14, 2024 am 11:27 AM

Linux内核主函数解析与分析Linux内核是一个庞大而复杂的系统,其中的主函数起着至关重要的作用,它是整个系统的入口点,负责初始化各种子系统、驱动程序和内核模块,最终启动整个操作系统。本文将针对Linux内核主函数进行解析与分析,通过具体的代码示例来展示其关键功能和执行流程。在Linux内核中,主函数的入口点位于init/main.c文件中的start_k

安卓系统究竟是不是基于Linux内核?安卓系统究竟是不是基于Linux内核?Mar 14, 2024 pm 03:12 PM

安卓系统究竟是不是基于Linux内核?安卓系统作为目前全球使用最广泛的移动操作系统之一,一直以来都被称为基于Linux内核开发的。然而,真正的情况究竟如何呢?我们来探讨一下这个问题。首先,让我们了解一下Linux内核。Linux内核作为一个开源的操作系统内核,是由LinusTorvalds于1991年首次发布的。它为许多操作系统提供了良好的基础,包括And

探究 Linux 内核底层所使用的编程语言探究 Linux 内核底层所使用的编程语言Mar 20, 2024 am 08:06 AM

标题:探究Linux内核底层所使用的编程语言Linux内核作为一个开源、稳定、可靠的操作系统内核,在计算机领域具有广泛的应用。而要深入了解Linux内核,就不得不涉及其底层所使用的编程语言。事实上,Linux内核主要是用C语言编写的,这是一个高效、灵活且易于维护的编程语言,非常适合用于操作系统的开发。本文将从细节的角度探究Linux内核底

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),