服务器上的服务器访问异常,查看/va/log/messages发现如下:

Sep 22 16:08:21 safeserver kernel: java invoked oom-killer: gfp_mask=0x280da, order=0, oom_adj=0, oom_score_adj=0
Sep 22 16:08:21 safeserver kernel: java cpuset=/ mems_allowed=0
Sep 22 16:08:21 safeserver kernel: Pid: 14859, comm: java Not tainted 2.6.32-754.30.2.el6.x86_64 #1

OOM Killer机制是怎样?又如何设置防止此种情况发生?Linux内存如何排查?

首先看内存:
$ free
                                  total           used                 free    shared    buffers    cached
Mem:                         4040360    4012200       28160         0     176628   3571348
-/+ buffers/cache:                        264224     3776136
Swap:                         0                         0                 0

注意要看红色的部分,上面的哪个free 28160不是真正的free,有如下说明:
In this example the total amount of available memory is 4040360 KB. 264224 KB are used by processes and 3776136 KB are free for other applications. Do not get confused by the first line which shows that 28160KB are free! If you look at the usage figures you can see that most of the memory use is for buffers and cache. Linux always tries to use RAM to speed up disk operations by using available memory for buffers (file system metadata) and cache (pages with actual contents of files or block devices). This helps the system to run faster because disk information is already in memory which saves I/O operations. If space is needed by programs or applications like Oracle, then Linux will free up the buffers and cache to yield memory for the applications. If your system runs for a while you will usually see a small number under the field "free" on the first line.
--from redhat

发现服务器没有设置Swap导致OOM killer频繁发生。

那又如何查看swap设置呢?

检查是否启用swap:
cat /proc/swaps
grep Swap /proc/meminfo
swapon -s
free -m
vmstat

Swap到底该设置多大呢?

https://access.redhat.com/solutions/15244

redhat 6,7一般推荐和内存一致(4~8G),具体参考上面链接。

启用swap:

swap:可以用逻辑卷或者文件方式。下面是采用文件方式。

[root@safedemo bin]# dd if=/dev/zero of=/swapfile bs=1G count=4
4+0 records in
4+0 records out
4294967296 bytes (4.3 GB) copied, 37.4051 s, 115 MB/s
[root@safedemo bin]# chmod 600 /swapfile
[root@safedemo bin]# mkswap /swapfile
mkswap: /swapfile: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=96e8b638-b36c-4660-8667-5654a92dc520
[root@safedemo bin]# swapon /swapfile
[root@safedemo bin]# vi /etc/fstab
/swapfile    swap    swap   defaults 0 0

做了一个例子来重现OOM killer

import java.util.Scanner;

public class OOMTest {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
java.util.List<int[]> l = new java.util.ArrayList(); try {
for (int i = 0; i < 1000; i++) {
System.out.println("Please press any text to allocate ~100M memory:");
String input = scanner.nextLine();
System.out.println("new memory(~100M)");
l.add(new int[26107200]);
}
} catch (Throwable t) {
t.printStackTrace();
}
} }

运行:
[root@safedemo bin]# java -Xmx2g OOMTest
Picked up JAVA_TOOL_OPTIONS: -Dhttps.protocols=TLSv1.2
Please press any text to allocate ~100M memory:

new memory(~100M)
Please press any text to allocate ~100M memory:

new memory(~100M)
Please press any text to allocate ~100M memory:

new memory(~100M)
Please press any text to allocate ~100M memory:

new memory(~100M)
Please press any text to allocate ~100M memory:

new memory(~100M)
Please press any text to allocate ~100M memory:

new memory(~100M)
Please press any text to allocate ~100M memory:

new memory(~100M)
Killed <-它自己触发系统oom killer,结果把自己杀死了。

//check /var/log/messages.
Sep 22 16:08:21 safeserver kernel: java invoked oom-killer: gfp_mask=0x280da, order=0, oom_adj=0, oom_score_adj=0
Sep 22 16:08:21 safeserver kernel: java cpuset=/ mems_allowed=0
Sep 22 16:08:21 safeserver kernel: Pid: 14859, comm: java Not tainted 2.6.32-754.30.2.el6.x86_64 #1
//14859就是引发oom killer的进程(上面的OOMTest)
....
Sep 22 16:08:21 safeserver kernel: Out of memory: Kill process 14857 (java) score 142 or sacrifice child
Sep 22 16:08:21 safeserver kernel: Killed process 14857, UID 0, (java) total-vm:3191104kB, anon-rss:676096kB, file-rss:68kB

OOM能不能禁用?
//Disable OOM killer  in redhat
Red Hat Enteprise Linux 5, 6 and 7 do not have the ability to completely disable OOM-KILLER. Please see the following section for tuning OOM-KILLER operation within RHEL 5, RHEL 6 and RHEL 7.

答案是不完全能够禁用。

可以通过调整某个进程的score来避免oom killer
There is also a special value of -17, which disables oom_killer for that process. In the example below, oom_score returns a value of O,indicating that this process would not be killed.
Raw

    # cat /proc/12465/oom_score
    78           
    # echo -17 > /proc/12465/oom_adj           
    # cat /proc/12465/oom_score
    0

也可以通过调整overcommit_memory来调整

,如果设置为2,内存不够时会报错,达到间接控制oom killer的目的(官方文档提到某些情况下也会trigger oom killer)
The /etc/sysctl.conf file consists
vm.overcommit_memory = 2
vm.overcommit_ratio = 100

over

Linux OOM Killer造成数据库访问异常排查的更多相关文章

  1. Linux OOM killer 与相关参数详解

    一.前言 本文是描述Linux virtual memory运行参数的第二篇,主要是讲OOM相关的参数的.为了理解OOM参数,第二章简单的描述什么是OOM.如果这个名词对你毫无压力,你可以直接进入第三 ...

  2. 嵌入式开发之内核内存异常排查---关闭oom killer

    通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解.uptimedmesg | tailvmstat 1mpstat -P ALL 1pidstat 1iostat -xz 1free ...

  3. 【原创】访问Linux进程文件表导致系统异常复位的排查记录

    前提知识: Linux内核.Linux 进程和文件数据结构.vmcore解析.汇编语言 问题背景: 这个问题出自项目的一个安全模块,主要功能是确定某进程是否有权限访问其正在访问的文件. 实现功能时,需 ...

  4. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #16 OOM Killer的运行与结构

    HACK #16 OOM Killer的运行与结构(1) 本节介绍OOM Killer的运行与结构. Linux中的Out Of Memory(OOM) Killer功能作为确保内存的最终手段,可以在 ...

  5. [C#]记录一次异常排查,关于using语法、sqlserver数据库session、DBHelper类

    最近在做一个基于asp.net和sqlserver的网站项目,发现网站运行一段时间之后,会报异常: 超时时间已到,但是尚未从池中获取连接.出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小 ...

  6. 理解和配置 Linux 下的 OOM Killer

    原文:http://www.vpsee.com/2013/10/how-to-configure-the-linux-oom-killer/ 最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有 ...

  7. .net单元测试——常用测试方式(异常模拟、返回值测试、参数测试、数据库访问代码测试)

    最近在看.net单元测试艺术,我也喜欢单元测试,今天介绍一下如何测试异常.如何测试返回值.如何测试模拟对象的参数传递.如何测试数据库访问代码.单元测试框架使用的是NUnit,模拟框架使用的是:Rhin ...

  8. Linux系统OOM killer机制详解

    介绍: Linux下面有个特性叫OOM killer(Out Of Memory killer),会在系统内存耗尽的情况下出现,选择性的干掉一些进程以求释放一些内存.广大从事Linux方面的IT农民工 ...

  9. Linux进程被杀掉(OOM killer),查看系统日志

    基本概念: Linux 内核有个机制叫OOM killer(Out Of Memory killer),该机制会监控那些占用内存过大,尤其是瞬间占用内存很快的进程,然后防止内存耗尽而自动把该进程杀掉. ...

随机推荐

  1. kali linux与虚拟机Vmware安装vmware tools(主机与虚拟机的文件拖拽)

    一.打开虚拟机任务栏"虚拟机"-----点击安装Vmware tools 二.回到开启的kali linux系统中,找到vmware tools CD文件夹,拖拽出文件中的压缩文件 ...

  2. css3系列之过渡transition

    transition 设置变换属性的过渡效果,举个例子,也就是说, 如果从width100px 变成 width200px,要以什么方式进行变换,速度,时间,类型等. transition trans ...

  3. JUC并发工具包之CountDownLatch

    1.介绍 本文将介绍CountDownLatch并给出实践中的几个例子,通过使用CountDownLatch我们可以让一个线程阻塞直到其他一个或多个线程执行完成. A synchronization ...

  4. 在Jenkins的帮助下让我们的应用CI与CD

    上图三位大家应该很熟悉吧,借助这三者可以让我们的服务在Linux环境下持续集成.容器中持续部署. 本篇博客的项目是core webapi, .NET 5.0 在11号已经正式发布了,你们的项目都升级了 ...

  5. Idea中如何导入jar包

    1.首先在idea左上角找到" File ",然后找到 "Project structure" 2.接着选择 " java ",选择后接着会 ...

  6. LeetCode周赛#204 题解

    1566. 重复至少 K 次且长度为 M 的模式 #模拟 题目链接 题意 给定正整数数组 arr,请你找出一个长度为 m 且在数组中至少重复 k 次的模式. 模式 是由一个或多个值组成的子数组(连续的 ...

  7. Java基础教程——正则表达式

    正则表达式·Regular Expression 正则表达式就是一个用于匹配字符串的模板,正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别. 主要用到的对象: java.util.rege ...

  8. 我与PHP和git不得不说的故事(梦开始的地方,从入门到放弃记录第一章)

    ·关于下载 阿瑶瑶跟wampsever官网搏斗了一下午,其实我觉得教材可能在PUA我.谷歌说它给的网址安全证书过期,然后下载以断网收场.(阿瑶的第一战,以失败告终) [经过我玲姐指点,下载路径变为迅雷 ...

  9. 音视频处理基础知识扫盲:数字视频YUV像素表示法以及视频帧和编解码概念介绍

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...

  10. PyQt(Python+Qt)学习随笔:QTreeWidget中给树型部件增加顶层项的方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeWidget对象创建后,是没有任何项的,要给部件增加项,首先要增加顶层项.顶层项的增加有三 ...