总结!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系统下面 ...
随机推荐
- 浏览器在线打开pdf
https://www.cnblogs.com/Leo_wl/p/5735001.html#_label0 https://blog.csdn.net/li_cheng_liang/article/d ...
- freemarker和jsp的比较
1.共享变量 FreeMarker 的共享变量是我最喜欢的“隐藏”功能之一.此功能可以让你设置自动添加到所有模板的值. 例如,可以设置应用程序的名称作为共享变量. 1 2 Configuration ...
- MapReduce编程实例4
MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...
- spring-core依赖jar包
- css断句 word-break
word-break:break-all;word-wrap:break-word; 兼容IE6 火狐 chrome
- 关于OutputStream的write方法FAQ(from bbs.csdn.net)
问: Java code ? 1 2 3 4 5 6 7 8 9 10 11 FileInputStream fis = new FileInputStream(new File("C:\\ ...
- Eqs - poj 1840(hash)
题意:对于方程:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 ,有xi∈[-50,50], xi != 0, any i∈{1,2,3,4,5}. 现在给出a1,a2,a3, ...
- 技巧C#
1. 在CallBack之后保持滚动条的位置: 在Asp.Net1.1中,CallBack之后保持滚动条的位置是一件非常痛苦的事情,特别是页中有一个Grid并且想要编辑特定的行.为了不停留在想 ...
- php ajax dom---动态增加
js代码 <script type="text/javascript"> $(document).ready(function(){ $("#talk_sen ...
- 在java中,List是个接口,那实现List接口的类有哪些,有什么区别?
在java中,List是个接口,那实现List接口的类有哪些,有什么区别? 解答: ArrayList是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引 ...