RuntimeUtility.java

  1. /*  This file is part of Openrouteservice.
  2.  *
  3.  *  Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
  4.  *  GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
  5.  *  of the License, or (at your option) any later version.

  6.  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  7.  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8.  *  See the GNU Lesser General Public License for more details.

  9.  *  You should have received a copy of the GNU Lesser General Public License along with this library;
  10.  *  if not, see <https://www.gnu.org/licenses/>.
  11.  */
  12. package org.heigit.ors.util;

  13. import org.apache.log4j.Logger;

  14. import java.text.DecimalFormat;

  15. public class RuntimeUtility {
  16.     private RuntimeUtility() {
  17.     }

  18.     public static void clearMemory(Logger logger) {
  19.         logger.info("====> Recycling garbage...");
  20.         printRAMInfo("Before: ", logger);
  21.         Runtime.getRuntime().gc();
  22.         printRAMInfo("After: ", logger);
  23.         logger.info("========================================================================");
  24.     }

  25.     public static void printRAMInfo(String hint, Logger logger) {
  26.         logger.info(hint + "Total - " + getMemorySize(Runtime.getRuntime().totalMemory()) + ", Free - "
  27.                 + getMemorySize(Runtime.getRuntime().freeMemory()) + ", Max: " + getMemorySize(Runtime.getRuntime().maxMemory())
  28.                 + ", Used - "
  29.                 + getMemorySize(Runtime.getRuntime().totalMemory() - (Runtime.getRuntime().freeMemory())));
  30.     }

  31.     public static String getMemorySize(long size) {
  32.         String hrSize = null;
  33.         double b = size;
  34.         double k = size / 1024.0;
  35.         double m = ((size / 1024.0) / 1024.0);
  36.         double g = (((size / 1024.0) / 1024.0) / 1024.0);
  37.         double t = ((((size / 1024.0) / 1024.0) / 1024.0) / 1024.0);

  38.         DecimalFormat dec1 = new DecimalFormat("0.00");
  39.         DecimalFormat dec2 = new DecimalFormat("0");
  40.         if (t > 1) {
  41.             hrSize = isDouble(t) ? dec1.format(t).concat(" TB") : dec2.format(t).concat(" TB");
  42.         } else if (g > 1) {
  43.             hrSize = isDouble(g) ? dec1.format(g).concat(" GB") : dec2.format(g).concat(" GB");
  44.         } else if (m > 1) {
  45.             hrSize = isDouble(m) ? dec1.format(m).concat(" MB") : dec2.format(m).concat(" MB");
  46.         } else if (k > 1) {
  47.             hrSize = isDouble(k) ? dec1.format(k).concat(" KB") : dec2.format(k).concat(" KB");
  48.         } else {
  49.             hrSize = isDouble(b) ? dec1.format(b).concat(" B") : dec2.format(b).concat(" B");
  50.         }
  51.         return hrSize;
  52.     }

  53.     private static boolean isDouble(double value) {
  54.         return value % 1 != 0;
  55.     }
  56. }