1. c脚本 消耗内存
1)在your_directory目录下,创建文件eatmem.c ,输入以下内容
2)编译:gcc eatmem.c -o eatmem
3) 创建定时任务,每15分钟执行:crontab -e 输入 */15 * * * * /your_directory/eatmem >> /your_directory/memcron.log wq保存,保存之后会生效。
#include <unistd.h> #include <errno.h> #include <stdlib.h> #include <stdio.h> #include <string.h> // Destription : release memory // 1.parameters# gcc test_eatMem.c -o test_eatMem // 2.parameters# ./test_eatMem // Date : 2015-1-12 #define block 4 // eat times 4 block=1G void eatMem() { int i =0; int j = 0; int cell = 256 * 1024 * 1024; //256M char * pMem[block]={0}; // init all pointer to NULL. for(i = 0; i < block; i++) { pMem[i] = (char*)malloc(cell); // eat... if(NULL == pMem[i]) // failed to eat. { printf("Insufficient memory avalible ,Cell %d Failure ", i); break; } memset(pMem[i], 0, cell); printf("[%d]%d Bytes. ", i, cell); fflush(stdout); sleep(1); } //Read&Write 10 次,维持内存消耗的时间 可自己设置 int nTimes = 0; for(nTimes = 0; nTimes < 10; nTimes++) { for(i=0; i<block; i++) { printf("Read&Write [%d] cell. ", i); if(NULL == pMem[i]) { continue; } char ch=0; int j=0; for(j=0; j<cell; j+=1024) { ch = pMem[i][j]; pMem[i][j] = 'a'; } memset(pMem[i], 0, cell); fflush(stdout); sleep(5); } sleep(5); } printf("Done! Start to release memory. "); //释放内存核心代码: for(i = 0; i < block; i++) { printf("free[%d] ", i); if(NULL != pMem[i]) { free(pMem[i]); pMem[i] = NULL; } fflush(stdout); sleep(2); } printf("Operation successfully! "); fflush(stdout); } int main(int argc,char * args[]) { eatMem(); }
2.shell脚本消耗内存
占用1GB内存1个小时. 注意需要可以mount的权限
#!/bin/bash mkdir /tmp/memory mount -t tmpfs -o size=1024M tmpfs /tmp/memory dd if=/dev/zero of=/tmp/memory/block sleep 3600 rm /tmp/memory/block umount /tmp/memory rmdir /tmp/memory
3. shell脚本消耗CPU
1)创建脚本 eatcpu.sh 输入以下内容
2)消耗4台cpu,持续60s(可自己设置): ./eatcpu.sh 4
#! /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 time=$(date "+%Y-%m-%d %H:%M:%S") echo "${time}" for i in "${pid_array[@]}"; do echo 'kill ' $i ';'; done sleep 60 for i in "${pid_array[@]}"; do kill $i; done