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

总结!linux 消耗内存和cpu 定时任务的更多相关文章

  1. 查找Linux中内存和CPU使用率最高的进程

    下面的命令会查看到按照RAM和CPU降序方式的前最高几名进程的列表: [root@iZ25pvjcsyhZ ~]# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem ...

  2. linux 查看内存和cpu占用比较多的进程

    1.可以使用一下命令查使用内存最多的10个进程        ps -aux | sort -k4nr | head -n 102. 可以使用一下命令查使用CPU最多的10个进程        ps ...

  3. Linux中内存、CPU使用情况查看

    1.背景 在实际生产中我们为了保证系统能稳定运行,我们经常要查看当前的CPU和系统使用情况 建议使用top,简单丰富,快捷 2.使用free查看内存使用情况 3.使用 top查看内存.cpu内存占比 ...

  4. linux 查看内存和cpu

    Linux查看CPU和内存使用情况 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 ...

  5. 查看Linux系统内存、CPU、磁盘使用率和详细信息

    一.查看内存占用 1.free # free -m 以MB为单位显示内存使用情况 [root@localhost ~]# free -m total used free shared buff/cac ...

  6. linux获取内存、cpu、负载、网口流量、磁盘信息

    内存信息 / meminfo 返回dict   #!/usr/bin/env python   def memory_stat():   mem = {}   f = open("/proc ...

  7. linux查看内存、CPU占用资源最多的进程

    [内存占用] #利用ps命令,默认使用ps参数会显示的结果 ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 ...

  8. 系统内存和CPU管理、监控

    本博文的主要内容有 .系统内存管理.监控:vmstat和free -mt .系统CPU管理.监控:sar -u.mpstat.uptime linux系统内存和CPU是在系统运行的过程中不断消耗的资源 ...

  9. Linux 查看进程消耗内存情况总结

    在Linux中,有很多命令或工具查看内存使用情况,今天我们来看看如何查看进程消耗.占用的内存情况,Linux的内存管理和相关概念要比Windows复杂一些.在此之前,我们需要了解一下Linux系统下面 ...

随机推荐

  1. [JNA系列]Java调用Delphi编写的Dll之实例Delphi使用PAnsiChar

    Delphi代码 unit UnitDll; interface uses StrUtils, SysUtils, Dialogs; function DoBusinessAnsi(pvData: P ...

  2. Android Screen Monitor

    实现屏幕同步前提是安装有JDK和配置好ADB的环境变量 1.官方地址 http://code.google.com/p/android-screen-monitor/ 2.解压缩得到asm.jar 3 ...

  3. linux学习笔记31--命令route和routetrace

    Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或 ...

  4. springboot2.0 fastjson 日期格式化不生效解决

    网上有很多介绍,主要有两种. 1.在启动类中注入bean /** * 集成fastjson * * @return */ @Bean public HttpMessageConverters fast ...

  5. The Definitive Guide To Django 2 学习笔记(一) Views and UrL confsRL

    1.如何找到django在Ubuntu下的安装路径: 进入python命令行,import django,print(django.__path__) 2.使用django-admin.py 创建项目 ...

  6. 如何使用VMWare共享Win7中的文件夹,对应Linux中的哪个目录下面?

    访问 /mnt/hgfs/你设置的共享名,如果找不到这个hgfs这个文件夹,那说明你还没正确安装好 install VMware tools

  7. mongdb复制集搭建

    可参考官网教程 复制集增加了数据的冗余同时也提高了mongodb的可靠性,相比传统的主从架构,mongodb具有自动容灾的特性,即主库挂掉后会自动从剩下的从库中选举出一个节点做为主库(不需要人工干预) ...

  8. volatile关键字是什么意思

    我写了一段简单的对比代码并分别用vs2017以release方式编译然后用IDA观察汇编代码,如下图所示: 代码1 int a=5; printf("%d",a) 代码2 vola ...

  9. Android.mk介绍

    Secrets of Android.mk Intro to Android.mk Simple example NDK Usage Defining Modules Simple APK APK D ...

  10. jquery.js:8672 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

    html5谷歌流浪器报错:jquery.js:8672 Synchronous XMLHttpRequest on the main thread is deprecated because of i ...