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

The following examples show how to use java.lang.management.MemoryPoolMXBean#setCollectionUsageThreshold() . 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: 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 2
Source File: HeapMemoryMonitor.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Register with the JVM to get threshold events.
 * 
 * Package private for testing.
 */
void startJVMThresholdListener() {
  final MemoryPoolMXBean memoryPoolMXBean = getTenuredMemoryPoolMXBean();

  // Set collection threshold to a low value, so that we can get
  // notifications after every GC run. After each such collection
  // threshold notification we set the usage thresholds to an
  // appropriate value.
  if (!testDisableMemoryUpdates) {
    memoryPoolMXBean.setCollectionUsageThreshold(1);
    // also set for notifications from survivor space GC
    final MemoryPoolMXBean survivorPoolMXBean = getSurvivorPoolMXBean();
    if (survivorPoolMXBean != null
        && survivorPoolMXBean.isCollectionUsageThresholdSupported()) {
      survivorPoolMXBean.setCollectionUsageThreshold(1);
    }
  }
  
  final long usageThreshold = memoryPoolMXBean.getUsageThreshold();
  this.cache.getLoggerI18n().info(
      LocalizedStrings.HeapMemoryMonitor_OVERRIDDING_MEMORYPOOLMXBEAN_HEAP_0_NAME_1,
      new Object[] { Long.valueOf(usageThreshold), memoryPoolMXBean.getName() });

  MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
  NotificationEmitter emitter = (NotificationEmitter) mbean;
  emitter.addNotificationListener(this, null, null);
}
 
Example 3
Source File: HeapMemoryMonitor.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Register with the JVM to get threshold events.
 * 
 * Package private for testing.
 */
void startJVMThresholdListener() {
  final MemoryPoolMXBean memoryPoolMXBean = getTenuredMemoryPoolMXBean();

  // Set collection threshold to a low value, so that we can get
  // notifications after every GC run. After each such collection
  // threshold notification we set the usage thresholds to an
  // appropriate value.
  if (!testDisableMemoryUpdates) {
    memoryPoolMXBean.setCollectionUsageThreshold(1);
    // also set for notifications from survivor space GC
    final MemoryPoolMXBean survivorPoolMXBean = getSurvivorPoolMXBean();
    if (survivorPoolMXBean != null
        && survivorPoolMXBean.isCollectionUsageThresholdSupported()) {
      survivorPoolMXBean.setCollectionUsageThreshold(1);
    }
  }
  
  final long usageThreshold = memoryPoolMXBean.getUsageThreshold();
  this.cache.getLoggerI18n().info(
      LocalizedStrings.HeapMemoryMonitor_OVERRIDDING_MEMORYPOOLMXBEAN_HEAP_0_NAME_1,
      new Object[] { Long.valueOf(usageThreshold), memoryPoolMXBean.getName() });

  MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
  NotificationEmitter emitter = (NotificationEmitter) mbean;
  emitter.addNotificationListener(this, null, null);
}
 
Example 4
Source File: VMLogger.java    From openjdk-systemtest with Apache License 2.0 4 votes vote down vote up
public void enableOptionalFunctionality()  {
    try {
        // ClassLoadingMXBean operations
        classBean.setVerbose(true);

        // MemoryMXBean operations
        memBean.setVerbose(true);

        // MemoryPoolMXBean operations
        for (MemoryPoolMXBean memPoolBean: memPoolBeans) {
            memPoolBean.resetPeakUsage();

            if (memPoolBean.isCollectionUsageThresholdSupported()) {
                memPoolBean.setCollectionUsageThreshold(10000);
            }

            if (memPoolBean.isUsageThresholdSupported()) {
                memPoolBean.setUsageThreshold(200000);
            }            
        }

        // ThreadMXBean operations
        threadBean.resetPeakThreadCount();

        if (threadBean.isThreadContentionMonitoringSupported()) {
            threadBean.setThreadContentionMonitoringEnabled(true);
        }

        if (threadBean.isThreadCpuTimeSupported()) {
            threadBean.setThreadCpuTimeEnabled(true);
        }
        
        // LoggingMXBean operations
        List<String> loggers = logBean.getLoggerNames();
        
        String[] levels = {"SEVERE", "WARNING", "INFO", "CONFIG", "FINE", "FINER", "FINEST"};
        int i = 0;
        
        for( String logger : loggers) {               	
            if (i > 6) {
            	i = i - 7;
            }
            
            // There's a chance the logger no longer exists
            String parent = logBean.getParentLoggerName(logger);
            if (parent != null) {
            	logBean.setLoggerLevel(logger, levels[i]);
            }
            
            i++;                
        }
        
    } catch (UnsupportedOperationException uoe) {
        Message.logOut("One of the operations you tried is not supported");
        uoe.printStackTrace();
    }
}
 
Example 5
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));
}