查询进程所占内存的方法如下:
//VmSize: 表示进程当前虚拟内存大小
//VmHWM: 表示进程所占用物理内存的峰值
//VmRSS: 表示进程当前占用物理内存的大小(与procrank中的RSS)
static const char *GREP_KEY = "-E \"VmSize|VmRSS|VmHWM\"";
void print_mem(const char *prefix, const char *grep)
{
int pid = getpid();
char command[256];
sprintf(command, "cat /proc/%d/status | grep %s", pid, grep);
printf("==== %s \n", prefix);
system(command);
printf("\n");
}
在应用中添内存使用查询接口,使用示例如下:
print_mem("before vip_run_network", GREP_KEY);
status = vip_run_network(batchs[i].network);
if (status != VIP_SUCCESS) {
printf("fail to run network, status=%d, batchCount=%d\n", status, i);
ret = -1;
goto exit;
}
print_mem("after vip_run_network", GREP_KEY);