Example usage for java.lang.management MemoryType HEAP

List of usage examples for java.lang.management MemoryType HEAP

Introduction

In this page you can find the example usage for java.lang.management MemoryType HEAP.

Prototype

MemoryType HEAP

To view the source code for java.lang.management MemoryType HEAP.

Click Source Link

Document

Heap memory type.

Usage

From source file:mondrian.util.UtilCompatibleJdk15.java

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

From source file:io.ecarf.core.utils.Utils.java

/**
 * Log memory usage// w w w.j a  v  a  2 s. co m
 * @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:org.apache.hadoop.hbase.io.util.MemorySizeUtil.java

/**
 * @return Pair of global memstore size and memory type(ie. on heap or off heap).
 *//*  w w  w  . jav  a2  s . co m*/
public static Pair<Long, MemoryType> getGlobalMemstoreSize(Configuration conf) {
    long offheapMSGlobal = conf.getLong(OFFHEAP_MEMSTORE_SIZE_KEY, 0);// Size in MBs
    if (offheapMSGlobal > 0) {
        // Off heap memstore size has not relevance when MSLAB is turned OFF. We will go with making
        // this entire size split into Chunks and pooling them in MemstoreLABPoool. We dont want to
        // create so many on demand off heap chunks. In fact when this off heap size is configured, we
        // will go with 100% of this size as the pool size
        if (MemStoreLAB.isEnabled(conf)) {
            // We are in offheap Memstore use
            long globalMemStoreLimit = (long) (offheapMSGlobal * 1024 * 1024); // Size in bytes
            return new Pair<Long, MemoryType>(globalMemStoreLimit, MemoryType.NON_HEAP);
        } else {
            // Off heap max memstore size is configured with turning off MSLAB. It makes no sense. Do a
            // warn log and go with on heap memstore percentage. By default it will be 40% of Xmx
            LOG.warn("There is no relevance of configuring '" + OFFHEAP_MEMSTORE_SIZE_KEY + "' when '"
                    + MemStoreLAB.USEMSLAB_KEY + "' is turned off."
                    + " Going with on heap global memstore size ('" + MEMSTORE_SIZE_KEY + "')");
        }
    }
    return new Pair<Long, MemoryType>(getOnheapGlobalMemstoreSize(conf), MemoryType.HEAP);
}

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 ww.  ja  va 2  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 w w.ja  v  a  2  s .  c om
            // 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);

}

From source file:org.jahia.bin.errors.ErrorFileDumper.java

public static void outputSystemInfo(PrintWriter strOut, boolean systemProperties, boolean environmentVariables,
        boolean jahiaSettings, boolean memory, boolean caches, boolean threads, boolean deadlocks,
        boolean loadAverage) {

    if (systemProperties) {
        // now let's output the system properties.
        strOut.println();// ww  w.j  av  a2s  . c  om
        strOut.println("System properties:");
        strOut.println("-------------------");
        Map<Object, Object> orderedProperties = new TreeMap<Object, Object>(System.getProperties());
        Iterator<Map.Entry<Object, Object>> entrySetIter = orderedProperties.entrySet().iterator();
        while (entrySetIter.hasNext()) {
            Map.Entry<Object, Object> curEntry = entrySetIter.next();
            String curPropertyName = (String) curEntry.getKey();
            String curPropertyValue = (String) curEntry.getValue();
            strOut.println("   " + curPropertyName + " : " + curPropertyValue);
        }
    }

    if (environmentVariables) {
        // now let's output the environment variables.
        strOut.println();
        strOut.println("Environment variables:");
        strOut.println("-------------------");
        Map<String, String> orderedProperties = new TreeMap<String, String>(System.getenv());
        Iterator<Map.Entry<String, String>> entrySetIter = orderedProperties.entrySet().iterator();
        while (entrySetIter.hasNext()) {
            Map.Entry<String, String> curEntry = entrySetIter.next();
            String curPropertyName = curEntry.getKey();
            String curPropertyValue = curEntry.getValue();
            strOut.println("   " + curPropertyName + " : " + curPropertyValue);
        }
    }

    if (jahiaSettings) {
        strOut.println();

        if (SettingsBean.getInstance() != null) {
            strOut.append("Server configuration (").append(Jahia.getFullProductVersion()).append(" - ")
                    .append(Jahia.getBuildDate()).append("):");
            strOut.println();
            strOut.println("---------------------");
            SettingsBean settings = SettingsBean.getInstance();
            Map<Object, Object> jahiaOrderedProperties = new TreeMap<Object, Object>(
                    settings.getPropertiesFile());
            Iterator<Map.Entry<Object, Object>> jahiaEntrySetIter = jahiaOrderedProperties.entrySet()
                    .iterator();
            while (jahiaEntrySetIter.hasNext()) {
                Map.Entry<Object, Object> curEntry = jahiaEntrySetIter.next();
                String curPropertyName = (String) curEntry.getKey();
                String curPropertyValue = null;
                if (curEntry.getValue() == null) {
                    curPropertyValue = null;
                } else if (curEntry.getValue() instanceof String) {
                    curPropertyValue = (String) curEntry.getValue();
                } else {
                    curPropertyValue = curEntry.getValue().toString();
                }
                if (curPropertyName.toLowerCase().indexOf("password") == -1
                        && (!"mail_server".equals(curPropertyName)
                                || !StringUtils.contains(curPropertyValue, "&password=")
                                        && !StringUtils.contains(curPropertyValue, "?password="))) {
                    strOut.println("   " + curPropertyName + " = " + curPropertyValue);
                }
            }
        }
    }

    if (memory) {
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
        printMemoryUsage(MemoryType.HEAP.toString(), memoryUsage, strOut);
        memoryUsage = memoryMXBean.getNonHeapMemoryUsage();
        printMemoryUsage(MemoryType.NON_HEAP.toString(), memoryUsage, strOut);

        strOut.println("--------------");
        strOut.println("Memory pool details");
        strOut.println("--------------");
        List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
        for (MemoryPoolMXBean bean : memoryPoolMXBeans) {
            printMemoryUsage("Memory Pool \"" + bean.getName() + "\" (" + bean.getType().toString() + ")",
                    bean.getUsage(), strOut);
        }
    }

    if (caches) {
        strOut.println();
        DecimalFormat percentFormat = new DecimalFormat("###.##");
        if (SpringContextSingleton.getInstance().isInitialized()
                && (ServicesRegistry.getInstance().getCacheService() != null)) {
            strOut.println("Cache status:");
            strOut.println("--------------");

            // non Ehcaches
            SortedSet<String> sortedCacheNames = new TreeSet<>(
                    ServicesRegistry.getInstance().getCacheService().getNames());
            for (String sortedCacheName : sortedCacheNames) {
                final Cache<Object, Object> objectCache = ServicesRegistry.getInstance().getCacheService()
                        .getCache(sortedCacheName);
                if (objectCache != null
                        && !(((Cache<?, ?>) objectCache).getCacheImplementation() instanceof EhCacheImpl)) {
                    String efficiencyStr = "0";
                    if (!Double.isNaN(objectCache.getCacheEfficiency())) {
                        efficiencyStr = percentFormat.format(objectCache.getCacheEfficiency());
                    }
                    strOut.println(sortedCacheName + ": size=" + objectCache.size() + ", successful hits="
                            + objectCache.getSuccessHits() + ", total hits=" + objectCache.getTotalHits()
                            + ", efficiency=" + efficiencyStr + "%");
                }
            }
        }

        // Ehcaches
        List<CacheManager> cacheManagers = CacheManager.ALL_CACHE_MANAGERS;
        for (CacheManager ehcacheManager : cacheManagers) {
            String[] ehcacheNames = ehcacheManager.getCacheNames();
            java.util.Arrays.sort(ehcacheNames);
            for (String ehcacheName : ehcacheNames) {
                Ehcache ehcache = ehcacheManager.getEhcache(ehcacheName);
                strOut.append(ehcacheName).append(": ");
                if (ehcache != null) {
                    StatisticsGateway ehcacheStats = ehcache.getStatistics();
                    String efficiencyStr = "0";
                    if (ehcacheStats.cacheHitCount() + ehcacheStats.cacheMissCount() > 0) {
                        efficiencyStr = percentFormat.format(ehcacheStats.cacheHitCount() * 100f
                                / (ehcacheStats.cacheHitCount() + ehcacheStats.cacheMissCount()));
                    }
                    strOut.append("size=" + ehcacheStats.getSize() + ", successful hits="
                            + ehcacheStats.cacheHitCount() + ", total hits="
                            + (ehcacheStats.cacheHitCount() + ehcacheStats.cacheMissCount()) + ", efficiency="
                            + efficiencyStr + "%");
                    strOut.println();
                }
            }
        }
    }

    ThreadMonitor threadMonitor = null;
    if (threads) {
        strOut.println();
        strOut.println("Thread status:");
        strOut.println("--------------");
        threadMonitor = ThreadMonitor.getInstance();
        threadMonitor.generateThreadInfo(strOut);
    }

    if (deadlocks) {
        strOut.println();
        strOut.println("Deadlock status:");
        threadMonitor = threadMonitor != null ? threadMonitor : ThreadMonitor.getInstance();
        ;
        String deadlock = threadMonitor.findDeadlock();
        strOut.println(deadlock != null ? deadlock : "none");
    }

    if (loadAverage) {
        strOut.println();
        strOut.println("Request load average:");
        strOut.println("---------------------");
        RequestLoadAverage info = RequestLoadAverage.getInstance();
        if (info != null) {
            strOut.println("Over one minute=" + info.getOneMinuteLoad() + " Over five minute="
                    + info.getFiveMinuteLoad() + " Over fifteen minute=" + info.getFifteenMinuteLoad());
        } else {
            strOut.println("not available");
        }
        strOut.println();
    }

    strOut.flush();
}

From source file:org.rhq.enterprise.agent.promptcmd.GCPromptCommand.java

private void printGlobalMemoryUsage(PrintWriter out, final MemoryMXBean memoryMxBean) {
    printMemoryUsage(out, "GLOBAL", MemoryType.HEAP, memoryMxBean.getHeapMemoryUsage());
    printMemoryUsage(out, "GLOBAL", MemoryType.NON_HEAP, memoryMxBean.getNonHeapMemoryUsage());
}