Example usage for java.lang.management MemoryPoolMXBean getName

List of usage examples for java.lang.management MemoryPoolMXBean getName

Introduction

In this page you can find the example usage for java.lang.management MemoryPoolMXBean getName.

Prototype

public String getName();

Source Link

Document

Returns the name representing this memory pool.

Usage

From source file:MainClass.java

public static void premain(final Instrumentation inst) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
                PrintWriter out = new PrintWriter(System.err);

                ThreadMXBean tb = ManagementFactory.getThreadMXBean();
                out.printf("Current thread count: %d%n", tb.getThreadCount());
                out.printf("Peak thread count: %d%n", tb.getPeakThreadCount());

                List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
                for (MemoryPoolMXBean pool : pools) {
                    MemoryUsage peak = pool.getPeakUsage();
                    out.printf("Peak %s memory used: %,d%n", pool.getName(), peak.getUsed());
                    out.printf("Peak %s memory reserved: %,d%n", pool.getName(), peak.getCommitted());
                }/*from   w  ww  .  j a  v  a2 s .  c  o  m*/

                Class[] loaded = inst.getAllLoadedClasses();
                out.println("Loaded classes:");
                for (Class c : loaded)
                    out.println(c.getName());
                out.close();
            } catch (Throwable t) {
                System.err.println("Exception in agent: " + t);
            }
        }
    });
}

From source file:com.thoughtworks.go.server.service.support.ServerRuntimeInformationProvider.java

private void poolInfo(InformationStringBuilder builder) {
    builder.addSection("Memory pool information");

    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        builder.append(String.format("Name: %s, Type: %s\n%s\n", memoryPoolMXBean.getName(),
                memoryPoolMXBean.getType(), format(memoryPoolMXBean.getUsage())));
    }/*w  ww  .  ja  va 2  s  .co  m*/
}

From source file:VerboseGC.java

/**
 * Prints the verbose GC log to System.out to list the memory usage of all
 * memory pools as well as the GC statistics.
 *//*ww  w . j a  va  2  s .  c om*/
public void printVerboseGc() {
    System.out.print("Uptime: " + formatMillis(rmbean.getUptime()));
    for (GarbageCollectorMXBean gc : gcmbeans) {
        System.out.print(" [" + gc.getName() + ": ");
        System.out.print("Count=" + gc.getCollectionCount());
        System.out.print(" GCTime=" + formatMillis(gc.getCollectionTime()));
        System.out.print("]");
    }
    System.out.println();
    for (MemoryPoolMXBean p : pools) {
        System.out.print("  [" + p.getName() + ":");
        MemoryUsage u = p.getUsage();
        System.out.print(" Used=" + formatBytes(u.getUsed()));
        System.out.print(" Committed=" + formatBytes(u.getCommitted()));
        System.out.println("]");
    }
}

From source file:controllers.api.core.RootApiController.java

/**
 * Return some information regarding the memory status of the instance.
 *//*  w  w  w  . ja v a2s .co m*/
@ApiAuthentication(onlyRootKey = true)
public Result instanceMemoryStatus() {

    try {

        RootResponse response = new RootResponse();

        response.attributes = new HashMap<String, JsonNode>();
        List<MemoryPoolMXBean> mbeans = ManagementFactory.getMemoryPoolMXBeans();
        if (mbeans != null) {
            for (MemoryPoolMXBean mbean : mbeans) {
                System.out.println(mbean.getName());
                MemoryUsage memUsage = mbean.getUsage();
                HashMap<String, Long> memoryUsageAsMap = new HashMap<String, Long>();
                memoryUsageAsMap.put("init", memUsage.getInit());
                memoryUsageAsMap.put("max", memUsage.getMax());
                memoryUsageAsMap.put("committed", memUsage.getCommitted());
                memoryUsageAsMap.put("used", memUsage.getUsed());
                response.attributes.put(mbean.getName(), Json.toJson(memoryUsageAsMap));
            }
        }

        return getJsonSuccessResponse(response);

    } catch (Exception e) {

        return getJsonErrorResponse(new ApiError(500, "INTERNAL SERVER ERROR", e));

    }
}

From source file:edu.usu.sdl.openstorefront.web.rest.service.Application.java

@GET
@RequireAdmin//  w  w w.j  a  va 2  s  .com
@APIDescription("Gets the application system status")
@Produces({ MediaType.APPLICATION_JSON })
@DataType(ApplicationStatus.class)
@Path("/status")
public Response getApplicationStatus() {
    OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    List<GarbageCollectorMXBean> garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

    ApplicationStatus applicationStatus = new ApplicationStatus();
    applicationStatus.setApplicationVersion(PropertiesManager.getApplicationVersion());
    applicationStatus.setProcessorCount(operatingSystemMXBean.getAvailableProcessors());
    applicationStatus.setSystemLoad(operatingSystemMXBean.getSystemLoadAverage());
    applicationStatus.setSystemProperties(runtimeMXBean.getSystemProperties());

    applicationStatus.getHeapMemoryStatus().setName("Heap");
    applicationStatus.getHeapMemoryStatus().setDetails(memoryMXBean.getHeapMemoryUsage().toString());
    applicationStatus.getHeapMemoryStatus()
            .setInitKb(memoryMXBean.getHeapMemoryUsage().getInit() != 0
                    ? memoryMXBean.getHeapMemoryUsage().getInit() / 1024
                    : 0);
    applicationStatus.getHeapMemoryStatus()
            .setUsedKb(memoryMXBean.getHeapMemoryUsage().getUsed() != 0
                    ? memoryMXBean.getHeapMemoryUsage().getUsed() / 1024
                    : 0);
    applicationStatus.getHeapMemoryStatus()
            .setMaxKb(memoryMXBean.getHeapMemoryUsage().getMax() != 0
                    ? memoryMXBean.getHeapMemoryUsage().getMax() / 1024
                    : 0);
    applicationStatus.getHeapMemoryStatus()
            .setCommitedKb(memoryMXBean.getHeapMemoryUsage().getCommitted() != 0
                    ? memoryMXBean.getHeapMemoryUsage().getCommitted() / 1024
                    : 0);

    applicationStatus.getNonHeapMemoryStatus().setName("Non-Heap");
    applicationStatus.getNonHeapMemoryStatus().setDetails(memoryMXBean.getNonHeapMemoryUsage().toString());
    applicationStatus.getNonHeapMemoryStatus()
            .setInitKb(memoryMXBean.getNonHeapMemoryUsage().getInit() != 0
                    ? memoryMXBean.getNonHeapMemoryUsage().getInit() / 1024
                    : 0);
    applicationStatus.getNonHeapMemoryStatus()
            .setUsedKb(memoryMXBean.getNonHeapMemoryUsage().getUsed() != 0
                    ? memoryMXBean.getNonHeapMemoryUsage().getUsed() / 1024
                    : 0);
    applicationStatus.getNonHeapMemoryStatus()
            .setMaxKb(memoryMXBean.getNonHeapMemoryUsage().getMax() != 0
                    ? memoryMXBean.getNonHeapMemoryUsage().getMax() / 1024
                    : 0);
    applicationStatus.getNonHeapMemoryStatus()
            .setCommitedKb(memoryMXBean.getNonHeapMemoryUsage().getCommitted() != 0
                    ? memoryMXBean.getNonHeapMemoryUsage().getCommitted() / 1024
                    : 0);

    applicationStatus.setLiveThreadCount(threadMXBean.getThreadCount());
    applicationStatus.setTotalThreadCount(threadMXBean.getTotalStartedThreadCount());

    applicationStatus.setStartTime(new Date(runtimeMXBean.getStartTime()));
    applicationStatus.setUpTime(TimeUtil.millisToString(runtimeMXBean.getUptime()));

    for (GarbageCollectorMXBean garbageCollectorMXBean : garbageCollectorMXBeans) {
        applicationStatus.getGarbageCollectionInfos()
                .add(TimeUtil.millisToString(garbageCollectorMXBean.getCollectionTime()) + " - ("
                        + garbageCollectorMXBean.getCollectionCount() + ")");
    }

    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        MemoryPoolStatus memoryPoolStatus = new MemoryPoolStatus();
        memoryPoolStatus.setName(memoryPoolMXBean.getName() + " - " + memoryPoolMXBean.getType());
        memoryPoolStatus.setDetails(memoryPoolMXBean.getUsage().toString());
        memoryPoolStatus.setInitKb(
                memoryPoolMXBean.getUsage().getInit() != 0 ? memoryPoolMXBean.getUsage().getInit() / 1024 : 0);
        memoryPoolStatus.setUsedKb(
                memoryPoolMXBean.getUsage().getUsed() != 0 ? memoryPoolMXBean.getUsage().getUsed() / 1024 : 0);
        memoryPoolStatus.setMaxKb(
                memoryPoolMXBean.getUsage().getMax() != 0 ? memoryPoolMXBean.getUsage().getMax() / 1024 : 0);
        memoryPoolStatus.setCommitedKb(memoryPoolMXBean.getUsage().getCommitted() != 0
                ? memoryPoolMXBean.getUsage().getCommitted() / 1024
                : 0);

        applicationStatus.getMemoryPools().add(memoryPoolStatus);
    }

    return sendSingleEntityResponse(applicationStatus);
}

From source file:com.eurelis.opencms.admin.systeminformation.CmsMemoryOverviewDialog.java

/**
 * Creates the list of widgets for this dialog.<p>
 *//*  w w  w  .  ja v a2  s  .  c om*/
protected void defineWidgets() {

    // initialize the cache object to use for the dialog
    initInfosObject();

    setKeyPrefix(KEY_PREFIX);

    // widgets to display
    addWidget(new CmsWidgetDialogParameter(m_adminSettings, "interval", PAGES[0], new CmsDisplayWidget()));

    for (java.lang.management.MemoryPoolMXBean item : ManagementFactory.getMemoryPoolMXBeans()) {
        String name = item.getName();
        if (name.toLowerCase().contains("perm")) {
            addWidget(new CmsWidgetDialogParameter(m_adminSettings, "displayMemPerm", PAGES[0],
                    new CmsCheckboxWidget()));
        } else if (name.toLowerCase().contains("old")) {
            addWidget(new CmsWidgetDialogParameter(m_adminSettings, "displayMemOld", PAGES[0],
                    new CmsCheckboxWidget()));
        } else if (name.toLowerCase().contains("eden")) {
            addWidget(new CmsWidgetDialogParameter(m_adminSettings, "displayMemEden", PAGES[0],
                    new CmsCheckboxWidget()));
        } else if (name.toLowerCase().contains("survivor")) {
            addWidget(new CmsWidgetDialogParameter(m_adminSettings, "displayMemSurvivor", PAGES[0],
                    new CmsCheckboxWidget()));
        }
    }

    // widgets to display
    int lineNumber = 5;
    int countItem = 0;
    for (java.lang.management.MemoryPoolMXBean item : ManagementFactory.getMemoryPoolMXBeans()) {
        String name = item.getName();

        if (name.toLowerCase().contains("perm")) {
            if (m_adminSettings.getDisplayMemPerm()) {
                addWidget(new CmsWidgetDialogParameter(this, "memPermMax", PAGES[0], new CmsDisplayWidget()));
                addWidget(new CmsWidgetDialogParameter(this, "memPermTotal", PAGES[0], new CmsDisplayWidget()));
                addWidget(new CmsWidgetDialogParameter(this, "memPermUsed", PAGES[0], new CmsDisplayWidget()));
                lineNumber = lineNumber + 3;
                countItem++;
            }
        } else if (name.toLowerCase().contains("old")) {
            if (m_adminSettings.getDisplayMemOld()) {
                addWidget(new CmsWidgetDialogParameter(this, "memOldMax", PAGES[0], new CmsDisplayWidget()));
                addWidget(new CmsWidgetDialogParameter(this, "memOldTotal", PAGES[0], new CmsDisplayWidget()));
                addWidget(new CmsWidgetDialogParameter(this, "memOldUsed", PAGES[0], new CmsDisplayWidget()));
                lineNumber = lineNumber + 3;
                countItem++;
            }
        } else if (name.toLowerCase().contains("eden")) {
            if (m_adminSettings.getDisplayMemEden()) {
                addWidget(new CmsWidgetDialogParameter(this, "memEdenMax", PAGES[0], new CmsDisplayWidget()));
                addWidget(new CmsWidgetDialogParameter(this, "memEdenTotal", PAGES[0], new CmsDisplayWidget()));
                addWidget(new CmsWidgetDialogParameter(this, "memEdenUsed", PAGES[0], new CmsDisplayWidget()));
                lineNumber = lineNumber + 3;
                countItem++;
            }
        } else if (name.toLowerCase().contains("survivor")) {
            if (m_adminSettings.getDisplayMemSurvivor()) {
                //if(CmsAdminSettings.getSettingsDisplayMemSurvivorValue(getCms(), getSession())){
                addWidget(
                        new CmsWidgetDialogParameter(this, "memSurvivorMax", PAGES[0], new CmsDisplayWidget()));
                addWidget(new CmsWidgetDialogParameter(this, "memSurvivorTotal", PAGES[0],
                        new CmsDisplayWidget()));
                addWidget(new CmsWidgetDialogParameter(this, "memSurvivorUsed", PAGES[0],
                        new CmsDisplayWidget()));
                lineNumber = lineNumber + 3;
                countItem++;
            }
        } else {
            //LOG.debug("MemoryPoolMXBean name = " + name.toLowerCase());
        }

    }
}

From source file:com.eurelis.opencms.admin.systeminformation.CmsMemoryOverviewDialog.java

/**
 * Initializes the infos object.<p>
 *//*from   w w w .jav a  2  s .c  o m*/
protected void initInfosObject() {

    com.sun.management.OperatingSystemMXBean sunOsBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory
            .getOperatingSystemMXBean();
    java.lang.management.OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
    java.lang.management.ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    java.lang.management.RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
    java.lang.management.ClassLoadingMXBean classesBean = ManagementFactory.getClassLoadingMXBean();

    for (java.lang.management.MemoryPoolMXBean item : ManagementFactory.getMemoryPoolMXBeans()) {
        java.lang.management.MemoryUsage mu = item.getUsage();
        String name = item.getName();

        if (name.toLowerCase().contains("perm")) {
            setMemPermMax("" + mu.getMax());
            setMemPermTotal("" + mu.getCommitted());
            setMemPermUsed("" + mu.getUsed());
        } else if (name.toLowerCase().contains("old")) {
            setMemOldMax("" + mu.getMax());
            setMemOldTotal("" + mu.getCommitted());
            setMemOldUsed("" + mu.getUsed());
        } else if (name.toLowerCase().contains("eden")) {
            setMemEdenMax("" + mu.getMax());
            setMemEdenTotal("" + mu.getCommitted());
            setMemEdenUsed("" + mu.getUsed());
        } else if (name.toLowerCase().contains("survivor")) {
            setMemSurvivorMax("" + mu.getMax());
            setMemSurvivorTotal("" + mu.getCommitted());
            setMemSurvivorUsed("" + mu.getUsed());
        } else {
            //LOG.debug("MemoryPoolMXBean name = " + name.toLowerCase());
        }
    }

    Object o;
    if (CmsStringUtil.isEmpty(getParamAction())) {
        o = new CmsAdminSettings(getSession());
    } else {
        // this is not the initial call, get the job object from session
        o = getDialogObject();
    }
    if (!(o instanceof CmsAdminSettings)) {
        // create a new history settings handler object
        m_adminSettings = new CmsAdminSettings(getSession());
    } else {
        // reuse html import handler object stored in session
        m_adminSettings = (CmsAdminSettings) o;
    }

    setParamCloseLink(getJsp()
            .link("/system/workplace/views/admin/admin-main.jsp?path=/eurelis_system_information/memory.jsp"));
}

From source file:com.eurelis.opencms.admin.systeminformation.CmsMemoryOverviewDialog.java

/**
 * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
 * /*from www  .  j  a v a  2 s.co m*/
 * This overwrites the method from the super class to create a layout variation for the widgets.<p>
 * 
 * @param dialog the dialog (page) to get the HTML for
 * @return the dialog HTML for all defined widgets of the named dialog (page)
 */
protected String createDialogHtml(String dialog) {

    StringBuffer result = new StringBuffer(1024);

    // create widget table
    result.append(createWidgetTableStart());

    // show error header once if there were validation errors
    result.append(createWidgetErrorHeader());

    if (dialog.equals(PAGES[0])) {

        //settings
        result.append(dialogBlockStart(key(Messages.GUI_SYSTEMINFORMATION_MEMORY_ADMIN_TOOL_BLOCK_SETTINGS)));
        result.append(createWidgetTableStart());
        result.append(createDialogRowsHtml(0, 0));

        int lineNumber = 1;

        String listOfMemories = "";
        for (java.lang.management.MemoryPoolMXBean item : ManagementFactory.getMemoryPoolMXBeans()) {
            String name = item.getName();
            listOfMemories = listOfMemories + name + ",";
            if (name.toLowerCase().contains("perm")) {
                result.append(createDialogRowsHtml(lineNumber, lineNumber));
                lineNumber = lineNumber + 1;
            } else if (name.toLowerCase().contains("old")) {
                result.append(createDialogRowsHtml(lineNumber, lineNumber));
                lineNumber = lineNumber + 1;
            } else if (name.toLowerCase().contains("eden")) {
                result.append(createDialogRowsHtml(lineNumber, lineNumber));
                lineNumber = lineNumber + 1;
            } else if (name.toLowerCase().contains("survivor")) {
                result.append(createDialogRowsHtml(lineNumber, lineNumber));
                lineNumber = lineNumber + 1;
            }
        }
        LOG.debug("createDialogHtml() :: listOfMemories = " + listOfMemories);

        result.append(createWidgetTableEnd());
        result.append(dialogBlockEnd());

        // create the widgets for the first dialog page
        for (java.lang.management.MemoryPoolMXBean item : ManagementFactory.getMemoryPoolMXBeans()) {
            String name = item.getName();

            if (name.toLowerCase().contains("perm")) {
                if (m_adminSettings.getDisplayMemPerm()) {
                    result.append(dialogBlockStart(
                            key(Messages.GUI_SYSTEMINFORMATION_MEMORY_ADMIN_TOOL_BLOCK_) + name.toUpperCase()));
                    result.append(createWidgetTableStart());
                    result.append(createDialogRowsHtml(lineNumber, lineNumber + 2));
                    result.append(createWidgetTableEnd());
                    result.append(dialogBlockEnd());
                    lineNumber = lineNumber + 3;
                }
            } else if (name.toLowerCase().contains("old")) {
                if (m_adminSettings.getDisplayMemOld()) {
                    result.append(dialogBlockStart(
                            key(Messages.GUI_SYSTEMINFORMATION_MEMORY_ADMIN_TOOL_BLOCK_) + name.toUpperCase()));
                    result.append(createWidgetTableStart());
                    result.append(createDialogRowsHtml(lineNumber, lineNumber + 2));
                    result.append(createWidgetTableEnd());
                    result.append(dialogBlockEnd());
                    lineNumber = lineNumber + 3;
                }
            } else if (name.toLowerCase().contains("eden")) {
                if (m_adminSettings.getDisplayMemEden()) {
                    result.append(dialogBlockStart(
                            key(Messages.GUI_SYSTEMINFORMATION_MEMORY_ADMIN_TOOL_BLOCK_) + name.toUpperCase()));
                    result.append(createWidgetTableStart());
                    result.append(createDialogRowsHtml(lineNumber, lineNumber + 2));
                    result.append(createWidgetTableEnd());
                    result.append(dialogBlockEnd());
                    lineNumber = lineNumber + 3;
                }
            } else if (name.toLowerCase().contains("survivor")) {
                LOG.debug("createDialogHtml() :: m_adminSettings.getDisplayMemSurvivor() = "
                        + m_adminSettings.getDisplayMemSurvivor());
                if (m_adminSettings.getDisplayMemSurvivor()) {
                    //if(CmsAdminSettings.getSettingsDisplayMemSurvivorValue(getCms(), getSession())){
                    result.append(dialogBlockStart(
                            key(Messages.GUI_SYSTEMINFORMATION_MEMORY_ADMIN_TOOL_BLOCK_) + name.toUpperCase()));
                    result.append(createWidgetTableStart());
                    result.append(createDialogRowsHtml(lineNumber, lineNumber + 2));
                    result.append(createWidgetTableEnd());
                    result.append(dialogBlockEnd());
                    lineNumber = lineNumber + 3;
                }
            }

        }
    }

    // close widget table
    result.append(createWidgetTableEnd());

    result.append("<script type='text/javascript' src='"
            + getJsp().link("/system/workplace/resources/jquery/packed/jquery.js") + "'></script>\n");
    result.append("<script type='text/javascript' src='"
            + getJsp().link("/system/workplace/resources/highcharts/highstock-1.3.0.js") + "'></script>\n");
    result.append("<script type='text/javascript' src='"
            + getJsp().link("/system/workplace/resources/highcharts/exporting-3.0.0.js") + "'></script>\n");
    result.append("<script type='text/javascript'>\n");
    result.append("$(function() {\n");
    result.append("  $('form#EDITOR').after('");
    result.append("<div class=\"customScripts\">");
    if (m_adminSettings.getDisplayMemPerm()) {
        result.append(
                "<div id=\"perm\" style=\"height: 300px; width: 50%; float: left;\">Loading Mem Perm graph...</div>");
    }
    if (m_adminSettings.getDisplayMemOld()) {
        result.append(
                "<div id=\"old\" style=\"height: 300px; width: 50%; float: left;\">Loading Mem Old graph...</div>");
    }
    if (m_adminSettings.getDisplayMemEden()) {
        result.append(
                "<div id=\"eden\" style=\"height: 300px; width: 50%; float: left;\">Loading Mem Eden graph...</div>");
    }
    if (m_adminSettings.getDisplayMemSurvivor()) {
        result.append(
                "<div id=\"survivor\" style=\"height: 300px; width: 50%; float: left;\">Loading Mem Survivor graph...</div>");
    }
    result.append("</div>");
    result.append("  ');\n");
    result.append("  Highcharts.setOptions({\n");
    result.append("    global : { useUTC : true }\n");
    result.append("  }); \n");
    result.append(getUpdateInfoFunction(m_adminSettings.getDisplayMemPerm(), m_adminSettings.getDisplayMemOld(),
            m_adminSettings.getDisplayMemEden(), m_adminSettings.getDisplayMemSurvivor()));
    if (m_adminSettings.getDisplayMemPerm()) {
        result.append(getHighChartPerm());
    }
    if (m_adminSettings.getDisplayMemOld()) {
        result.append(getHighChartOld());
    }
    if (m_adminSettings.getDisplayMemEden()) {
        result.append(getHighChartEden());
    }
    if (m_adminSettings.getDisplayMemSurvivor()) {
        result.append(getHighChartSurvivor());
    }
    result.append("});\n");
    result.append("</script>\n");

    return result.toString();
}

From source file:org.apache.hadoop.hive.llap.daemon.impl.LlapDaemon.java

public static long getTotalHeapSize() {
    // runtime.getMax() gives a very different number from the actual Xmx sizing.
    // you can iterate through the
    // http://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryPoolMXBean.html
    // from java.lang.management to figure this out, but the hard-coded params in the llap run.sh
    // result in 89% usable heap (-XX:NewRatio=8) + a survivor region which is technically not
    // in the usable space.

    long total = 0;
    for (MemoryPoolMXBean mp : ManagementFactory.getMemoryPoolMXBeans()) {
        long sz = mp.getUsage().getMax();
        if (mp.getName().contains("Survivor")) {
            sz *= 2; // there are 2 survivor spaces
        }/*from w  w  w  .  j  a  va2 s .  c o  m*/
        if (mp.getType().equals(MemoryType.HEAP)) {
            total += sz;
        }
    }
    // round up to the next MB
    total += (total % (1024 * 1024));
    return total;
}

From source file:org.apache.pig.impl.util.SpillableMemoryManager.java

private SpillableMemoryManager() {
    ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).addNotificationListener(this, null, null);
    List<MemoryPoolMXBean> mpbeans = ManagementFactory.getMemoryPoolMXBeans();
    long totalSize = 0;
    for (MemoryPoolMXBean pool : mpbeans) {
        log.debug("Found heap (" + pool.getName() + ") of type " + pool.getType());
        if (pool.getType() == MemoryType.HEAP) {
            long size = pool.getUsage().getMax();
            totalSize += size;/*from   w ww . j a v  a  2  s .co m*/
            // CMS Old Gen or "tenured" is the only heap that supports
            // setting usage threshold.
            if (pool.isUsageThresholdSupported()) {
                tenuredHeap = pool;
            }
        }
    }
    extraGCSpillSizeThreshold = (long) (totalSize * extraGCThresholdFraction);
    if (tenuredHeap == null) {
        throw new RuntimeException("Couldn't find heap");
    }

    configureMemoryThresholds(MEMORY_THRESHOLD_FRACTION_DEFAULT, COLLECTION_THRESHOLD_FRACTION_DEFAULT,
            UNUSED_MEMORY_THRESHOLD_DEFAULT);

}