Linux下内存限制进程
我们必须在Linux系统上启动几个饥饿进程。这些进程通常需要几Go(~5Go)内存才能运行(总内存:16Go RAM+2Go交换)Linux下内存限制进程,linux,memory,process,Linux,Memory,Process,我们必须在Linux系统上启动几个饥饿进程。这些进程通常需要几Go(~5Go)内存才能运行(总内存:16Go RAM+2Go交换) 起初,当系统内存不足时,OOM杀手杀死了进程,我们每次都必须重新启动系统 然后,我们尝试使用overmit_memory(=2)+overmit_ratio(=75)参数,因此在情况变得危急时不会启动进程。因此,不再需要重新启动服务器。但我们的启动脚本现在在达到限制时报告了几十个错误:新进程立即出错,而进程从未启动 因此,现在我们正在寻找一种解决方案来启动我们想要
- 起初,当系统内存不足时,OOM杀手杀死了进程,我们每次都必须重新启动系统
- 然后,我们尝试使用overmit_memory(=2)+overmit_ratio(=75)参数,因此在情况变得危急时不会启动进程。因此,不再需要重新启动服务器。但我们的启动脚本现在在达到限制时报告了几十个错误:新进程立即出错,而进程从未启动
- 因此,现在我们正在寻找一种解决方案来启动我们想要的“任意多个”进程,然后它们会被延迟/暂停或其他任何事情,等待它们的兄弟停止。。。它存在吗
- 我编写这个小工具是为了允许任意数量的作业并行运行,或者限制并行度。只需阅读标题中的注释,了解如何使用它-您可以将作业数量限制为每次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
这个应该放在这里