Example usage for java.lang Runtime maxMemory

List of usage examples for java.lang Runtime maxMemory

Introduction

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

Prototype

public native long maxMemory();

Source Link

Document

Returns the maximum amount of memory that the Java virtual machine will attempt to use.

Usage

From source file:ai.susi.server.api.susi.StatusService.java

@Override
public JSONObject serviceImpl(Query post, HttpServletResponse response, Authorization rights,
        JsonObjectWithDefault permissions) throws APIException {

    post.setResponse(response, "application/javascript");

    // generate json
    Runtime runtime = Runtime.getRuntime();
    JSONObject json = new JSONObject(true);
    JSONObject system = new JSONObject(true);
    system.put("assigned_memory", runtime.maxMemory());
    system.put("used_memory", runtime.totalMemory() - runtime.freeMemory());
    system.put("available_memory", runtime.maxMemory() - runtime.totalMemory() + runtime.freeMemory());
    system.put("cores", runtime.availableProcessors());
    system.put("threads", Thread.activeCount());
    system.put("runtime", System.currentTimeMillis() - Caretaker.startupTime);
    system.put("load_system_average", OS.getSystemLoadAverage());
    system.put("load_system_cpu", OS.getSystemCpuLoad());
    system.put("load_process_cpu", OS.getProcessCpuLoad());
    system.put("server_threads", SusiServer.getServerThreads());
    system.put("server_uri", SusiServer.getServerURI());

    JSONObject index = new JSONObject(true);
    JSONObject messages = new JSONObject(true);
    JSONObject queue = new JSONObject(true);
    messages.put("queue", queue);
    JSONObject users = new JSONObject(true);
    JSONObject queries = new JSONObject(true);
    JSONObject accounts = new JSONObject(true);
    JSONObject user = new JSONObject(true);
    index.put("messages", messages);
    index.put("users", users);
    index.put("queries", queries);
    index.put("accounts", accounts);
    index.put("user", user);

    JSONObject client_info = new JSONObject(true);
    client_info.put("RemoteHost", post.getClientHost());
    client_info.put("IsLocalhost", post.isLocalhostAccess() ? "true" : "false");

    JSONObject request_header = new JSONObject(true);
    Enumeration<String> he = post.getRequest().getHeaderNames();
    while (he.hasMoreElements()) {
        String h = he.nextElement();
        request_header.put(h, post.getRequest().getHeader(h));
    }/*from w w w.  j  a  v  a 2  s .c om*/
    client_info.put("request_header", request_header);

    json.put("system", system);
    json.put("index", index);
    json.put("client_info", client_info);

    return json;
}

From source file:org.openflamingo.web.util.VersionConfigurer.java

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    Log4jWebConfigurer.initLogging(servletContextEvent.getServletContext());

    Properties properties = new Properties();
    ServletContext context = servletContextEvent.getServletContext();
    InputStream inputStream = null;
    try {//from w ww . j a v  a2 s .com
        inputStream = context.getResourceAsStream("/WEB-INF/version.properties");
        properties.load(inputStream);
    } catch (Exception ex) {
        throw new IllegalArgumentException("Cannot load a '/WEB/INF/version.properties' file.", ex);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

    StringBuilder builder = new StringBuilder();

    printHeader(builder, "Application Information");
    Properties appProps = new Properties();
    appProps.put("Application", "Flamingo Workflow Engine");
    appProps.put("Version", properties.get("version"));
    appProps.put("Build Date", properties.get("build.timestamp"));
    appProps.put("Build Number", properties.get("build.number"));
    appProps.put("Revision Number", properties.get("revision.number"));
    appProps.put("Build Key", properties.get("build.key"));

    if (context != null) {
        appProps.put("Application Server", context.getServerInfo() + " - Servlet API "
                + context.getMajorVersion() + "." + context.getMinorVersion());
    }

    Properties systemProperties = System.getProperties();
    appProps.put("Java Version", systemProperties.getProperty("java.version", UNKNOWN) + " - "
            + systemProperties.getProperty("java.vendor", UNKNOWN));
    appProps.put("Current Working Directory", systemProperties.getProperty("user.dir", UNKNOWN));

    print(builder, appProps);

    Properties memPros = new Properties();
    final Runtime rt = Runtime.getRuntime();
    final long maxMemory = rt.maxMemory() / MEGA_BYTES;
    final long totalMemory = rt.totalMemory() / MEGA_BYTES;
    final long freeMemory = rt.freeMemory() / MEGA_BYTES;
    final long usedMemory = totalMemory - freeMemory;

    memPros.put("Maximum Allowable Memory", maxMemory + "MB");
    memPros.put("Total Memory", totalMemory + "MB");
    memPros.put("Free Memory", freeMemory + "MB");
    memPros.put("Used Memory", usedMemory + "MB");

    print(builder, memPros);

    printHeader(builder, "Java System Properties");
    Properties sysProps = new Properties();
    for (final Map.Entry<Object, Object> entry : systemProperties.entrySet()) {
        sysProps.put(entry.getKey(), entry.getValue());
    }

    print(builder, sysProps);

    printHeader(builder, "System Environments");
    Map<String, String> getenv = System.getenv();
    Properties envProps = new Properties();
    Set<String> strings = getenv.keySet();
    for (String key : strings) {
        String message = getenv.get(key);
        envProps.put(key, message);
    }

    print(builder, envProps);

    logger.info("=================================================");
    logger.info(" Flamingo Web Services starting...");
    logger.info("=================================================\n{}", builder.toString());
}

From source file:com.liferay.portal.events.LogMemoryUsageAction.java

public void run(HttpServletRequest req, HttpServletResponse res) throws ActionException {

    Runtime runtime = Runtime.getRuntime();

    NumberFormat nf = NumberFormat.getInstance();

    String freeMemory = nf.format(runtime.freeMemory());
    String totalMemory = nf.format(runtime.totalMemory());
    String maxMemory = nf.format(runtime.maxMemory());

    _log.debug("Memory Usage:\t" + freeMemory + "\t" + totalMemory + "\t" + maxMemory);
}

From source file:org.exem.flamingo.web.util.ApplicationInformationDisplayContextListener.java

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    System.setProperty("PID", SystemUtils.getPid());

    Properties properties = new Properties();
    ServletContext context = servletContextEvent.getServletContext();
    InputStream inputStream = null;
    try {/*w ww.  j  a v  a  2  s. c om*/
        inputStream = context.getResourceAsStream("/WEB-INF/app.properties");
        properties.load(inputStream);
    } catch (Exception ex) {
        throw new IllegalArgumentException("Cannot load a '/WEB/INF/app.properties' file.", ex);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

    // See : http://patorjk.com/software/taag/#p=display&f=Slant&t=Flamingo
    StringBuilder builder = new StringBuilder();
    builder.append("    ________                _                 \n"
            + "   / ____/ /___ _____ ___  (_)___  ____ _____ \n"
            + "  / /_  / / __ `/ __ `__ \\/ / __ \\/ __ `/ __ \\\n"
            + " / __/ / / /_/ / / / / / / / / / / /_/ / /_/ /\n"
            + "/_/   /_/\\__,_/_/ /_/ /_/_/_/ /_/\\__, /\\____/ \n"
            + "                                /____/        ");

    printHeader(builder, "Application Information");
    Properties appProps = new Properties();
    appProps.put("Instance", StringUtils.isEmpty(System.getProperty("instance")) ? "** UNKNOWN **"
            : System.getProperty("instance"));
    appProps.put("Version", properties.get("version"));
    appProps.put("Build Date", properties.get("build.timestamp"));
    appProps.put("Build Number", properties.get("build.number"));
    appProps.put("Revision Number", properties.get("revision.number"));
    appProps.put("Organization", properties.get("organization"));
    appProps.put("Homepage", properties.get("homepage"));

    if (context != null) {
        appProps.put("Application Server", context.getServerInfo() + " - Servlet API "
                + context.getMajorVersion() + "." + context.getMinorVersion());
    }

    Properties systemProperties = System.getProperties();
    appProps.put("Java Version", systemProperties.getProperty("java.version", UNKNOWN) + " - "
            + systemProperties.getProperty("java.vendor", UNKNOWN));
    appProps.put("Current Working Directory", systemProperties.getProperty("user.dir", UNKNOWN));

    print(builder, appProps);

    Properties memPros = new Properties();
    final Runtime rt = Runtime.getRuntime();
    final long maxMemory = rt.maxMemory() / MEGA_BYTES;
    final long totalMemory = rt.totalMemory() / MEGA_BYTES;
    final long freeMemory = rt.freeMemory() / MEGA_BYTES;
    final long usedMemory = totalMemory - freeMemory;

    memPros.put("Maximum Allowable Memory", maxMemory + "MB");
    memPros.put("Total Memory", totalMemory + "MB");
    memPros.put("Free Memory", freeMemory + "MB");
    memPros.put("Used Memory", usedMemory + "MB");

    print(builder, memPros);

    printHeader(builder, "Java System Properties");
    Properties sysProps = new Properties();
    for (final Map.Entry<Object, Object> entry : systemProperties.entrySet()) {
        sysProps.put(entry.getKey(), entry.getValue());
    }

    print(builder, sysProps);

    printHeader(builder, "System Environments");
    Map<String, String> envs = System.getenv();
    Properties envProps = new Properties();
    Set<String> strings = envs.keySet();
    for (String key : strings) {
        String message = envs.get(key);
        envProps.put(key, message);
    }

    print(builder, envProps);

    WebLogbackConfigurer.initLogging(servletContextEvent.getServletContext());

    System.out.println(builder.toString());

    logger.info("============================================================");
    logger.info(" Now starting ..... PID: " + SystemUtils.getPid());
    logger.info("============================================================");
}

From source file:org.apache.pulsar.common.stats.JvmMetrics.java

public List<Metrics> generate() {

    Metrics m = createMetrics();//from  ww  w.  j av a  2 s. c  om

    Runtime r = Runtime.getRuntime();

    m.put("jvm_heap_used", r.totalMemory() - r.freeMemory());
    m.put("jvm_max_memory", r.maxMemory());
    m.put("jvm_total_memory", r.totalMemory());

    m.put("jvm_direct_memory_used", getJvmDirectMemoryUsed());
    m.put("jvm_max_direct_memory", PlatformDependent.maxDirectMemory());
    m.put("jvm_thread_cnt", getThreadCount());

    this.gcLogger.logMetrics(m);

    long totalAllocated = 0;
    long totalUsed = 0;

    for (PoolArenaMetric arena : PooledByteBufAllocator.DEFAULT.directArenas()) {
        for (PoolChunkListMetric list : arena.chunkLists()) {
            for (PoolChunkMetric chunk : list) {
                int size = chunk.chunkSize();
                int used = size - chunk.freeBytes();

                totalAllocated += size;
                totalUsed += used;
            }
        }
    }

    m.put(this.componentName + "_default_pool_allocated", totalAllocated);
    m.put(this.componentName + "_default_pool_used", totalUsed);

    return Lists.newArrayList(m);
}

From source file:com.liferay.portal.events.GarbageCollectorAction.java

public void run(HttpSession ses) throws ActionException {

    try {//www  .  j a v a  2  s .  c  o  m
        Runtime runtime = Runtime.getRuntime();

        NumberFormat nf = NumberFormat.getInstance();

        _log.debug("Before GC: " + nf.format(runtime.freeMemory()) + " free, "
                + nf.format(runtime.totalMemory()) + " total, and " + nf.format(runtime.maxMemory()) + " max");

        _log.debug("Running garbage collector");

        System.gc();

        _log.debug("After GC: " + nf.format(runtime.freeMemory()) + " free, " + nf.format(runtime.totalMemory())
                + " total, and " + nf.format(runtime.maxMemory()) + " max");
    } catch (Exception e) {
        throw new ActionException(e);
    }
}

From source file:org.openflamingo.engine.util.VersionConfigurer.java

/**
 * Notification that the web application initialization
 * process is starting.//from ww  w  .  j ava  2 s .  c  o  m
 * All ServletContextListeners are notified of context
 * initialization before any filter or servlet in the web
 * application is initialized.
 *
 * @param servletContextEvent {@link javax.servlet.ServletContextEvent}
 */
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
    Log4jWebConfigurer.initLogging(servletContextEvent.getServletContext());

    Properties properties = new Properties();
    ServletContext context = servletContextEvent.getServletContext();
    InputStream inputStream = null;
    try {
        inputStream = context.getResourceAsStream("/WEB-INF/version.properties");
        properties.load(inputStream);
    } catch (Exception ex) {
        throw new IllegalArgumentException("Cannot load a '/WEB/INF/version.properties' file.", ex);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

    StringBuilder builder = new StringBuilder();

    printHeader(builder, "Application Information");
    Properties appProps = new Properties();
    appProps.put("Application", "Flamingo Workflow Engine");
    appProps.put("Version", properties.get("version"));
    appProps.put("Build Date", properties.get("build.timestamp"));
    appProps.put("Build Number", properties.get("build.number"));
    appProps.put("Revision Number", properties.get("revision.number"));
    appProps.put("Build Key", properties.get("build.key"));

    if (context != null) {
        appProps.put("Application Server", context.getServerInfo() + " - Servlet API "
                + context.getMajorVersion() + "." + context.getMinorVersion());
    }

    Properties systemProperties = System.getProperties();
    appProps.put("Java Version", systemProperties.getProperty("java.version", UNKNOWN) + " - "
            + systemProperties.getProperty("java.vendor", UNKNOWN));
    appProps.put("Current Working Directory", systemProperties.getProperty("user.dir", UNKNOWN));

    print(builder, appProps);

    printHeader(builder, "JVM Heap Information");

    Properties memPros = new Properties();
    final Runtime rt = Runtime.getRuntime();
    final long maxMemory = rt.maxMemory() / MEGA_BYTES;
    final long totalMemory = rt.totalMemory() / MEGA_BYTES;
    final long freeMemory = rt.freeMemory() / MEGA_BYTES;
    final long usedMemory = totalMemory - freeMemory;

    memPros.put("Maximum Allowable Memory", maxMemory + "MB");
    memPros.put("Total Memory", totalMemory + "MB");
    memPros.put("Free Memory", freeMemory + "MB");
    memPros.put("Used Memory", usedMemory + "MB");

    print(builder, memPros);

    printHeader(builder, "Java System Properties");
    Properties sysProps = new Properties();
    for (final Map.Entry<Object, Object> entry : systemProperties.entrySet()) {
        sysProps.put(entry.getKey(), entry.getValue());
    }

    print(builder, sysProps);

    printHeader(builder, "System Environments");
    Map<String, String> getenv = System.getenv();
    Properties envProps = new Properties();
    Set<String> strings = getenv.keySet();
    for (String key : strings) {
        String message = getenv.get(key);
        envProps.put(key, message);
    }

    print(builder, envProps);

    logger.info("=================================================");
    logger.info(" Flamingo Workflow Engine starting...");
    logger.info("=================================================\n{}", builder.toString());
}

From source file:com.ikon.servlet.admin.StatsGraphServlet.java

/**
 * Generate memory statistics/*from   w  ww.j  a v a  2s  .c  o m*/
 * http://blog.codebeach.com/2008/02/determine-available-memory-in-java.html
 */
public JFreeChart jvmMemStats() throws IOException, ServletException {
    Runtime runtime = Runtime.getRuntime();
    long max = runtime.maxMemory(); // maximum amount of memory that the JVM will attempt to use
    long available = runtime.totalMemory(); // total amount of memory in the JVM
    long free = runtime.freeMemory(); // amount of free memory in the JVM
    long used = max - available;
    long total = free + used;
    String title = "JVM memory: " + FormatUtil.formatSize(total);

    log.debug("JVM maximun memory: {}", FormatUtil.formatSize(max));
    log.debug("JVM available memory: {}", FormatUtil.formatSize(available));
    log.debug("JVM free memory: {}", FormatUtil.formatSize(free));
    log.debug("JVM used memory: {}", FormatUtil.formatSize(used));
    log.debug("JVM total memory: {}", FormatUtil.formatSize(total));

    DefaultPieDataset dataset = new DefaultPieDataset();
    dataset.setValue("Available (" + FormatUtil.formatSize(free) + ")", free * 100 / total);
    dataset.setValue("Used (" + FormatUtil.formatSize(used) + ")", used * 100 / total);

    return ChartFactory.createPieChart(title, dataset, true, false, false);
}

From source file:io.cloudslang.worker.management.monitor.WorkerMonitorsImpl.java

@Override
public synchronized Map<WorkerMonitorInfoEnum, Serializable> getMonitorInfo() {
    try {/*from ww  w .j a  v a 2s.c o m*/
        Map<WorkerMonitorInfoEnum, Serializable> monitorInfo = Maps.newHashMap();

        Runtime runtime = Runtime.getRuntime();
        monitorInfo.put(WorkerMonitorInfoEnum.TOTAL_MEMORY, runtime.totalMemory());
        monitorInfo.put(WorkerMonitorInfoEnum.FREE_MEMORY, runtime.freeMemory());
        monitorInfo.put(WorkerMonitorInfoEnum.MAX_MEMORY, runtime.maxMemory());

        monitorInfo.put(WorkerMonitorInfoEnum.WORKER_ID, workerManager.getWorkerUuid());

        monitorInfo.put(WorkerMonitorInfoEnum.EXECUTION_THREADS_AMOUNT,
                workerManager.getExecutionThreadsCount());

        monitorInfo.put(WorkerMonitorInfoEnum.OUTBUFFER_CAPACITY, outBuffer.getCapacity());

        monitorInfo.put(WorkerMonitorInfoEnum.INBUFFER_CAPACITY, inBuffer.getCapacity());

        for (WorkerMonitor monitor : monitors) {
            monitor.captureMonitorInfo(monitorInfo);
        }

        monitorInfo.put(WorkerMonitorInfoEnum.MONITOR_START_TIME, monitorStartTime);
        monitorInfo.put(WorkerMonitorInfoEnum.MONITOR_END_TIME, System.currentTimeMillis());

        return monitorInfo;
    } finally {
        resetMonitor();
    }
}

From source file:org.b3log.symphony.processor.StatusProcessor.java

/**
 * Reports running status.//from w ww  . j  a v a2 s .co m
 *
 * @param context the specified context
 * @param request the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/status", method = HTTPRequestMethod.GET)
public void reportStatus(final HTTPRequestContext context, final HttpServletRequest request,
        final HttpServletResponse response) throws Exception {
    final String key = Symphonys.get("keyOfSymphony");
    if (!key.equals(request.getParameter("key"))) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);

        return;
    }

    final JSONObject ret = new JSONObject();

    context.renderJSON(ret);

    ret.put(Common.ONLINE_VISITOR_CNT, optionQueryService.getOnlineVisitorCount());
    ret.put(Common.ARTICLE_CHANNEL_CNT, ArticleChannel.SESSIONS.size());
    ret.put(Common.ARTICLE_LIST_CHANNEL_CNT, ArticleListChannel.SESSIONS.size());

    final JSONObject memory = new JSONObject();
    ret.put("memory", memory);

    final int mb = 1024 * 1024;
    final Runtime runtime = Runtime.getRuntime();
    memory.put("totoal", runtime.totalMemory() / mb);
    memory.put("free", runtime.freeMemory() / mb);
    memory.put("used", (runtime.totalMemory() - runtime.freeMemory()) / mb);
    memory.put("max", runtime.maxMemory() / mb);

    LOGGER.info(ret.toString(SymphonyServletListener.JSON_PRINT_INDENT_FACTOR));
    ret.put(Keys.STATUS_CODE, true);
}