Example usage for java.lang Runtime totalMemory

List of usage examples for java.lang Runtime totalMemory

Introduction

In this page you can find the example usage for java.lang Runtime totalMemory.

Prototype

public native long totalMemory();

Source Link

Document

Returns the total amount of memory in the Java virtual machine.

Usage

From source file:com.weibo.api.motan.util.StatsUtil.java

public static String memoryStatistic() {
    Runtime runtime = Runtime.getRuntime();

    double freeMemory = (double) runtime.freeMemory() / (1024 * 1024);
    double maxMemory = (double) runtime.maxMemory() / (1024 * 1024);
    double totalMemory = (double) runtime.totalMemory() / (1024 * 1024);
    double usedMemory = totalMemory - freeMemory;
    double percentFree = ((maxMemory - usedMemory) / maxMemory) * 100.0;

    double percentUsed = 100 - percentFree;

    DecimalFormat mbFormat = new DecimalFormat("#0.00");
    DecimalFormat percentFormat = new DecimalFormat("#0.0");

    StringBuilder sb = new StringBuilder();
    sb.append(mbFormat.format(usedMemory)).append("MB of ").append(mbFormat.format(maxMemory)).append(" MB (")
            .append(percentFormat.format(percentUsed)).append("%) used");
    return sb.toString();
}

From source file:fiftyfive.wicket.util.LoggingUtils.java

/**
 * Returns a string that describes the JVM's memory conditions in this
 * format: {@code 36M used, 29M free, 533M max}.
 *//*from w  w  w .jav  a 2s.c  o m*/
public static String describeMemoryUsage() {
    Runtime runtime = Runtime.getRuntime();
    long free = runtime.freeMemory();

    return String.format("%sM used, %sM free, %sM max", (runtime.totalMemory() - free) / 1000000,
            free / 1000000, runtime.maxMemory() / 1000000);
}

From source file:org.spigotmc.timings.TimingsExport.java

/**
 * Builds an XML report of the timings to be uploaded for parsing.
 *
 * @param sender Who to report to//  w  w  w .j  av  a2  s  . co  m
 */
static void reportTimings(CommandSender sender) {
    Map parent = createObject(
            // Get some basic system details about the server
            pair("version", Bukkit.getVersion()), pair("maxplayers", Bukkit.getMaxPlayers()),
            pair("start", TimingsManager.timingStart / 1000), pair("end", System.currentTimeMillis() / 1000),
            pair("sampletime", (System.currentTimeMillis() - TimingsManager.timingStart) / 1000));
    if (!TimingsManager.privacy) {
        appendObjectData(parent, pair("server", Bukkit.getServerName()),
                pair("motd", Bukkit.getServer().getMotd()),
                pair("online-mode", Bukkit.getServer().getOnlineMode()),
                pair("icon", Bukkit.getServer().getServerIcon().getData()));
    }

    final Runtime runtime = Runtime.getRuntime();

    parent.put("system",
            createObject(pair("timingcost", getCost()), pair("name", System.getProperty("os.name")),
                    pair("version", System.getProperty("os.version")),
                    pair("arch", System.getProperty("os.arch")), pair("totalmem", runtime.totalMemory()),
                    pair("usedmem", runtime.totalMemory() - runtime.freeMemory()),
                    pair("maxmem", runtime.maxMemory()), pair("cpu", runtime.availableProcessors()),
                    pair("runtime", (System.currentTimeMillis() / 1000) - TimingsManager.SERVER_START)));

    Set<Material> tileEntityTypeSet = Sets.newHashSet();
    Set<EntityType> entityTypeSet = Sets.newHashSet();

    int size = HISTORY.size();
    TimingHistory[] history = new TimingHistory[size + 1];
    int i = 0;
    for (TimingHistory timingHistory : HISTORY) {
        tileEntityTypeSet.addAll(timingHistory.tileEntityTypeSet);
        entityTypeSet.addAll(timingHistory.entityTypeSet);
        history[i++] = timingHistory;
    }

    history[i] = new TimingHistory(); // Current snapshot
    tileEntityTypeSet.addAll(history[i].tileEntityTypeSet);
    entityTypeSet.addAll(history[i].entityTypeSet);

    Map handlers = createObject();
    for (TimingIdentifier.TimingGroup group : TimingIdentifier.GROUP_MAP.values()) {
        for (TimingHandler id : group.handlers) {
            if (!id.timed && !id.isSpecial()) {
                continue;
            }
            handlers.put(id.id, toArray(group.id, id.name));
        }
    }

    parent.put("idmap", createObject(pair("groups", toObjectMapper(TimingIdentifier.GROUP_MAP.values(),
            new Function<TimingIdentifier.TimingGroup, JSONPair>() {
                @Override
                public JSONPair apply(TimingIdentifier.TimingGroup group) {
                    return pair(group.id, group.name);
                }
            })), pair("handlers", handlers), pair("worlds", toObjectMapper(TimingHistory.worldMap.entrySet(),
                    new Function<Map.Entry<String, Integer>, JSONPair>() {
                        @Override
                        public JSONPair apply(Map.Entry<String, Integer> input) {
                            return pair(input.getValue(), input.getKey());
                        }
                    })),
            pair("tileentity", toObjectMapper(tileEntityTypeSet, new Function<Material, JSONPair>() {
                @Override
                public JSONPair apply(Material input) {
                    return pair(input.getId(), input.name());
                }
            })), pair("entity", toObjectMapper(entityTypeSet, new Function<EntityType, JSONPair>() {
                @Override
                public JSONPair apply(EntityType input) {
                    return pair(input.getTypeId(), input.name());
                }
            }))));

    // Information about loaded plugins

    parent.put("plugins",
            toObjectMapper(Bukkit.getPluginManager().getPlugins(), new Function<Plugin, JSONPair>() {
                @Override
                public JSONPair apply(Plugin plugin) {
                    return pair(plugin.getName(), createObject(
                            pair("version", plugin.getDescription().getVersion()),
                            pair("description",
                                    String.valueOf(plugin.getDescription().getDescription()).trim()),
                            pair("website", plugin.getDescription().getWebsite()),
                            pair("authors", StringUtils.join(plugin.getDescription().getAuthors(), ", "))));
                }
            }));

    // Information on the users Config

    parent.put("config", createObject(pair("spigot", mapAsJSON(Bukkit.spigot().getSpigotConfig(), null)),
            pair("bukkit", mapAsJSON(Bukkit.spigot().getBukkitConfig(), null))));

    new TimingsExport(sender, parent, history).start();
}

From source file:SystemUtils.java

public static double GetAvailableMemory() {
    Runtime rt = Runtime.getRuntime();
    double availableMem;
    long maxMem, freeMem, usedJvmMem;

    usedJvmMem = rt.totalMemory();
    maxMem = rt.maxMemory();/*from ww  w.  j a  va2s.c o m*/
    freeMem = rt.freeMemory();
    availableMem = (maxMem - (usedJvmMem - freeMem)) / 1048576.0d;

    return availableMem;
}

From source file:pcgen.util.Logging.java

/**
 * Generate the memory report string/*from   w  w  w .ja  v  a 2  s  .c o  m*/
 * @return the memory report string
 */
public static String memoryReportStr() {
    Runtime rt = Runtime.getRuntime();
    NumberFormat numFmt = NumberFormat.getNumberInstance();
    StringBuilder sb = new StringBuilder("Memory: ");
    sb.append(numFmt.format(rt.totalMemory() / 1024.0));
    sb.append("Kb total, ");
    sb.append(numFmt.format(rt.freeMemory() / 1024.0));
    sb.append("Kb free, ");
    sb.append(numFmt.format(rt.maxMemory() / 1024.0));
    sb.append("Kb max.");
    return sb.toString();
}

From source file:com.searchbox.framework.web.SystemController.java

@ModelAttribute("jvmInfo")
public static Map<String, Object> getJvmInfo() {
    Map<String, Object> jvm = new HashMap<String, Object>();

    final String javaVersion = System.getProperty("java.specification.version", "unknown");
    final String javaVendor = System.getProperty("java.specification.vendor", "unknown");
    final String javaName = System.getProperty("java.specification.name", "unknown");
    final String jreVersion = System.getProperty("java.version", "unknown");
    final String jreVendor = System.getProperty("java.vendor", "unknown");
    final String vmVersion = System.getProperty("java.vm.version", "unknown");
    final String vmVendor = System.getProperty("java.vm.vendor", "unknown");
    final String vmName = System.getProperty("java.vm.name", "unknown");

    // Summary Info
    jvm.put("version", jreVersion + " " + vmVersion);
    jvm.put("name", jreVendor + " " + vmName);

    // details//from w w  w.ja v  a 2s.c o  m
    Map<String, Object> java = new HashMap<String, Object>();
    java.put("vendor", javaVendor);
    java.put("name", javaName);
    java.put("version", javaVersion);
    jvm.put("spec", java);
    Map<String, Object> jre = new HashMap<String, Object>();
    jre.put("vendor", jreVendor);
    jre.put("version", jreVersion);
    jvm.put("jre", jre);
    Map<String, Object> vm = new HashMap<String, Object>();
    vm.put("vendor", vmVendor);
    vm.put("name", vmName);
    vm.put("version", vmVersion);
    jvm.put("vm", vm);

    Runtime runtime = Runtime.getRuntime();
    jvm.put("processors", runtime.availableProcessors());

    // not thread safe, but could be thread local
    DecimalFormat df = new DecimalFormat("#.#", DecimalFormatSymbols.getInstance(Locale.ROOT));

    Map<String, Object> mem = new HashMap<String, Object>();
    Map<String, Object> raw = new HashMap<String, Object>();
    long free = runtime.freeMemory();
    long max = runtime.maxMemory();
    long total = runtime.totalMemory();
    long used = total - free;
    double percentUsed = ((double) (used) / (double) max) * 100;
    raw.put("free", free);
    mem.put("free", humanReadableUnits(free, df));
    raw.put("total", total);
    mem.put("total", humanReadableUnits(total, df));
    raw.put("max", max);
    mem.put("max", humanReadableUnits(max, df));
    raw.put("used", used);
    mem.put("used", humanReadableUnits(used, df) + " (%" + df.format(percentUsed) + ")");
    raw.put("used%", percentUsed);

    mem.put("raw", raw);
    jvm.put("memory", mem);

    // JMX properties -- probably should be moved to a different handler
    Map<String, Object> jmx = new HashMap<String, Object>();
    try {
        RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
        jmx.put("bootclasspath", mx.getBootClassPath());
        jmx.put("classpath", mx.getClassPath());

        // the input arguments passed to the Java virtual machine
        // which does not include the arguments to the main method.
        jmx.put("commandLineArgs", mx.getInputArguments());

        jmx.put("startTime", new Date(mx.getStartTime()));
        jmx.put("upTimeMS", mx.getUptime());

    } catch (Exception e) {
        LOGGER.warn("Error getting JMX properties", e);
    }
    jvm.put("jmx", jmx);
    return jvm;
}

From source file:jp.terasoluna.fw.batch.util.BatchUtil.java

/**
 * Java ?????? ??????/*from   w w w  . ja va2 s.  c  o m*/
 * @return Java ?
 */
public static String getMemoryInfo() {
    DecimalFormat f1 = new DecimalFormat("#,###KB");
    DecimalFormat f2 = new DecimalFormat("##.#");

    Runtime rt = Runtime.getRuntime();
    long free = rt.freeMemory() / 1024;
    long total = rt.totalMemory() / 1024;
    long max = rt.maxMemory() / 1024;
    long used = total - free;
    double ratio = used * 100 / (double) total;

    StringBuilder sb = new StringBuilder();

    sb.append("Java memory info : ");
    sb.append("used=");
    sb.append(f1.format(used));
    sb.append(" (");
    sb.append(f2.format(ratio));
    sb.append("%), ");
    sb.append("total=");
    sb.append(f1.format(total));
    sb.append(", ");
    sb.append("max=");
    sb.append(f1.format(max));

    return sb.toString();
}

From source file:com.github.nlloyd.hornofmongo.MongoScope.java

public static Object getMemInfo(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
    Scriptable memInfo = (Scriptable) MongoRuntime.call(new NewInstanceAction((MongoScope) thisObj, "Object"));

    Runtime runtime = Runtime.getRuntime();
    long freeMemory = runtime.freeMemory();
    long totalMemory = runtime.totalMemory();
    long maxMemory = runtime.maxMemory();

    ScriptableObject.putProperty(memInfo, "used", ((totalMemory - freeMemory) / 1024 / 1024));
    ScriptableObject.putProperty(memInfo, "total", (totalMemory / 1024 / 1024));
    ScriptableObject.putProperty(memInfo, "max", (maxMemory / 1024 / 1024));

    return memInfo;
}

From source file:com.toughra.mlearnplayer.EXEStrMgr.java

/**
 * This is used to log error messages/* w w  w  . j av  a  2s .  c o  m*/
 */
public static void lg(int errCode, String msg, Exception e) {
    if (e != null) {
        System.err.println(msg);
        e.printStackTrace();
        msg += "Exception: " + e.toString();
    }

    if (errCode > ERROR_LOG_THRESHOLD) {
        getInstance().l('D', errCode + ":" + msg, null, 0, 0, 0, 0, 0, null, 0, 0, null, null);
    }

    if (ERROR_LOG_THRESHOLD == 0) {
        //desperate debugging mode
        Runtime rt = Runtime.getRuntime();
        String memMsg = "Total Memory : " + rt.totalMemory() + " Free Memory " + rt.freeMemory();
        getInstance().l('D', memMsg, null, 0, 0, 0, 0, 0, null, 0, 0, null, null);

        try {
            getInstance().logOut.flush();
        } catch (IOException e2) {
            System.err.println("exception flushing log output");
        }
    }
}

From source file:org.lnicholls.galleon.gui.Galleon.java

private static void printSystemProperties() {
    Properties properties = System.getProperties();
    Enumeration Enumeration = properties.propertyNames();
    for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) {
        String propertyName = (String) e.nextElement();
        log.info(propertyName + "=" + System.getProperty(propertyName));
    }/*  w  ww. j  a v  a 2 s  .c om*/
    Runtime runtime = Runtime.getRuntime();
    log.info("Max Memory: " + runtime.maxMemory());
    log.info("Total Memory: " + runtime.totalMemory());
    log.info("Free Memory: " + runtime.freeMemory());
}