Example usage for java.lang.management ManagementFactory getMemoryPoolMXBeans

List of usage examples for java.lang.management ManagementFactory getMemoryPoolMXBeans

Introduction

In this page you can find the example usage for java.lang.management ManagementFactory getMemoryPoolMXBeans.

Prototype

public static List<MemoryPoolMXBean> getMemoryPoolMXBeans() 

Source Link

Document

Returns a list of MemoryPoolMXBean objects in the Java virtual machine.

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());
                }/*  w  w  w.ja  va 2  s.  com*/

                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.datumbox.common.utilities.ProcessStatistics.java

public long peakMemoryBytes() {
    long maxBytes = Long.MIN_VALUE;

    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean pool : pools) {
        MemoryUsage peak = pool.getPeakUsage();

        long peakUsed = peak.getUsed();
        if (maxBytes < peakUsed) {
            maxBytes = peakUsed;/*from w  ww  .j a  v a 2  s  .c  o m*/
        }
    }

    return maxBytes;
}

From source file:it.geosolutions.geobatch.ui.mvc.MemoryController.java

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    ModelAndView mav = new ModelAndView("memory"); // jsp
    final MemoryMXBean mmxbean = ManagementFactory.getMemoryMXBean();
    mav.addObject("objectPendingFinalizationCount", mmxbean.getObjectPendingFinalizationCount());
    mav.addObject("nonHeapMemoryUsage", mmxbean.getNonHeapMemoryUsage());
    mav.addObject("heapMemoryUsage", mmxbean.getHeapMemoryUsage());
    mav.addObject("memoryPoolBeansList", ManagementFactory.getMemoryPoolMXBeans());

    return mav;/* w w w .j a  v a  2 s  . c o m*/
}

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())));
    }/* www. j ava2s .  c o m*/
}

From source file:mondrian.util.UtilCompatibleJdk15.java

private static MemoryPoolMXBean findTenuredGenPool() {
    for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
        if (pool.getType() == MemoryType.HEAP) {
            return pool;
        }//ww w. j a  v  a2  s .  c  om
    }
    throw new AssertionError("Could not find tenured space");
}

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

/**
 * Return some information regarding the memory status of the instance.
 *///  www  .  j av a  2s .  c o  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  ww. j  a  v  a2s.co m
@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 dialog HTML for all defined widgets of the named dialog (page).<p>
 * /*  w w  w  .  java  2  s  .  com*/
 * 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:io.ecarf.core.utils.Utils.java

/**
 * Log memory usage//  ww  w  .  jav  a 2  s.  c om
 * @return
 */
public static long getMemoryUsage() {
    List<MemoryPoolMXBean> memoryPools = new ArrayList<MemoryPoolMXBean>(
            ManagementFactory.getMemoryPoolMXBeans());
    long usedHeapMemoryAfterLastGC = 0;
    for (MemoryPoolMXBean memoryPool : memoryPools) {
        if (memoryPool.getType().equals(MemoryType.HEAP)) {
            MemoryUsage poolCollectionMemoryUsage = memoryPool.getCollectionUsage();
            usedHeapMemoryAfterLastGC += poolCollectionMemoryUsage.getUsed();
        }
    }
    return usedHeapMemoryAfterLastGC;
}

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

/**
 * Creates the list of widgets for this dialog.<p>
 *//*from  www.j  a  v  a  2  s  .  c o  m*/
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());
        }

    }
}