如何减少inode和dentry的缓存以尽量避免OOM


=Start=

缘由:

在服务器上执行某个find操作时,导致内存占用升高,业务进程的内存占用也持续升高且恢复缓慢,初步排查看上去像是因为proc_inode_cache的占用升高导致。

正文:

参考解答:

通过学习 [Linux Used内存到底哪里去了? | http://blog.yufeng.info/archives/2456] 了解到「内存的去向主要有3个:1.进程消耗;2.slab消耗;3.pagetable消耗。」

struct page是系统boot的时候就会根据内存大小算出来分配出去的,18内核是1.56%左右,32内核由于cgroup的原因会在2.3%

查看slab使用情况的方法
$ slabtop
[OR]
$ cat /proc/slabinfo |awk '{print $1,$3*$4/1024,"KB"}' | sort -k2 -n | tail 

&

比较危险的做法
echo 1 > /proc/sys/vm/drop_caches  # free pagecache
     [OR]
echo 2 > /proc/sys/vm/drop_caches  # free dentries and inodes
     [OR]
echo 3 > /proc/sys/vm/drop_caches  # free pagecache, dentries and inodes
sync  # forces the dump to be destructive

&

温和一点的做法
echo 300 > /proc/sys/vm/vfs_cache_pressure  # Increasing vfs_cache_pressure beyond 100 causes the kernel to prefer to reclaim dentries and inodes
参考链接:

=END=

,

《“如何减少inode和dentry的缓存以尽量避免OOM”》 有 8 条评论

  1. Linux的审计程序auditd中的一段避免进程因为OOOM机制被杀死的代码(static void avoid_oom_killer(void){}):
    https://github.com/linux-audit/audit-userspace/blob/master/src/auditd.c#L324
    `
    static void avoid_oom_killer(void)
    {
    int oomfd, len, rc;
    char *score = NULL;

    /* New kernels use different technique */
    if ((oomfd = open(“/proc/self/oom_score_adj”, O_NOFOLLOW | O_WRONLY)) >= 0) {
    score = “-1000”;
    } else if ((oomfd = open(“/proc/self/oom_adj”, O_NOFOLLOW | O_WRONLY)) >= 0) {
    score = “-17”;
    } else {
    audit_msg(LOG_NOTICE, “Cannot open out of memory adjuster”);
    return;
    }

    len = strlen(score);
    rc = write(oomfd, score, len);
    if (rc != len)
    audit_msg(LOG_NOTICE, “Unable to adjust out of memory score”);
    close(oomfd);
    }
    `

  2. 理解和配置 Linux 下的 OOM Killer
    http://www.vpsee.com/2013/10/how-to-configure-the-linux-oom-killer/
    `
    dmesg
    grep -i “Kill process” /var/log/messages

    # vi oomscore.sh
    #!/bin/bash
    for proc in $(find /proc -maxdepth 1 -regex ‘/proc/[0-9]+’); do
    printf “%2d %5d %s\n” \
    “$(cat $proc/oom_score)” \
    “$(basename $proc)” \
    “$(cat $proc/cmdline | tr ‘\0’ ‘ ‘ | head -c 50)”
    done 2>/dev/null | sort -nr | head -n 10

    # chmod +x oomscore.sh
    # ./oomscore.sh
    `
    Linux 的 OOM 终结者
    https://linux.cn/article-5824-1.html

  3. CentOS下如何检查OOM消息
    https://stackoverflow.com/questions/624857/finding-which-process-was-killed-by-linux-oom-killer
    https://unix.stackexchange.com/questions/128642/debug-out-of-memory-with-var-log-messages
    `
    grep -i ‘killed process’ /var/log/messages
    dmesg | egrep -i ‘killed process’

    grep oom /var/log/*
    grep total_vm /var/log/*

    dstat –top-oom #显示可能会因为OOM被kill掉的第一个进程
    `

    CentOS 7上的OOM日志放在哪?
    https://www.reddit.com/r/linuxadmin/comments/3zun74/whered_the_oom_logs_go_in_centos7_systemd/
    https://unix.stackexchange.com/questions/128642/debug-out-of-memory-with-var-log-messages/

回复 a-z 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注