VPS测试实际可用内存的脚本

网上找的,觉得有用记录下。

/**
 * 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的总和。

anyShare分享到: