zoukankan      html  css  js  c++  java
  • Linux下内存占用和CPU占用的计算

    ->使用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         }    
  • 相关阅读:
    Harbor1.5.2批量清理无效镜像
    前端私服地址修改
    Paxos算法
    缓存淘汰算法--LRU算法
    一致性哈希
    mysql常见知识点总结
    mysql分库分表(二)
    mysql分库分表(一)
    dubbo学习小结
    dubbo超时重试和异常处理
  • 原文地址:https://www.cnblogs.com/littlemeng/p/4756744.html
Copyright © 2011-2022 走看看