private void setupTenuredGenPoolThreshold(final MemoryPoolMXBean tenuredGenPool) {
    if (tenuredGenPool == null) {
      return;
    }
    for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
      final long memoryThreshold = (int) Math.floor(pool.getUsage().getMax() * threshold);
      final boolean isTenured = isTenured(pool);
      if (!isTenured) {
        continue;
      }
      // set memory threshold on memory used after GC
      final boolean isCollectionUsageThresholdSupported = pool.isCollectionUsageThresholdSupported();
      if (isCollectionUsageThresholdSupported) {
        LOG.info("Setting collection usage threshold to {}", memoryThreshold);
        pool.setCollectionUsageThreshold(memoryThreshold);
        return;
      } else {
        // if collection usage threshold is not support, worst case set memory threshold on memory usage (before GC)
        final boolean isUsageThresholdSupported = pool.isUsageThresholdSupported();
        if (isUsageThresholdSupported) {
          LOG.info("Setting usage threshold to {}", memoryThreshold);
          pool.setUsageThreshold(memoryThreshold);
          return;
        }
      }
    }
  }
Class that monitors memory usage and notifies the listeners when a certain of threshold of memory is used
after GC (collection usage).
common/src/java/org/apache/hadoop/hive/common/HeapMemoryMonitor.java