【问题标题】:Limiting processes by memory under LinuxLinux下通过内存限制进程
【发布时间】:2013-11-28 16:04:04
【问题描述】:

我们必须在 Linux 系统上启动几个饥饿的进程。这些进程通常需要几个 Go (~5Go) 内存才能运行(总内存:16Go RAM + 2Go swap)。

  • 起初,当系统内存不足时,OOM-killer 会杀死进程,每次发生这种情况时我们都必须重新启动系统。

  • 然后,我们尝试使用 overcommit_memory (= 2) + overcommit_ratio (= 75) 参数,因此当情况变得危急时不会启动进程。因此,不再需要重新启动服务器。但是我们的启动脚本现在在达到限制时会报告几十个错误:新进程立即出错,进程永远不会启动。

  • 1234563 /p>

【问题讨论】:

标签: linux memory process


【解决方案1】:

我编写了这个小工具来允许并行运行任意数量的作业,或者限制并行度。只需阅读标题中的 cmets 以了解如何使用它 - 您可以一次将作业数量限制为 2 个或 3 个或类似的东西,但一次将它们全部提交。它在下面使用 REDIS,它是免费的,而且非常易于安装。

#!/bin/bash
################################################################################
# File: core
# Author: Mark Setchell
# 
# Primitive, but effective tool for managing parallel execution of jobs in the
# shell. Based on, and requiring REDIS.
#
# Usage:
#
# core -i 8 # Initialise to 8 cores, or specify 0 to use all available cores
# for i in {0..63}
# do
#   # Wait for a core, do a process, release core
#   (core -p; process; core -v)&
# done
# wait
################################################################################
function usage {
    echo "Usage: core -i ncores # Initialise with ncores. Use 0 for all cores."
    echo "       core -p        # Wait (forever) for free core."
    echo "       core -v        # Release core."
    exit 1
}

function init {
    # Delete list of cores in REDIS
    echo DEL cores | redis-cli > /dev/null 2>&1
    for i in `seq 1 $NCORES`
    do
       # Add another core to list of cores in REDIS
       echo LPUSH cores 1 | redis-cli > /dev/null 2>&1
    done
    exit 0
}

function WaitForCore {
    # Wait forever for a core to be available
    echo BLPOP cores 0 | redis-cli > /dev/null 2>&1
    exit 0
}

function ReleaseCore {
    # Release or give back a core
    echo LPUSH cores 1 | redis-cli > /dev/null 2>&1
    exit 0
}

################################################################################
# Main
################################################################################
while getopts "i:pv" optname
  do
    case "$optname" in
      "i")
        if [ $OPTARG -lt 1 ]; then
           NCORES=`sysctl -n hw.logicalcpu`;    # May differ if not on OSX
        else
           NCORES=$OPTARG
        fi
    init $NCORES
        ;;
      "p")
    WaitForCore
        ;;
      "v")
    ReleaseCore
        ;;
      "?")
        echo "Unknown option $OPTARG"
        ;;
    esac
done
usage

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2011-10-03
    • 2012-09-16
    • 2011-01-24
    • 1970-01-01
    相关资源
    最近更新 更多