Linux消耗内存和CPU资源

934 阅读2分钟

这是我参与11月更文挑战的第1天,活动详情查看:2021最后一次更文挑战

问题

在生产环境的服务器上,偶尔会出现因为其他服务占用资源而影响我们主要服务的运行。当我们测试环境的服务器配置较高时,或者占用不是很高的时候,我们需要采取一些特殊手段来占用资源来测试对应用服务的影响。也有可能是客户提出的一些奇怪的需求(我们就是客户嫌资源占用不是很高,需要搞一搞)。

消耗内存

查看内存占用

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           7.6G        838M        5.0G        376M        1.8G        6.2G
Swap:            0B          0B          0B

创建一个临时目录

$ mkdir /tmp/memory

指定tmpfs的文件系统及大小3000M挂载在该目录下。大小可以根据情况调整

$ mount -t tmpfs -o size=3000M tmpfs /tmp/memory

接着使用 dd 命令,往该目录下写入多少内容,就会占用多少内存,由于我们的目的是占用内存,因此 if 直接使用 /dev/zero

$ dd if=/dev/zero of=/tmp/memory/block

查看占用结果

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           7.6G        838M        2.0G        3.3G        4.8G        3.2G
Swap:            0B          0B          0B

删除占用

$ rm -rf /tmp/memory/block

消耗CPU

查看CPU占用

$ top
top - 14:15:55 up 270 days, 21:25,  2 users,  load average: 0.02, 0.77, 0.89
Tasks: 238 total,   1 running, 237 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  0.1 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8008276 total,  2140308 free,   857384 used,  5010584 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  3387124 avail Mem 

# 按1切换到查看多核
top - 14:17:02 up 270 days, 21:26,  2 users,  load average: 0.01, 0.62, 0.83
Tasks: 238 total,   1 running, 237 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  0.3 us,  0.0 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8008276 total,  2140184 free,   857508 used,  5010584 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  3387000 avail Mem 

# Ctrl + C 退出

编写脚本

$ vim killcpu.sh

脚本内容

#! /bin/sh   
# filename killcpu.sh  
if [ $# != 1 ] ; then  
 echo "USAGE: $0 <CPUs>"  
 exit 1;  
fi  
for i in `seq $1`  
do  
 echo -ne "   
i=0;   
while true  
do  
i=i+1;   
done" | /bin/sh &  
 pid_array[$i]=$! ;  
done  
  
for i in "${pid_array[@]}"; do  
 echo 'kill ' $i ';';  
done

分配权限以及设为可执行文件

$ chmod 777 killcpu.sh

运行脚本

# 脚本后参数表示占用几个核心
$ ./killcpu.sh 2
kill  24302 ;
kill  24304 ;

查看验证

$ top
top - 14:29:50 up 270 days, 21:39,  2 users,  load average: 1.75, 0.73, 0.61
Tasks: 240 total,   3 running, 237 sleeping,   0 stopped,   0 zombie
%Cpu0  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :100.0 us,  0.0 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8008276 total,  2139140 free,   858452 used,  5010684 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  3386060 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                          
24302 root      20   0  113280   1192   1008 R 100.0  0.0   2:03.36 sh                                                                               
24304 root      20   0  113280   1192   1008 R 100.0  0.0   2:03.35 sh 

关闭占用

$ kill  24302
$ kill  24304