可以用来编译出吃内存进程的代码段
2018-01-28 09:54
113 查看
下面的代码是根据网上的某一篇博客整理得出的,在实践中使用过的“用来编译出吃内存进程的代码段”。原博客主可以联系我加上原始链接
实际使用时,会将该段代码使用实际设备配套的工具链编译出相应的进程,将该进程在设备上后台运行后并可以主动去吃内存。
实际使用时,会将该段代码使用实际设备配套的工具链编译出相应的进程,将该进程在设备上后台运行后并可以主动去吃内存。
#include <unistd.h> #include <errno.h> #include <stdlib.h> #include <stdio.h> #include <string.h> void eatMem(unsigned int block) { unsigned int i; unsigned int j; unsigned int nTimes; char ch; char **pMem; /* 每次申请 1M */ unsigned int cell = 1 * 1024 * 1024; pMem = (char **)malloc(sizeof(char *) * block); for (i = 0; i < block; i++) { pMem[i] = (char *)malloc(cell); if(NULL == pMem[i]) { printf("Insufficient memory avalible ,Cell %d Failure\n", i); break; } memset(pMem[i], 0, cell); printf("[%u]%u Bytes.\n", i, cell); fflush(stdout); sleep(1); } /* Read & Write 100 次, 每次40秒,用来维持占有的内存一段时间 */ for (nTimes = 0; nTimes < 100; nTimes++) { for(i = 0; i < block; i++) { printf("Read&Write [%u] cell.\n", i); if(NULL == pMem[i]) { continue; } for(j = 0; j < cell; j++) { pMem[i][j] = 'a'; ch = pMem[i][j]; } memset(pMem[i], 0, cell); fflush(stdout); sleep(1); } sleep(1); } printf("Done! Start to release memory.\n"); //释放内存核心代码 for(i = 0; i < block; i++) { printf("free[%u]\n", i); if(NULL != pMem[i]) { free(pMem[i]); pMem[i] = NULL; } fflush(stdout); sleep(2); } printf("Operation successfully!\n"); fflush(stdout); } /* gcc test.c -o test, 使用该进程时可以 ./test 30, 表示该进程主动吃30M 的内存 */ int main(int argc,charchar * args[]) { if (argc != 2) { printf("please input correct parameter\n"); fflush(stdout); } unsigned int block = (unsigned int)atoi(argv[1]); printf("block is %u\n", block); eatMem(block); while (1) { sleep(10); } return 0; }
相关文章推荐
- 第一个Makefile文件(可以用来编译前面红黑树的实现程序和测试程序)
- System.UnauthorizedAccessException: 拒绝访问 temp 目录。用来运行 XmlSerializer 的标识“NT AUTHORITY\NETWORK SERVICE”没有访问 temp 目录的足够权限。CodeDom 将使用进程正在使用的用户帐户进行编译,这样,如
- 转:wiindows的单个进程在系统最大可以访问的内存数量
- unistd.h 中int access(const char * pathname, int mode); 判断进程能否以mode模式访问pathname文件(可以用来判断文件/目录是否存在)
- DotNet Helper 可以用来将DLL生成IL,修改完IL以后再将其编译为DLL
- 程序猿之---C语言细节22(函数返回指针注意事项<悬空指针>、查看进程可以分配的内存大小)
- System.UnauthorizedAccessException: 拒绝访问 temp 目录。用来运行 XmlSerializer 的标识“NT AUTHORITY\NETWORK SERVICE”没有访问 temp 目录的足够权限。CodeDom 将使用进程正在使用的用户帐户进行编译,这样,如
- 动态加载EXE文件到内存执行(测试可以编译成功)
- 《C专家编程》笔记(二)---测试一下你的进程中可以分配多大的内存
- 《C专家编程》笔记(二)---测试一下你的进程中可以分配多大的内存
- Clang - 是LLVM的前端,可以用来编译C,C++,ObjectiveC等语言
- C++中erase函数的使用,可以用来删除内存擦除
- ★实验 8-1 1. 创建一个守候进程。并为自己创建独立的日志文件。 2. 每隔 1s 向日志文件中写入如下信息: a) 未使用的内存大小(MemFree) 可以从/proc/meminf
- System.UnauthorizedAccessException: 拒绝访问 temp 目录。用来运行 XmlSerializer 的标识“NT AUTHORITY\NETWORK SERVICE”没有访问 temp 目录的足够权限。CodeDom 将使用进程正在使用的用户帐户进行编译,这样,如
- Android Gradle Release Version 2.4 增加多进程并行编译,经测试可以提升编译速度20% 到 40%
- 编译clang的时候ld进程耗尽内存
- 习惯把运行的class的名字作为文件名保存 如果文件中类是public修饰,那么类名必须和文件名相同 一个java文件中可以放几个public的类??? java变量:变量是用来标识一块内存的,变量必
- ubuntu 14 编译视频第三方库ijkplayer,可以在winows下使用
- .NET进程通信机制——内存映像文件