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 =; int j = ; int cell = * * ; //256M
char * pMem[block]={}; // init all pointer to NULL.
for(i = ; 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], , cell);
printf("[%d]%d Bytes.\n", i, cell); fflush(stdout);
sleep();
} //Read&Write 10 次,维持内存消耗的时间 可自己设置
int nTimes = ;
for(nTimes = ; nTimes < ; nTimes++)
{
for(i=; i<block; i++)
{
printf("Read&Write [%d] cell.\n", i); if(NULL == pMem[i])
{
continue;
} char ch=;
int j=;
for(j=; j<cell; j+=)
{
ch = pMem[i][j];
pMem[i][j] = 'a';
}
memset(pMem[i], , cell); fflush(stdout);
sleep();
} sleep();
} printf("Done! Start to release memory.\n");
//释放内存核心代码:
for(i = ; i < block; i++)
{
printf("free[%d]\n", i);
if(NULL != pMem[i])
{
free(pMem[i]);
pMem[i] = NULL;
} fflush(stdout);
sleep();
} 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
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 [ $# != ] ; then
echo "USAGE: $0 <CPUs>"
exit ;
fi
for i in `seq $`
do
echo -ne "
i=;
while true
do
i=i+;
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 for i in "${pid_array[@]}"; do
kill $i;
done