Linux脚本之模拟内存占用

前言

总有一些场景我们需要让内存看起来很满,那还等啥,填满她!

1. 准备脚本

准备 memlimit.sh 脚本

#!/bin/bash

if [ # -gt 2 ]; then
    echo "USAGE:0<cmd,size>"
    exit 1
fi
cmd=1
free -h
if [cmd = 'start' ];then
    [ -d /tmp/memory ] && exit 1 || mkdir /tmp/memory
    #单位G,可自行更改
    mount -t tmpfs -o size=2G tmpfs /tmp/memory
    dd if=/dev/zero of=/tmp/memory/block
    free -h
fi
if [cmd = 'stop' ];then
    if [ -d /tmp/memory ];then
        #等同rm -rf /tmp/memory/block
        find /tmp/memory -name block -delete
        umount /tmp/memory
        #等同rmdir /tmp/memory
        find /tmp -name memory -delete
        free -h
    fi
fi

2. 运行脚本

#限制cpu 60%
sh memlimit.sh start 10
#释放cpu
sh memlimit.sh stop

Leave a Reply

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