好的,下面是一个简单的
内存 压力 测试工具的示例代码,可以在
Linux系统中
使用:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#define MB (1024 * 1024)int main(int argc, char *argv[]) {if (argc < 2) {printf("Usage:memtest<memory size in MB>");return 0;}int size = atoi(argv[1]) * MB;char *p = malloc(size);if (!p) {printf("Memory allocation failed!");return 0;}memset(p, 0, size);printf("Allocated %d MB memory successfully!", size / MB);while (1) {sleep(1);}free(p);return 0;}
这个程序会根据输入的参数申请相应大小的
内存空间,并通过循环不断占用该
内存空间,直到程序被手动停止。
你可以通过命令行输入需要测试的
内存大小,比如:
$ https://blog.csdn.net/Naidas/article/details/memtest100
这个命令会申请100MB的
内存空间,并不断占用该空间,直到程序被手动停止。
注意,这个程序可能会对系统造成一定的负担,因此请谨慎
使用,并确保你有足够的权限来执行该程序。同时,也请确保你的系统有足够的可用
内存来测试。
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.mushiming.com/mjsbk/10930.html