VPS 可用内存检测工具

现在VPS超售都已经是默认的了,特别是OpenVZ架构的虚拟技术,买来一个512M内存的,free命令显示是足的,但是用起来卡不说,没开几个程序就内存爆满了,这种就是超售的感觉。超售多少完全看主机商的良心了。我也被坑过,所以,网上找了方法加上自己折腾,弄了个工具检测。

在linux的VPS新建一个文件名位memtest.cpp的c++文件,内容如下:

/**
 *  * 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;
}

接下来就是编译了

yum install gcc gcc-c++ libstdc++-devel
g++ memtest.cpp -o memtest

编译好了,在同级目录有个可执行文件memtest

执行下就能检测可用内存了,结果如下:

I will try my best to fill your RAM.
You have 3 seconds to quit (Ctrl+C)
10MB allocated
20MB allocated
30MB allocated
40MB allocated
...
430MB allocated
440MB allocated
450MB allocated
460MB allocated
470MB allocated
480MB allocated
490MB allocated
500MB allocated
510MB allocated
Killed

上述是版瓦工的9.9刀年付VPS测试的结果,结果和商家给的512M内存+64M swap略有出入,但是应该算是有良心的商家了。

赞 (1)

评论