#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <errno.h>

int main(void)
{
int nMemoryLimit = 1024 * 1024 * 5;//5M

struct rlimit memoryL;

getrlimit(RLIMIT_AS, &memoryL);
printf("before set mem:%lu,%lu\r\n", (unsigned long)memoryL.rlim_cur, (unsigned long)memoryL.rlim_max);

memoryL.rlim_cur = nMemoryLimit;
setrlimit(RLIMIT_AS ,&memoryL);

getrlimit(RLIMIT_AS, &memoryL);
printf("after set mem:%lu,%lu\r\n", (unsigned long)memoryL.rlim_cur, (unsigned long)memoryL.rlim_max);

int i = 0;
while(1)
{
i++;
//当进程内存达到最大值时,malloc calloc返回NULL,记录错误errno=ENOMEM
errno = 0;
char *psString = (char *)calloc(1, 1024);
if(NULL == psString)
{
//printf("errno=%d, ENOMEM=%d\r\n", errno, ENOMEM);
}
else
{
printf("success, %d\r\n", i);
}
}
return 0;
}