Java Code Examples for java.lang.management.MemoryPoolMXBean#getType()

The following examples show how to use java.lang.management.MemoryPoolMXBean#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemoryLogger.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the memory pool statistics from the JVM.
 *
 * @param poolBeans The collection of memory pool beans.
 * @return A string denoting the names and sizes of the memory pools.
 */
public static String getMemoryPoolStatsAsString(List<MemoryPoolMXBean> poolBeans) {
	StringBuilder bld = new StringBuilder("Off-heap pool stats: ");
	int count = 0;
	
	for (MemoryPoolMXBean bean : poolBeans) {
		if (bean.getType() == MemoryType.NON_HEAP) {
			if (count > 0) {
				bld.append(", ");
			}
			count++;

			MemoryUsage usage = bean.getUsage();
			long used = usage.getUsed() >> 20;
			long committed = usage.getCommitted() >> 20;
			long max = usage.getMax() >> 20;
			
			bld.append('[').append(bean.getName()).append(": ");
			bld.append(used).append('/').append(committed).append('/').append(max);
			bld.append(" MB (used/committed/max)]");
		}
	}

	return bld.toString();
}
 
Example 2
Source File: RangerMetricsUtil.java    From ranger with Apache License 2.0 6 votes vote down vote up
/**
 * collect the pool division of java
 */
protected Map<String, Object> getPoolDivision() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerMetricsUtil.getPoolDivision()");
    }

    Map<String, Object> poolDivisionValues = new LinkedHashMap<>();
    for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) {
        if (mpBean.getType() == MemoryType.HEAP) {
            poolDivisionValues.put(mpBean.getName(), mpBean.getUsage());
        }
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerMetricsUtil.getPoolDivision()" + poolDivisionValues);
    }

    return poolDivisionValues;
}
 
Example 3
Source File: RangerMetricsUtil.java    From ranger with Apache License 2.0 6 votes vote down vote up
/**
 * collect the pool division of java
 */
protected Map<String, Object> getPoolDivision() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerJVMMetricUtil.getPoolDivision()");
    }

    Map<String, Object> poolDivisionValues = new LinkedHashMap<>();
    for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) {
        if (mpBean.getType() == MemoryType.HEAP) {
            poolDivisionValues.put(mpBean.getName(), mpBean.getUsage());
        }
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerJVMMetricUtil.getPoolDivision()" + poolDivisionValues);
    }

    return poolDivisionValues;
}
 
Example 4
Source File: HeapMemoryMonitor.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Determines if the name of the memory pool MXBean provided matches a list of
 * known tenured pool names.
 * 
 * Package private for testing.
 * 
 * @param memoryPoolMXBean
 *          The memory pool MXBean to check.
 * @return True if the pool name matches a known tenured pool name, false
 *         otherwise.
 */
static boolean isTenured(MemoryPoolMXBean memoryPoolMXBean) {
  if (memoryPoolMXBean.getType() != MemoryType.HEAP) {
    return false;
  }
  
  String name = memoryPoolMXBean.getName();
  
  return name.equals("CMS Old Gen")     // Sun Concurrent Mark Sweep GC
      || name.equals("PS Old Gen")      // Sun Parallel GC
      || name.equals("G1 Old Gen")      // Sun G1 GC
      || name.equals("Old Space")       // BEA JRockit 1.5, 1.6 GC
      || name.equals("Tenured Gen")     // Hitachi 1.5 GC
      || name.equals("Java heap")       // IBM 1.5, 1.6 GC
      
      // Allow an unknown pool name to monitor
      || (HEAP_POOL != null && name.equals(HEAP_POOL));
}
 
Example 5
Source File: HeapMemoryMonitor.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Determines if the name of the memory pool MXBean provided matches a list of
 * known young generation pool names.
 * 
 * @param memoryPoolMXBean
 *          The memory pool MXBean to check.
 * @return True if the pool name matches a known young generation pool name,
 *         false otherwise.
 */
static boolean isEden(MemoryPoolMXBean memoryPoolMXBean) {
  if (memoryPoolMXBean.getType() != MemoryType.HEAP) {
    return false;
  }

  String name = memoryPoolMXBean.getName();

  return name.equals("Par Eden Space")  // Oracle ParNew with Concurrent Mark Sweep GC
      || name.equals("PS Eden Space")   // Oracle Parallel GC
      || name.equals("G1 Eden")         // Oracle G1 GC
      //|| name.equals("Nursery")       // BEA JRockit 1.5, 1.6 GC
      || name.equals("Eden Space")      // Hitachi 1.5 GC
      // Allow an unknown pool name to monitor
      || (HEAP_EDEN_POOL != null && name.equals(HEAP_EDEN_POOL));
}
 
Example 6
Source File: HeapMemoryMonitor.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Determines if the name of the memory pool MXBean provided matches a list of
 * known tenured pool names.
 * 
 * Package private for testing.
 * 
 * @param memoryPoolMXBean
 *          The memory pool MXBean to check.
 * @return True if the pool name matches a known tenured pool name, false
 *         otherwise.
 */
static boolean isTenured(MemoryPoolMXBean memoryPoolMXBean) {
  if (memoryPoolMXBean.getType() != MemoryType.HEAP) {
    return false;
  }
  
  String name = memoryPoolMXBean.getName();
  
  return name.equals("CMS Old Gen")     // Sun Concurrent Mark Sweep GC
      || name.equals("PS Old Gen")      // Sun Parallel GC
      || name.equals("G1 Old Gen")      // Sun G1 GC
      || name.equals("Old Space")       // BEA JRockit 1.5, 1.6 GC
      || name.equals("Tenured Gen")     // Hitachi 1.5 GC
      || name.equals("Java heap")       // IBM 1.5, 1.6 GC
      
      // Allow an unknown pool name to monitor
      || (HEAP_POOL != null && name.equals(HEAP_POOL));
}
 
Example 7
Source File: HeapMonitorThread.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private void registerForNotifications() {
  // Register the listener with MemoryMXBean
  NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean();
  emitter.addNotificationListener(listener, null, null);

  // set collection usage threshold.
  for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
    if (pool.getType() == MemoryType.HEAP &&
      pool.isUsageThresholdSupported() &&
      pool.isCollectionUsageThresholdSupported()) {

      long threshold = (pool.getUsage().getMax() * thresholdPercentage) / 100;
      logger.info("setting collection threshold for " + pool.getName() +
        " with max " + pool.getUsage().getMax() +
        " to " + threshold);

      pool.setCollectionUsageThreshold(threshold);
      monitoredPools.put(pool.getName(), pool.getCollectionUsageThresholdCount());
    } else {
      logger.info("skip monitoring for pool " + pool.getName());
    }
  }
}
 
Example 8
Source File: Memory.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a summary information about the memory pools.
 */
public static String poolSummaries() {
    // Why ? list-archive?4273859
    // How ? http://stackoverflow.com/questions/697336/how-do-i-programmatically-find-out-my-permgen-space-usage
    //       http://stackoverflow.com/questions/8356416/xxmaxpermsize-with-or-without-xxpermsize
    StringBuilder sb = new StringBuilder();
    for (MemoryPoolMXBean item : ManagementFactory.getMemoryPoolMXBeans()) {
        String name = item.getName();
        MemoryType type = item.getType();
        MemoryUsage usage = item.getUsage();
        MemoryUsage peak = item.getPeakUsage();
        MemoryUsage collections = item.getCollectionUsage();
        sb.append("Memory pool name: " + name
                  + ", type: " + type
                  + ", usage: " + usage
                  + ", peak: " + peak
                  + ", collections: " + collections
                  + "\n");
    }
    return sb.toString();
}
 
Example 9
Source File: HeapMonitorSource.java    From flink-tutorials with Apache License 2.0 6 votes vote down vote up
@Override
public void run(SourceFunction.SourceContext<HeapMetrics> sourceContext) throws Exception {
	LOG.info("starting HeapMonitorSource");

	int subtaskIndex = this.getRuntimeContext().getIndexOfThisSubtask();
	String hostname = InetAddress.getLocalHost().getHostName();

	while (running) {
		Thread.sleep(sleepMillis);

		for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) {
			if (mpBean.getType() == MemoryType.HEAP) {
				MemoryUsage memoryUsage = mpBean.getUsage();
				long used = memoryUsage.getUsed();
				long max = memoryUsage.getMax();

				synchronized (sourceContext.getCheckpointLock()) {
					sourceContext.collect(new HeapMetrics(mpBean.getName(), used, max, (double) used / max, subtaskIndex, hostname));
				}
			}
		}
	}
}
 
Example 10
Source File: MemoryLogger.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the memory pool statistics from the JVM.
 *
 * @param poolBeans The collection of memory pool beans.
 * @return A string denoting the names and sizes of the memory pools.
 */
public static String getMemoryPoolStatsAsString(List<MemoryPoolMXBean> poolBeans) {
	StringBuilder bld = new StringBuilder("Off-heap pool stats: ");
	int count = 0;
	
	for (MemoryPoolMXBean bean : poolBeans) {
		if (bean.getType() == MemoryType.NON_HEAP) {
			if (count > 0) {
				bld.append(", ");
			}
			count++;

			MemoryUsage usage = bean.getUsage();
			long used = usage.getUsed() >> 20;
			long committed = usage.getCommitted() >> 20;
			long max = usage.getMax() >> 20;
			
			bld.append('[').append(bean.getName()).append(": ");
			bld.append(used).append('/').append(committed).append('/').append(max);
			bld.append(" MB (used/committed/max)]");
		}
	}

	return bld.toString();
}
 
Example 11
Source File: HeapMemoryMonitor.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the name of the memory pool MXBean provided matches a list of
 * known survivor space pool names.
 * 
 * @param memoryPoolMXBean
 *          The memory pool MXBean to check.
 * @return True if the pool name matches a known survivor space pool name,
 *         false otherwise.
 */
static boolean isSurvivor(MemoryPoolMXBean memoryPoolMXBean) {
  if (memoryPoolMXBean.getType() != MemoryType.HEAP) {
    return false;
  }

  String name = memoryPoolMXBean.getName();

  return name.equals("Par Survivor Space")  // Oracle Concurrent Mark Sweep GC
      || name.equals("PS Survivor Space")   // Oracle Parallel GC
      || name.equals("G1 Survivor")         // Oracle G1 GC
      || name.equals("Survivor Space")      // Hitachi 1.5 GC
      // Allow an unknown pool name to monitor
      || (HEAP_SURVIVOR_POOL != null && name.equals(HEAP_SURVIVOR_POOL));
}
 
Example 12
Source File: JmxSupport.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
MemoryPoolMXBean getPermGenPool() {
    try {
        if (permGenPool == null) {
            JvmMXBeans jmx = getJvmMXBeans();
            if (jmx != null) {
                Collection<MemoryPoolMXBean> pools = jmx.getMemoryPoolMXBeans();
                for (MemoryPoolMXBean pool : pools) {
                    MemoryType type = pool.getType();
                    String name = pool.getName();
                    if (MemoryType.NON_HEAP.equals(type) &&
                            (PERM_GEN.equals(name) ||
                            PS_PERM_GEN.equals(name) ||
                            CMS_PERM_GEN.equals(name) ||
                            G1_PERM_GEN.equals(name) ||
                            METASPACE.equals(name) ||
                            IBM_PERM_GEN.equals(name))) {
                        permGenPool = pool;
                        break;
                    }
                }
            }
        }
        return permGenPool;
    } catch (Exception e) {
        LOGGER.throwing(JmxSupport.class.getName(), "getPermGenPool", e); // NOI18N
        return null;
    }
}
 
Example 13
Source File: AtlasMetricJVMUtil.java    From atlas with Apache License 2.0 5 votes vote down vote up
/**
 * collect the pool division of java
 */
private static void pooldivision(Map<String, Object> memory) {
    Map<String, Object> poolDivisionValues = new LinkedHashMap<>();
    for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) {
        if (mpBean.getType() == MemoryType.HEAP) {
            poolDivisionValues.put(mpBean.getName(), mpBean.getUsage());
        }
    }
    memory.put("memory_pool_usages", poolDivisionValues);
}
 
Example 14
Source File: PerfStatsTracking.java    From buck with Apache License 2.0 5 votes vote down vote up
public void probeMemory() {
  long freeMemoryBytes = Runtime.getRuntime().freeMemory();
  long totalMemoryBytes = Runtime.getRuntime().totalMemory();
  long maxMemoryBytes = Runtime.getRuntime().maxMemory();

  long totalGcTimeMs = 0;
  for (GarbageCollectorMXBean gcMxBean : ManagementFactory.getGarbageCollectorMXBeans()) {
    long collectionTimeMs = gcMxBean.getCollectionTime();
    if (collectionTimeMs == -1) {
      // Gc collection time is not supported on this JVM.
      totalGcTimeMs = -1;
      break;
    }
    totalGcTimeMs += collectionTimeMs;
  }

  ImmutableMap.Builder<String, Long> currentMemoryBytesUsageByPool = ImmutableMap.builder();
  for (MemoryPoolMXBean memoryPoolBean : ManagementFactory.getMemoryPoolMXBeans()) {
    String name = memoryPoolBean.getName();
    MemoryType type = memoryPoolBean.getType();
    long currentlyUsedBytes = memoryPoolBean.getUsage().getUsed();
    currentMemoryBytesUsageByPool.put(name + "(" + type + ")", currentlyUsedBytes);
  }

  eventBus.post(
      new MemoryPerfStatsEvent(
          freeMemoryBytes,
          totalMemoryBytes,
          maxMemoryBytes,
          totalGcTimeMs,
          currentMemoryBytesUsageByPool.build()));
}
 
Example 15
Source File: HeapMemoryMonitor.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the name of the memory pool MXBean provided matches a list of
 * known survivor space pool names.
 * 
 * @param memoryPoolMXBean
 *          The memory pool MXBean to check.
 * @return True if the pool name matches a known survivor space pool name,
 *         false otherwise.
 */
static boolean isSurvivor(MemoryPoolMXBean memoryPoolMXBean) {
  if (memoryPoolMXBean.getType() != MemoryType.HEAP) {
    return false;
  }

  String name = memoryPoolMXBean.getName();

  return name.equals("Par Survivor Space")  // Oracle Concurrent Mark Sweep GC
      || name.equals("PS Survivor Space")   // Oracle Parallel GC
      || name.equals("G1 Survivor")         // Oracle G1 GC
      || name.equals("Survivor Space")      // Hitachi 1.5 GC
      // Allow an unknown pool name to monitor
      || (HEAP_SURVIVOR_POOL != null && name.equals(HEAP_SURVIVOR_POOL));
}
 
Example 16
Source File: MemoryWarningSystem.java    From metafacture-core with Apache License 2.0 5 votes vote down vote up
/**
 * Tenured Space Pool can be determined by it being of type HEAP and by it
 * being possible to set the usage threshold.
 *
 * @return MXBean for the Tenured Space Pool
 */
private static MemoryPoolMXBean findTenuredGenPool() {
    // I don't know whether this approach is better, or whether
    // we should rather check for the pool name "Tenured Gen"?
    for (final MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans())
        if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
            return pool;
        }
    throw new AssertionError("Could not find tenured space");
}
 
Example 17
Source File: MemoryMonitor.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void setPercentageUsageThreshold(double percentage) {
    for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
        if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
            if (percentage <= 0.0 || percentage > 1.0) {
                throw new IllegalArgumentException("percentage not in range");
            }
            long warningThreshold = (long) (pool.getUsage().getMax() * percentage);
            pool.setUsageThreshold(warningThreshold);
        }
    }
}
 
Example 18
Source File: MemoryMonitor.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private static MemoryPoolMXBean findTenuredGenPool() {
    for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
        if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
            return pool;
        }
    }
    throw new AssertionError("could not find tenured space");
}
 
Example 19
Source File: MemoryWarningService.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Tenured Space Pool can be determined by it being of type HEAP and by it
 * being possible to set the usage threshold.
 */
private static MemoryPoolMXBean findTenuredGenPool() {
	for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
		// I don't know whether this approach is better, or whether
		// we should rather check for the pool name "Tenured Gen"?
		if (pool.getType() == MemoryType.HEAP
				&& pool.isUsageThresholdSupported()) {
			return pool;
		}
	}
	throw new AssertionError("Could not find tenured space");
}
 
Example 20
Source File: SpillableMemoryManager.java    From spork with Apache License 2.0 4 votes vote down vote up
private SpillableMemoryManager() {
    ((NotificationEmitter)ManagementFactory.getMemoryMXBean()).addNotificationListener(this, null, null);
    List<MemoryPoolMXBean> mpbeans = ManagementFactory.getMemoryPoolMXBeans();
    MemoryPoolMXBean tenuredHeap = null;
    long tenuredHeapSize = 0;
    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;
            // CMS Old Gen or "tenured" is the only heap that supports
            // setting usage threshold.
            if (pool.isUsageThresholdSupported()) {
                tenuredHeapSize = size;
                tenuredHeap = pool;
            }
        }
    }
    extraGCSpillSizeThreshold  = (long) (totalSize * extraGCThresholdFraction);
    if (tenuredHeap == null) {
        throw new RuntimeException("Couldn't find heap");
    }
    log.debug("Selected heap to monitor (" +
        tenuredHeap.getName() + ")");

    // we want to set both collection and usage threshold alerts to be
    // safe. In some local tests after a point only collection threshold
    // notifications were being sent though usage threshold notifications
    // were sent early on. So using both would ensure that
    // 1) we get notified early (though usage threshold exceeded notifications)
    // 2) we get notified always when threshold is exceeded (either usage or
    //    collection)

    /* We set the threshold to be 50% of tenured since that is where
     * the GC starts to dominate CPU time according to Sun doc */
    tenuredHeap.setCollectionUsageThreshold((long)(tenuredHeapSize * collectionMemoryThresholdFraction));
    // we set a higher threshold for usage threshold exceeded notification
    // since this is more likely to be effective sooner and we do not
    // want to be spilling too soon
    tenuredHeap.setUsageThreshold((long)(tenuredHeapSize * memoryThresholdFraction));
}