->使用free命令查看内存使用情况:
1.echo 3 > /proc/sys/vm/drop_caches
2.free
或者使用cat /proc/yourpid/status 来查看对应pid的内存占用(VmRSS为当前值,VmHWM为峰值)
(注:linux下的malloc申请100mb内存,并不会马上得到100mb,只会预分配一部分,用到时才另外分配)
->使用top -n 1查看进程的CPU占用情况。
或者使用以下方法计算:
1 FILE *fp = fopen("/proc/stat", "r"); 2 if (fp) { 3 char name[64] = { 0 }; 4 char buffer[1024] = { 0 }; 5 static int user0 = 0, total0 = 0; 6 int user1, nice, sys, idle, iowait, irq, softirq, total1 = 0; 7 8 fgets(buffer, sizeof(buffer), fp); 9 sscanf(buffer, "%s %d %d %d %d %d %d %d", name, &user1, &nice, &sys, 10 &idle, &iowait, &irq, &softirq); 11 total1 = user1 + nice + sys + iowait + irq + softirq + idle; 12 if (total1 != total0) 13 m_iCpuPercent = ((user1 - user0) * 100)/ (double) (total1 - total0); 14 else 15 m_iCpuPercent = 0; 16 user0 = user1; 17 total0 = total1; 18 fclose(fp); 19 } else { 20 fprintf(stderr, "fopen failed:%s ", strerror(errno)); 21 }