11 月 3 2014
VPS测试实际可用内存的脚本
本文共被喵星人侦察过11,867次。。。网上找的,觉得有用记录下。
/**
* MemoryTest.cpp
* (c) 2014 David Huang
*
* Use this program AT YOUR OWN RISK !
* DO NOT COMPILE AND USE IT ON ANY
* PRODUCTION SERVER !
*/
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
int main()
{
cout<<"I will try my best to fill your RAM."<<endl
<<"You have 3 seconds to quit (Ctrl+C)"<<endl;
sleep(3);
long allocatedMB = 0;
while (true)
{
unsigned char * leakThisMemoryPlease = new unsigned char[10485760];
for (int i = 0; i < 10485760; i++)
leakThisMemoryPlease[i] = i*rand();
allocatedMB += 10;
cout<<allocatedMB<<"MB allocated"<<endl;
}
return 0;
}
C++文件,想编译的话得有GCC神马的。。。CentOS(RHEL)下
sudo yum groupinstall 'Development Tools'
Ubuntu(Debian)下
sudo apt-get install build-essential
用GCC编译的时候记得加上
-l stdc++
参数。另外如果有SWAP的话最后输出的值应该是Ram+Swap的总和。
2014 年 11 月 13 日 @ 20:20
这个的确值得mark
2014 年 12 月 14 日 @ 00:30
马一个,赞~
2014 年 12 月 14 日 @ 00:32
@RalphTsui:喵了个咪的~~~
2014 年 12 月 23 日 @ 16:21
支持一下。。。独立IP主机5折疯抢,终身续费5折,有需要的抓紧时间。
2016 年 01 月 26 日 @ 14:13
这程序差点吧我电脑弄爆了= =
2016 年 01 月 26 日 @ 20:21
@静静:哈哈这种暴力测试可用内存的程序会让大多数其他进程被系统kill的。。。不过重启就好了2333
2016 年 01 月 26 日 @ 21:36
@大懒猫:我们来一发友链吧QWQ