@Test
  public void recoveringTooMuchDataDoesNotRunOutOfOffHeapMemory() {
    final int ENTRY_COUNT = 40;
    GemFireCacheImpl gfc = createCache();
    try {
      Region<Object, Object> r = createRegion(gfc);
      byte[] v = new byte[1024 * 1024];
      for (int i = 0; i < ENTRY_COUNT; i++) {
        r.put(i, v);
      }
    } finally {
      closeCache(gfc);
    }
    await().until(() -> {
      return MemoryAllocatorImpl.getAllocator().getStats().getObjects() == 0;
    });
    System.setProperty("gemfire.disk.recoverValuesSync", "true");
    System.setProperty("gemfire.disk.recoverLruValues", "true");
    try {
      gfc = createCache();
      try {
        createDiskStore(gfc);
        try {
          int offHeapObjects = MemoryAllocatorImpl.getAllocator().getStats().getObjects();
          if (offHeapObjects < 10) {
            fail("expected at least 10 offheap values to be recovered but only did "
                + offHeapObjects);
          }
          if (offHeapObjects == ENTRY_COUNT) {
            // Why "== ENTRY_COUNT" instead of "== 10"?
            // As we recover values we asynchronously notify the resource manager
            // of how much off-heap memory was consumed. Once it sees we are over
            // the LRU limit then recovery of values will stop happening.
            // Since it is async it can allow us to exceed the LRU limit.
            // So far this test usually sees 10 but sometimes sees 11.
            // I allow up to ENTRY_COUNT-1 just to prevent intermittent test failures.
            // We should consider changing this async notification to be sync so
            // that the limit can not be exceeded.
            fail("expected at most " + (ENTRY_COUNT - 1)
                + " offheap values to be recovered but actually did " + offHeapObjects);
          }
        } finally {
          DiskStore ds = gfc.findDiskStore(DS_NAME);
          ds.destroy();
        }
      } finally {
        closeCache(gfc);
      }
    } finally {
      System.clearProperty("gemfire.disk.recoverValuesSync");
      System.clearProperty("gemfire.disk.recoverLruValues");
    }
  }
Test populates an offheap heaplru persistent region that contains more data than can fit in
offheap memory. It then recovers the region to demonstrate that recovering this data will not
try to put everything into offheap but instead leave some of it on disk.
geode-core/src/integrationTest/java/org/apache/geode/internal/offheap/OffHeapLRURecoveryRegressionTest.java