一条运维短信引起的思考

刚吃完饭,看到手机来了几条短信,显示内存不够,赶紧登陆机器

发现问题:

8G内存,占用了7G,而我的java进程设置了 -Xms2048m -Xmx2048m  ,那内存跑哪去了?


恰好看到azkaban计算可用内存代码,留存一份,代码如下:

private static void readMemoryInfoFile() {

    BufferedReader br = null;

    try {

      br = new BufferedReader(new FileReader(MEMINFO_FILE));

 

      long sizeMemFree = 0;

      long sizeBuffers = 0;

      long sizeCached = 0;

      long sizeSwapCached = 0;

      int count = 0;

      String line = br.readLine();

      while (line != null) {

        if (line.startsWith("MemFree:") || line.startsWith("Buffers:")

            || line.startsWith("Cached") || line.startsWith("SwapCached")) {

          int idx1 = line.indexOf(":");

          int idx2 = line.lastIndexOf("kB");

          String strSize = line.substring(idx1+1, idx2-1).trim();

          

          if (line.startsWith("MemFree:")) {

            sizeMemFree = Long.parseLong(strSize);

          } else if (line.startsWith("Buffers:")) {

            sizeBuffers = Long.parseLong(strSize);

          } else if (line.startsWith("Cached:")) {

            sizeCached = Long.parseLong(strSize);

          } else if (line.startsWith("SwapCached:")) {

            sizeSwapCached = Long.parseLong(strSize);

          }

 

          //all lines read

          if (++count == 4) {

            break;

          }

        }

        line = br.readLine();

      }

 

      if (count < 4) {

        logger.error("Error: less than 4 rows read from /proc/meminfo for free memory information");

      }

 

      long sizeTotal = sizeMemFree + sizeBuffers + sizeCached + sizeSwapCached;

 

      logger.info(String.format("Current system free memory is %d kb (MemFree %d, Buffers %d, Cached %d, SwapCached %d)",

              sizeTotal, sizeMemFree, sizeBuffers, sizeCached, sizeSwapCached));

 

      if (sizeTotal > 0) {

        updateFreeMemAmount(sizeTotal);

      }

    } catch (IOException e) {

      logger.error("Exception in reading memory info file", e);

    } finally {

      try {

        if (br != null) {

          br.close();

        }

      } catch (IOException e) {

        logger.error("Exception in closing the buffered reader", e);

      }

    }

  }

以后别不知道怎么计算一个机器的可用内存了哈!


命令行的方式就是

cat /proc/meminfo | grep -E "^MemTotal:|^MemFree:|^Buffers:|^Cached:|^SwapCached:"

参考: 

azkaban.execapp.ServerStatisticsServlet.fillRemainingMemoryPercent


你可能感兴趣的:(zabbix)