Linux环境下C语言模拟内存负载测试

简介: 新年第一天,希望在新年里认识更多朋友,互相学习,共同进步。今天用C语言实现一个Memory负载的压力测试程序. 点击(此处)折叠或打开 #include stdio.
新年第一天,希望在新年里认识更多朋友,互相学习,共同进步。今天用C语言实现一个Memory负载的压力测试程序.

点击(此处)折叠或打开

  1. #include stdio.h>
  2. #include stdlib.h>
  3. #include string.h>

  4. #define PAGE_SZ (112)

  5. int main() {
  6.     int i;
  7.     int gb = 1; //以GB为单位分配内存大小

  8.     for (i = 0; i ((unsigned long)gb30)/PAGE_SZ ; ++i) {
  9.         void *m = malloc(PAGE_SZ);
  10.         if (!m)
  11.             break;
  12.         memset(m, 0, 1);
  13.     }
  14.     printf("allocated %lu MB\n", ((unsigned long)i*PAGE_SZ)>>20);
  15.     getchar();
  16.     return 0;
  17. }

系统当前的内存大小为1.2G.


查看当前系统内存的动态变化状态,1.2G大约使用了148M左右.


编译程序后执行再观察,程序中允许分配的内存为1GBi=1024MBi,发现30s内CPU和内存的负载均达到了最大,监控中看到内存占用了1.1G,约1.2G的91.8%.


这个方法在模拟内存负载时,做压力测试还是比较有用处的,所以分享一下.

目录
相关文章
|
1月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
106 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
1月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
169 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
1月前
|
存储 Linux 开发工具
Linux环境下使用Buildroot配置软件包
使用Buildroot可以大大简化嵌入式Linux系统的开发和维护工作,但它需要对Linux系统和交叉编译有深入的理解。通过上述步骤,可以有效地配置和定制软件包,为特定的嵌入式应用构建高效、稳定的系统。
166 11
|
2月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
268 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
2月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
366 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
2月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
534 0
|
3月前
|
Linux
Linux环境下的UDEV机制及其与守护进程的关联
实际使用时管理员需要熟悉编写合适udev rules去满足特殊需求;同时也需要注意避免编写过度复杂导致无法预料结果rules.UDEVD虽然稳健但错误配置可能导致无法预料问题因此需谨慎处理相关配置工作.
139 16
|
2月前
|
存储 安全 Linux
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
486 0
|
安全 Linux 测试技术
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
210 0