public void close() {
    if(notificationListener != null) {
      MemoryMXBean mxBean = ManagementFactory.getMemoryMXBean();
      NotificationEmitter emitter = (NotificationEmitter) mxBean;
      try {
        emitter.removeNotificationListener(notificationListener);
      } catch(ListenerNotFoundException e) {
        LOG.warn("Failed to remove HeapMemoryMonitor notification listener from MemoryMXBean", e);
      }
    }
  }
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