需求:消耗指定大小的内存来提升服务器利用率或者验证服务器某些问题
1、使用C程序的方法
消耗指定大小的内存:cat malloc_mb.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define UNIT (1024*1024)
int main(int argc, char *argv[])
{
long long i = 0;
int size = 0;
if (argc != 2) {
printf(" === argc must 2\n");
return 1;
}
size = strtoull(argv[1], NULL, 10);
if (size == 0) {
printf(" argv[1]=%s not good\n", argv[1]);
return 1;
}
char *buff = (char *) malloc(size * UNIT);
if (buff)
printf(" we malloced %d Mb\n", size);
buff[0] = 1;
for (i = 1; i < (size * UNIT); i++) {
if (i%1024 == 0)
buff[i] = buff[i-1]/