@Test
  public void testSizes() throws IOException {
    Class<?> cl;
    long expected;
    long actual;

    // KeyValue
    cl = KeyValue.class;
    expected = ClassSize.estimateBase(cl, false);
    KeyValue kv = new KeyValue();
    actual = kv.heapSize();
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      assertEquals(expected, actual);
    }

    // LruBlockCache Overhead
    cl = LruBlockCache.class;
    actual = LruBlockCache.CACHE_FIXED_OVERHEAD;
    expected = ClassSize.estimateBase(cl, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      assertEquals(expected, actual);
    }

    // CachedBlock Fixed Overhead
    // We really need "deep" sizing but ClassSize does not do this.
    // Perhaps we should do all these more in this style....
    cl = LruCachedBlock.class;
    actual = LruCachedBlock.PER_BLOCK_OVERHEAD;
    expected = ClassSize.estimateBase(cl, false);
    expected += ClassSize.estimateBase(String.class, false);
    expected += ClassSize.estimateBase(ByteBuffer.class, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      ClassSize.estimateBase(String.class, true);
      ClassSize.estimateBase(ByteBuffer.class, true);
      assertEquals(expected, actual);
    }

    // DefaultMemStore Overhead
    cl = DefaultMemStore.class;
    actual = DefaultMemStore.FIXED_OVERHEAD;
    expected = ClassSize.estimateBase(cl, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      assertEquals(expected, actual);
    }

    // DefaultMemStore Deep Overhead
    actual = DefaultMemStore.DEEP_OVERHEAD;
    expected = ClassSize.estimateBase(cl, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      assertEquals(expected, actual);
    }

    // CompactingMemStore Deep Overhead
    cl = CompactingMemStore.class;
    actual = CompactingMemStore.DEEP_OVERHEAD;
    expected = ClassSize.estimateBase(cl, false);
    expected += ClassSize.estimateBase(AtomicBoolean.class, false);
    expected += ClassSize.estimateBase(AtomicBoolean.class, false);
    expected += ClassSize.estimateBase(CompactionPipeline.class, false);
    expected += ClassSize.estimateBase(LinkedList.class, false); // inside CompactionPipeline
    expected += ClassSize.estimateBase(LinkedList.class, false); // inside CompactionPipeline
    expected += ClassSize.estimateBase(MemStoreCompactor.class, false);
    expected += ClassSize.estimateBase(AtomicBoolean.class, false);// inside MemStoreCompactor
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      ClassSize.estimateBase(AtomicBoolean.class, true);
      ClassSize.estimateBase(AtomicBoolean.class, true);
      ClassSize.estimateBase(CompactionPipeline.class, true);
      ClassSize.estimateBase(LinkedList.class, true);
      ClassSize.estimateBase(LinkedList.class, true);
      ClassSize.estimateBase(MemStoreCompactor.class, true);
      ClassSize.estimateBase(AtomicBoolean.class, true);
      assertEquals(expected, actual);
    }

    // Segment Deep overhead
    cl = Segment.class;
    actual = Segment.DEEP_OVERHEAD;
    expected = ClassSize.estimateBase(cl, false);
    expected += 2 * ClassSize.estimateBase(AtomicLong.class, false);
    expected += ClassSize.estimateBase(AtomicReference.class, false);
    expected += ClassSize.estimateBase(CellSet.class, false);
    expected += ClassSize.estimateBase(ReentrantReadWriteLock.class, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      ClassSize.estimateBase(AtomicLong.class, true);
      ClassSize.estimateBase(AtomicReference.class, true);
      ClassSize.estimateBase(CellSet.class, true);
      ClassSize.estimateBase(ReentrantReadWriteLock.class, true);
      assertEquals(expected, actual);
    }

    // MutableSegment Deep overhead
    cl = MutableSegment.class;
    actual = MutableSegment.DEEP_OVERHEAD;
    expected = ClassSize.estimateBase(cl, false);
    expected += 2 * ClassSize.estimateBase(AtomicLong.class, false);
    expected += ClassSize.estimateBase(AtomicReference.class, false);
    expected += ClassSize.estimateBase(CellSet.class, false);
    expected += ClassSize.estimateBase(ReentrantReadWriteLock.class, false);
    expected += ClassSize.estimateBase(SyncTimeRangeTracker.class, false);
    expected += ClassSize.estimateBase(ConcurrentSkipListMap.class, false);
    expected += ClassSize.estimateBase(AtomicBoolean.class, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      ClassSize.estimateBase(AtomicLong.class, true);
      ClassSize.estimateBase(AtomicLong.class, true);
      ClassSize.estimateBase(AtomicReference.class, true);
      ClassSize.estimateBase(CellSet.class, true);
      ClassSize.estimateBase(ReentrantReadWriteLock.class, true);
      ClassSize.estimateBase(SyncTimeRangeTracker.class, true);
      ClassSize.estimateBase(ConcurrentSkipListMap.class, true);
      ClassSize.estimateBase(AtomicBoolean.class, true);
      assertEquals(expected, actual);
    }

    // ImmutableSegments Deep overhead
    cl = ImmutableSegment.class;
    actual = ImmutableSegment.DEEP_OVERHEAD;
    expected = ClassSize.estimateBase(cl, false);
    expected += 2 * ClassSize.estimateBase(AtomicLong.class, false);
    expected += ClassSize.estimateBase(AtomicReference.class, false);
    expected += ClassSize.estimateBase(CellSet.class, false);
    expected += ClassSize.estimateBase(ReentrantReadWriteLock.class, false);
    expected += ClassSize.estimateBase(NonSyncTimeRangeTracker.class, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      ClassSize.estimateBase(AtomicLong.class, true);
      ClassSize.estimateBase(AtomicLong.class, true);
      ClassSize.estimateBase(AtomicReference.class, true);
      ClassSize.estimateBase(CellSet.class, true);
      ClassSize.estimateBase(ReentrantReadWriteLock.class, true);
      ClassSize.estimateBase(NonSyncTimeRangeTracker.class, true);
      assertEquals(expected, actual);
    }

    cl = CSLMImmutableSegment.class;
    actual = CSLMImmutableSegment.DEEP_OVERHEAD_CSLM;
    expected = ClassSize.estimateBase(cl, false);
    expected += 2 * ClassSize.estimateBase(AtomicLong.class, false);
    expected += ClassSize.estimateBase(AtomicReference.class, false);
    expected += ClassSize.estimateBase(CellSet.class, false);
    expected += ClassSize.estimateBase(ReentrantReadWriteLock.class, false);
    expected += ClassSize.estimateBase(NonSyncTimeRangeTracker.class, false);
    expected += ClassSize.estimateBase(ConcurrentSkipListMap.class, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      ClassSize.estimateBase(AtomicLong.class, true);
      ClassSize.estimateBase(AtomicLong.class, true);
      ClassSize.estimateBase(AtomicReference.class, true);
      ClassSize.estimateBase(CellSet.class, true);
      ClassSize.estimateBase(ReentrantReadWriteLock.class, true);
      ClassSize.estimateBase(NonSyncTimeRangeTracker.class, true);
      ClassSize.estimateBase(ConcurrentSkipListMap.class, true);
      assertEquals(expected, actual);
    }
    cl = CellArrayImmutableSegment.class;
    actual = CellArrayImmutableSegment.DEEP_OVERHEAD_CAM;
    expected = ClassSize.estimateBase(cl, false);
    expected += 2 * ClassSize.estimateBase(AtomicLong.class, false);
    expected += ClassSize.estimateBase(AtomicReference.class, false);
    expected += ClassSize.estimateBase(CellSet.class, false);
    expected += ClassSize.estimateBase(ReentrantReadWriteLock.class, false);
    expected += ClassSize.estimateBase(NonSyncTimeRangeTracker.class, false);
    expected += ClassSize.estimateBase(CellArrayMap.class, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      ClassSize.estimateBase(AtomicLong.class, true);
      ClassSize.estimateBase(AtomicLong.class, true);
      ClassSize.estimateBase(AtomicReference.class, true);
      ClassSize.estimateBase(CellSet.class, true);
      ClassSize.estimateBase(ReentrantReadWriteLock.class, true);
      ClassSize.estimateBase(NonSyncTimeRangeTracker.class, true);
      ClassSize.estimateBase(CellArrayMap.class, true);
      assertEquals(expected, actual);
    }

    // Store Overhead
    cl = HStore.class;
    actual = HStore.FIXED_OVERHEAD;
    expected = ClassSize.estimateBase(cl, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      assertEquals(expected, actual);
    }

    // Region Overhead
    cl = HRegion.class;
    actual = HRegion.FIXED_OVERHEAD;
    expected = ClassSize.estimateBase(cl, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      assertEquals(expected, actual);
    }

    cl = StoreHotnessProtector.class;
    actual = StoreHotnessProtector.FIXED_SIZE;
    expected = ClassSize.estimateBase(cl, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      assertEquals(expected, actual);
    }

    // Block cache key overhead. Only tests fixed overhead as estimating heap
    // size of strings is hard.
    cl = BlockCacheKey.class;
    actual = BlockCacheKey.FIXED_OVERHEAD;
    expected = ClassSize.estimateBase(cl, false);
    if (expected != actual) {
      ClassSize.estimateBase(cl, true);
      assertEquals(expected, actual);
    }

    // Currently NOT testing Deep Overheads of many of these classes.
    // Deep overheads cover a vast majority of stuff, but will not be 100%
    // accurate because it's unclear when we're referencing stuff that's already
    // accounted for. But we have satisfied our two core requirements.
    // Sizing is quite accurate now, and our tests will throw errors if
    // any of these classes are modified without updating overhead sizes.
  }
Testing the classes that implements HeapSize and are a part of 0.20. Some are not tested here
for example BlockIndex which is tested in TestHFile since it is a non public class
hbase-server/src/test/java/org/apache/hadoop/hbase/io/TestHeapSize.java