总结!linux 消耗内存和cpu 定时任务
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 定时任务的更多相关文章
- 查找Linux中内存和CPU使用率最高的进程
下面的命令会查看到按照RAM和CPU降序方式的前最高几名进程的列表: [root@iZ25pvjcsyhZ ~]# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem ...
- linux 查看内存和cpu占用比较多的进程
1.可以使用一下命令查使用内存最多的10个进程 ps -aux | sort -k4nr | head -n 102. 可以使用一下命令查使用CPU最多的10个进程 ps ...
- Linux中内存、CPU使用情况查看
1.背景 在实际生产中我们为了保证系统能稳定运行,我们经常要查看当前的CPU和系统使用情况 建议使用top,简单丰富,快捷 2.使用free查看内存使用情况 3.使用 top查看内存.cpu内存占比 ...
- linux 查看内存和cpu
Linux查看CPU和内存使用情况 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 ...
- 查看Linux系统内存、CPU、磁盘使用率和详细信息
一.查看内存占用 1.free # free -m 以MB为单位显示内存使用情况 [root@localhost ~]# free -m total used free shared buff/cac ...
- linux获取内存、cpu、负载、网口流量、磁盘信息
内存信息 / meminfo 返回dict #!/usr/bin/env python def memory_stat(): mem = {} f = open("/proc ...
- linux查看内存、CPU占用资源最多的进程
[内存占用] #利用ps命令,默认使用ps参数会显示的结果 ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 ...
- 系统内存和CPU管理、监控
本博文的主要内容有 .系统内存管理.监控:vmstat和free -mt .系统CPU管理.监控:sar -u.mpstat.uptime linux系统内存和CPU是在系统运行的过程中不断消耗的资源 ...
- Linux 查看进程消耗内存情况总结
在Linux中,有很多命令或工具查看内存使用情况,今天我们来看看如何查看进程消耗.占用的内存情况,Linux的内存管理和相关概念要比Windows复杂一些.在此之前,我们需要了解一下Linux系统下面 ...
随机推荐
- atitit.jQuery Validate验证框架详解与ati Validate 设计新特性
atitit.jQuery Validate验证框架详解与ati Validate 设计新特性 1. AtiValidate的目标1 2. 默的认校验规则1 2.1. 使用方式 1.metadata用 ...
- atitit.提取zip rar文件列表 java php c# 的原理与设计
atitit.java提取zip rar文件列表 1. 取zip rar文件的场景问题 1 1.1. 多重压缩的问题 1 1.2. 文件名编码的问题 1 1.3. 目录的判定 1 2. rar的解析 ...
- 使用SQLite
SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成. Python就 ...
- redis命令_ZREVRANGEBYSCORE
ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] 返回有序集 key 中, score 值介于 max 和 min 之间(默 ...
- sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛
Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown character set: 'utf8mb4'
排除 1.mysql 是utf8 2.连接url加了useUnicode=true&&characterEncoding=utf8 3.项目也没有用到utf8mb4 由于是mysql驱 ...
- js----Navigator对象,查看浏览器信息,Screen对象,查看屏幕信息
Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本. 对象属性: 查看浏览器的名称和版本,代码如下: <script type=" ...
- JSON学习【转自慕课网】
视频网址 从慕课网视频里的PPT截图过来的,因为是用PHP讲的,而且后面讲的一般,所以只截取了前两节课的基础内容,其实只看一下第一张PPT就可以了.
- org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class
转载自:http://songjianyong.iteye.com/blog/1663170 org.springframework.transaction.CannotCreateTransacti ...
- anaconda指定镜像源,解决conda下载速度慢失败问题
conda升级默认官网地址,速度会特别慢,现在我们指定一个当前可用的镜像,步骤如下: 1.执行命令,生成.condarc文件 conda config --add channels https://m ...