@Test
  public void testDeregisterOnOutOfScope() {
    Configuration conf = new Configuration();
    ConfigurationManager cm = new ConfigurationManager();

    boolean outOfScopeObserversDeregistered = false;

    // On my machine, I was able to cause a GC after around 5 iterations.
    // If we do not cause a GC in 100k iterations, which is very unlikely,
    // there might be something wrong with the GC.
    for (int i = 0; i < 100000; i++) {
      registerLocalObserver(cm);
      cm.notifyAllObservers(conf);

      // 'Suggest' the system to do a GC. We should be able to cause GC
      // atleast once in the 2000 iterations.
      System.gc();

      // If GC indeed happened, all the observers (which are all out of scope),
      // should have been deregistered.
      if (cm.getNumObservers() <= i) {
        outOfScopeObserversDeregistered = true;
        break;
      }
    }
    if (!outOfScopeObserversDeregistered) {
      LOG.warn("Observers were not GC-ed! Something seems to be wrong.");
    }
    assertTrue(outOfScopeObserversDeregistered);
  }
Test if out-of-scope observers are deregistered on GC.
hbase-common/src/test/java/org/apache/hadoop/hbase/conf/TestConfigurationManager.java