Search in sources :

Example 41 with MemoryUsage

use of java.lang.management.MemoryUsage in project bazel by bazelbuild.

the class RetainedHeapLimiter method handleNotification.

@Override
public void handleNotification(Notification notification, Object handback) {
    if (!notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
        return;
    }
    GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());
    Map<String, MemoryUsage> spaces = info.getGcInfo().getMemoryUsageAfterGc();
    for (Map.Entry<String, MemoryUsage> entry : spaces.entrySet()) {
        if (isTenuredSpace(entry.getKey())) {
            MemoryUsage space = entry.getValue();
            if (space.getMax() == 0) {
                // The CMS collector sometimes passes us nonsense stats.
                continue;
            }
            long percentUsed = 100 * space.getUsed() / space.getMax();
            if (percentUsed > occupiedHeapPercentageThreshold) {
                if (info.getGcCause().equals("System.gc()") && !throwingOom.getAndSet(true)) {
                    // Assume we got here from a GC initiated by the other branch.
                    String exitMsg = String.format("RetainedHeapLimiter forcing exit due to GC thrashing: tenured space " + "%s out of %s (>%s%%) occupied after back-to-back full GCs", space.getUsed(), space.getMax(), occupiedHeapPercentageThreshold);
                    System.err.println(exitMsg);
                    LOG.info(exitMsg);
                    // Exits the runtime.
                    BugReport.handleCrash(new OutOfMemoryError(exitMsg));
                } else if (System.currentTimeMillis() - lastTriggeredGcInMilliseconds > MIN_TIME_BETWEEN_TRIGGERED_GC_MILLISECONDS) {
                    LOG.info("Triggering a full GC with " + space.getUsed() + " out of " + space.getMax() + " used");
                    // Force a full stop-the-world GC and see if it can get us below the threshold.
                    System.gc();
                    lastTriggeredGcInMilliseconds = System.currentTimeMillis();
                }
            }
        }
    }
}
Also used : GarbageCollectionNotificationInfo(com.sun.management.GarbageCollectionNotificationInfo) MemoryUsage(java.lang.management.MemoryUsage) Map(java.util.Map)

Example 42 with MemoryUsage

use of java.lang.management.MemoryUsage in project hazelcast by hazelcast.

the class TimedMemberStateFactoryHelper method createRuntimeProps.

static void createRuntimeProps(MemberStateImpl memberState) {
    Runtime runtime = Runtime.getRuntime();
    ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
    RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
    ClassLoadingMXBean clMxBean = ManagementFactory.getClassLoadingMXBean();
    MemoryMXBean memoryMxBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage heapMemory = memoryMxBean.getHeapMemoryUsage();
    MemoryUsage nonHeapMemory = memoryMxBean.getNonHeapMemoryUsage();
    Map<String, Long> map = new HashMap<String, Long>();
    map.put("runtime.availableProcessors", Integer.valueOf(runtime.availableProcessors()).longValue());
    map.put("date.startTime", runtimeMxBean.getStartTime());
    map.put("seconds.upTime", runtimeMxBean.getUptime());
    map.put("memory.maxMemory", runtime.maxMemory());
    map.put("memory.freeMemory", runtime.freeMemory());
    map.put("memory.totalMemory", runtime.totalMemory());
    map.put("memory.heapMemoryMax", heapMemory.getMax());
    map.put("memory.heapMemoryUsed", heapMemory.getUsed());
    map.put("memory.nonHeapMemoryMax", nonHeapMemory.getMax());
    map.put("memory.nonHeapMemoryUsed", nonHeapMemory.getUsed());
    map.put("runtime.totalLoadedClassCount", clMxBean.getTotalLoadedClassCount());
    map.put("runtime.loadedClassCount", Integer.valueOf(clMxBean.getLoadedClassCount()).longValue());
    map.put("runtime.unloadedClassCount", clMxBean.getUnloadedClassCount());
    map.put("runtime.totalStartedThreadCount", threadMxBean.getTotalStartedThreadCount());
    map.put("runtime.threadCount", Integer.valueOf(threadMxBean.getThreadCount()).longValue());
    map.put("runtime.peakThreadCount", Integer.valueOf(threadMxBean.getPeakThreadCount()).longValue());
    map.put("runtime.daemonThreadCount", Integer.valueOf(threadMxBean.getDaemonThreadCount()).longValue());
    OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
    map.put("osMemory.freePhysicalMemory", get(osMxBean, "getFreePhysicalMemorySize", 0L));
    map.put("osMemory.committedVirtualMemory", get(osMxBean, "getCommittedVirtualMemorySize", 0L));
    map.put("osMemory.totalPhysicalMemory", get(osMxBean, "getTotalPhysicalMemorySize", 0L));
    map.put("osSwap.freeSwapSpace", get(osMxBean, "getFreeSwapSpaceSize", 0L));
    map.put("osSwap.totalSwapSpace", get(osMxBean, "getTotalSwapSpaceSize", 0L));
    map.put("os.maxFileDescriptorCount", get(osMxBean, "getMaxFileDescriptorCount", 0L));
    map.put("os.openFileDescriptorCount", get(osMxBean, "getOpenFileDescriptorCount", 0L));
    map.put("os.processCpuLoad", get(osMxBean, "getProcessCpuLoad", -1L));
    map.put("os.systemLoadAverage", get(osMxBean, "getSystemLoadAverage", -1L));
    map.put("os.systemCpuLoad", get(osMxBean, "getSystemCpuLoad", -1L));
    map.put("os.processCpuTime", get(osMxBean, "getProcessCpuTime", 0L));
    map.put("os.availableProcessors", get(osMxBean, "getAvailableProcessors", 0L));
    memberState.setRuntimeProps(map);
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) MemoryMXBean(java.lang.management.MemoryMXBean) HashMap(java.util.HashMap) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) MemoryUsage(java.lang.management.MemoryUsage) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 43 with MemoryUsage

use of java.lang.management.MemoryUsage in project hbase by apache.

the class MemorySizeUtil method getBucketCacheSize.

/**
   * @param conf used to read config for bucket cache size. (< 1 is treated as % and > is treated as MiB)
   * @return the number of bytes to use for bucket cache, negative if disabled.
   */
public static long getBucketCacheSize(final Configuration conf) {
    final float bucketCachePercentage = conf.getFloat(HConstants.BUCKET_CACHE_SIZE_KEY, 0F);
    long bucketCacheSize;
    // Values < 1 are treated as % of heap
    if (bucketCachePercentage < 1) {
        long max = -1L;
        final MemoryUsage usage = safeGetHeapMemoryUsage();
        if (usage != null) {
            max = usage.getMax();
        }
        bucketCacheSize = (long) (max * bucketCachePercentage);
    // values >= 1 are treated as # of MiB
    } else {
        bucketCacheSize = (long) (bucketCachePercentage * 1024 * 1024);
    }
    return bucketCacheSize;
}
Also used : MemoryUsage(java.lang.management.MemoryUsage)

Example 44 with MemoryUsage

use of java.lang.management.MemoryUsage in project hbase by apache.

the class MemorySizeUtil method getLruCacheSize.

/**
   * @param conf used to read cache configs
   * @return the number of bytes to use for LRU, negative if disabled.
   * @throws IllegalArgumentException if HFILE_BLOCK_CACHE_SIZE_KEY is > 1.0
   */
public static long getLruCacheSize(final Configuration conf) {
    float cachePercentage = conf.getFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, HConstants.HFILE_BLOCK_CACHE_SIZE_DEFAULT);
    if (cachePercentage <= 0.0001f) {
        return -1;
    }
    if (cachePercentage > 1.0) {
        throw new IllegalArgumentException(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY + " must be between 0.0 and 1.0, and not > 1.0");
    }
    long max = -1L;
    final MemoryUsage usage = safeGetHeapMemoryUsage();
    if (usage != null) {
        max = usage.getMax();
    }
    // Calculate the amount of heap to give the heap.
    return (long) (max * cachePercentage);
}
Also used : MemoryUsage(java.lang.management.MemoryUsage)

Example 45 with MemoryUsage

use of java.lang.management.MemoryUsage in project hbase by apache.

the class TestCacheConfig method testBucketCacheConfigL1L2Setup.

/**
   * Assert that when BUCKET_CACHE_COMBINED_KEY is false, the non-default, that we deploy
   * LruBlockCache as L1 with a BucketCache for L2.
   */
@Test(timeout = 10000)
public void testBucketCacheConfigL1L2Setup() {
    this.conf.set(HConstants.BUCKET_CACHE_IOENGINE_KEY, "offheap");
    // Make lru size is smaller than bcSize for sure.  Need this to be true so when eviction
    // from L1 happens, it does not fail because L2 can't take the eviction because block too big.
    this.conf.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.001f);
    MemoryUsage mu = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
    long lruExpectedSize = MemorySizeUtil.getLruCacheSize(this.conf);
    final int bcSize = 100;
    // MB.
    long bcExpectedSize = 100 * 1024 * 1024;
    assertTrue(lruExpectedSize < bcExpectedSize);
    this.conf.setInt(HConstants.BUCKET_CACHE_SIZE_KEY, bcSize);
    this.conf.setBoolean(CacheConfig.BUCKET_CACHE_COMBINED_KEY, false);
    CacheConfig cc = new CacheConfig(this.conf);
    basicBlockCacheOps(cc, false, false);
    assertTrue(cc.getBlockCache() instanceof LruBlockCache);
    // TODO: Assert sizes allocated are right and proportions.
    LruBlockCache lbc = (LruBlockCache) cc.getBlockCache();
    assertEquals(lruExpectedSize, lbc.getMaxSize());
    BlockCache bc = lbc.getVictimHandler();
    // getMaxSize comes back in bytes but we specified size in MB
    assertEquals(bcExpectedSize, ((BucketCache) bc).getMaxSize());
    // Test the L1+L2 deploy works as we'd expect with blocks evicted from L1 going to L2.
    long initialL1BlockCount = lbc.getBlockCount();
    long initialL2BlockCount = bc.getBlockCount();
    Cacheable c = new DataCacheEntry();
    BlockCacheKey bck = new BlockCacheKey("bck", 0);
    lbc.cacheBlock(bck, c, false, false);
    assertEquals(initialL1BlockCount + 1, lbc.getBlockCount());
    assertEquals(initialL2BlockCount, bc.getBlockCount());
    // Force evictions by putting in a block too big.
    final long justTooBigSize = lbc.acceptableSize() + 1;
    lbc.cacheBlock(new BlockCacheKey("bck2", 0), new DataCacheEntry() {

        @Override
        public long heapSize() {
            return justTooBigSize;
        }

        @Override
        public int getSerializedLength() {
            return (int) heapSize();
        }
    });
    // The eviction thread in lrublockcache needs to run.
    while (initialL1BlockCount != lbc.getBlockCount()) Threads.sleep(10);
    assertEquals(initialL1BlockCount, lbc.getBlockCount());
    long count = bc.getBlockCount();
    assertTrue(initialL2BlockCount + 1 <= count);
}
Also used : MemoryUsage(java.lang.management.MemoryUsage) Test(org.junit.Test)

Aggregations

MemoryUsage (java.lang.management.MemoryUsage)75 MemoryMXBean (java.lang.management.MemoryMXBean)15 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)15 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)6 RuntimeMXBean (java.lang.management.RuntimeMXBean)6 HashMap (java.util.HashMap)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)5 ThreadMXBean (java.lang.management.ThreadMXBean)4 Map (java.util.Map)4 IOException (java.io.IOException)3 MemoryType (java.lang.management.MemoryType)3 ArrayList (java.util.ArrayList)3 NotNull (org.jetbrains.annotations.NotNull)3 ApplicationInfo (com.intellij.openapi.application.ApplicationInfo)2 GarbageCollectionNotificationInfo (com.sun.management.GarbageCollectionNotificationInfo)2 File (java.io.File)2 BufferPoolMXBean (java.lang.management.BufferPoolMXBean)2 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)2 Date (java.util.Date)2 Properties (java.util.Properties)2