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\n", i); break; } memset(pMem[i], 0, cell); printf("[%d]%d Bytes.\n", 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.\n", 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.\n"); //釋放內存核心代碼: for(i = 0; i < block; i++) { printf("free[%d]\n", i); if(NULL != pMem[i]) { free(pMem[i]); pMem[i] = NULL; } fflush(stdout); sleep(2); } printf("Operation successfully!\n"); 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
