You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2016/03/05 01:51:32 UTC

[32/38] incubator-geode git commit: renamed ObjectStoredInMemory to OffHeapStoredObject

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObject.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObject.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObject.java
new file mode 100644
index 0000000..50e1449
--- /dev/null
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObject.java
@@ -0,0 +1,718 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.internal.offheap;
+
+import java.io.DataOutput;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import com.gemstone.gemfire.cache.Region;
+import com.gemstone.gemfire.internal.DSCODE;
+import com.gemstone.gemfire.internal.HeapDataOutputStream;
+import com.gemstone.gemfire.internal.InternalDataSerializer;
+import com.gemstone.gemfire.internal.cache.BytesAndBitsForCompactor;
+import com.gemstone.gemfire.internal.cache.EntryBits;
+import com.gemstone.gemfire.internal.cache.EntryEventImpl;
+import com.gemstone.gemfire.internal.cache.RegionEntry;
+import com.gemstone.gemfire.internal.cache.RegionEntryContext;
+import com.gemstone.gemfire.internal.offheap.annotations.Unretained;
+
+/**
+   * A class that stores a Java object in off-heap memory.
+   * See {@link AddressableMemoryManager} for how off-heap memory
+   * can be allocated, accessed, modified, and freed.
+   * Currently the object stored in this class
+   * is always an entry value of a Region.
+   * Note: this class has a natural ordering that is inconsistent with equals.
+   * Instances of this class should have a short lifetime. We do not store references
+   * to it in the cache. Instead the memoryAddress is stored in a primitive field in
+   * the cache and if used it will then, if needed, create an instance of this class.
+   */
+  public class OffHeapStoredObject extends AbstractStoredObject implements Comparable<OffHeapStoredObject>, MemoryBlock {
+    /**
+     * The memory address of the first byte of addressable memory that belongs to this object
+     */
+    private final long memoryAddress;
+    
+    /**
+     * The useCount, chunkSize, dataSizeDelta, isSerialized, and isCompressed
+     * are all stored in addressable memory in a HEADER. This saves heap memory
+     * by using off heap.
+     */
+    public final static int HEADER_SIZE = 4 + 4;
+    /**
+     * We need to smallest chunk to at least have enough room for a hdr
+     * and for an off heap ref (which is a long).
+     */
+    public final static int MIN_CHUNK_SIZE = HEADER_SIZE + 8;
+    /**
+     * int field.
+     * The number of bytes in this chunk.
+     */
+    private final static int CHUNK_SIZE_OFFSET = 0;
+    /**
+     * Volatile int field
+     * The upper two bits are used for the isSerialized
+     * and isCompressed flags.
+     * The next three bits are unused.
+     * The lower 3 bits of the most significant byte contains a magic number to help us detect
+     * if we are changing the ref count of an object that has been released.
+     * The next byte contains the dataSizeDelta.
+     * The number of bytes of logical data in this chunk.
+     * Since the number of bytes of logical data is always <= chunkSize
+     * and since chunkSize never changes, we have dataSize be
+     * a delta whose max value would be HUGE_MULTIPLE-1.
+     * The lower two bytes contains the use count.
+     */
+    final static int REF_COUNT_OFFSET = 4;
+    /**
+     * The upper two bits are used for the isSerialized
+     * and isCompressed flags.
+     */
+    final static int IS_SERIALIZED_BIT =    0x80000000;
+    final static int IS_COMPRESSED_BIT =    0x40000000;
+    // UNUSED 0x38000000
+    final static int MAGIC_MASK = 0x07000000;
+    final static int MAGIC_NUMBER = 0x05000000;
+    final static int DATA_SIZE_DELTA_MASK = 0x00ff0000;
+    final static int DATA_SIZE_SHIFT = 16;
+    final static int REF_COUNT_MASK =       0x0000ffff;
+    final static int MAX_REF_COUNT = 0xFFFF;
+    final static long FILL_PATTERN = 0x3c3c3c3c3c3c3c3cL;
+    final static byte FILL_BYTE = 0x3c;
+    
+    protected OffHeapStoredObject(long memoryAddress, int chunkSize) {
+      SimpleMemoryAllocatorImpl.validateAddressAndSize(memoryAddress, chunkSize);
+      this.memoryAddress = memoryAddress;
+      setSize(chunkSize);
+      AddressableMemoryManager.writeIntVolatile(getAddress()+REF_COUNT_OFFSET, MAGIC_NUMBER);
+    }
+    public void readyForFree() {
+      AddressableMemoryManager.writeIntVolatile(getAddress()+REF_COUNT_OFFSET, 0);
+    }
+    public void readyForAllocation() {
+      if (!AddressableMemoryManager.writeIntVolatile(getAddress()+REF_COUNT_OFFSET, 0, MAGIC_NUMBER)) {
+        throw new IllegalStateException("Expected 0 but found " + Integer.toHexString(AddressableMemoryManager.readIntVolatile(getAddress()+REF_COUNT_OFFSET)));
+      }
+    }
+    /**
+     * Should only be used by FakeChunk subclass
+     */
+    protected OffHeapStoredObject() {
+      this.memoryAddress = 0L;
+    }
+    
+    /**
+     * Used to create a Chunk given an existing, already allocated,
+     * memoryAddress. The off heap header has already been initialized.
+     */
+    protected OffHeapStoredObject(long memoryAddress) {
+      SimpleMemoryAllocatorImpl.validateAddress(memoryAddress);
+      this.memoryAddress = memoryAddress;
+    }
+    
+    protected OffHeapStoredObject(OffHeapStoredObject chunk) {
+      this.memoryAddress = chunk.memoryAddress;
+    }
+    
+    @Override
+    public void fillSerializedValue(BytesAndBitsForCompactor wrapper, byte userBits) {
+      if (isSerialized()) {
+        userBits = EntryBits.setSerialized(userBits, true);
+      }
+      wrapper.setOffHeapData(this, userBits);
+    }
+    
+    String getShortClassName() {
+      String cname = getClass().getName();
+      return cname.substring(getClass().getPackage().getName().length()+1);
+    }
+
+    @Override
+    public boolean checkDataEquals(@Unretained StoredObject so) {
+      if (this == so) {
+        return true;
+      }
+      if (isSerialized() != so.isSerialized()) {
+        return false;
+      }
+      int mySize = getValueSizeInBytes();
+      if (mySize != so.getValueSizeInBytes()) {
+        return false;
+      }
+      if (!(so instanceof OffHeapStoredObject)) {
+        return false;
+      }
+      OffHeapStoredObject other = (OffHeapStoredObject) so;
+      if (getAddress() == other.getAddress()) {
+        return true;
+      }
+      // We want to be able to do this operation without copying any of the data into the heap.
+      // Hopefully the jvm is smart enough to use our stack for this short lived array.
+      final byte[] dataCache1 = new byte[1024];
+      final byte[] dataCache2 = new byte[dataCache1.length];
+      // TODO OFFHEAP: no need to copy to heap. Just get the address of each and compare each byte. No need to call incReads when reading from address.
+      int i;
+      // inc it twice since we are reading two different objects
+      SimpleMemoryAllocatorImpl.getAllocator().getStats().incReads();
+      SimpleMemoryAllocatorImpl.getAllocator().getStats().incReads();
+      for (i=0; i < mySize-(dataCache1.length-1); i+=dataCache1.length) {
+        this.readDataBytes(i, dataCache1);
+        other.readDataBytes(i, dataCache2);
+        for (int j=0; j < dataCache1.length; j++) {
+          if (dataCache1[j] != dataCache2[j]) {
+            return false;
+          }
+        }
+      }
+      int bytesToRead = mySize-i;
+      if (bytesToRead > 0) {
+        // need to do one more read which will be less than dataCache.length
+        this.readDataBytes(i, dataCache1, 0, bytesToRead);
+        other.readDataBytes(i, dataCache2, 0, bytesToRead);
+        for (int j=0; j < bytesToRead; j++) {
+          if (dataCache1[j] != dataCache2[j]) {
+            return false;
+          }
+        }
+      }
+      return true;
+    }
+    
+    @Override
+    public boolean checkDataEquals(byte[] serializedObj) {
+      // caller was responsible for checking isSerialized
+      int mySize = getValueSizeInBytes();
+      if (mySize != serializedObj.length) {
+        return false;
+      }
+      // We want to be able to do this operation without copying any of the data into the heap.
+      // Hopefully the jvm is smart enough to use our stack for this short lived array.
+      // TODO OFFHEAP: no need to copy to heap. Just get the address of each and compare each byte. No need to call incReads when reading from address.
+      final byte[] dataCache = new byte[1024];
+      int idx=0;
+      int i;
+      SimpleMemoryAllocatorImpl.getAllocator().getStats().incReads();
+      for (i=0; i < mySize-(dataCache.length-1); i+=dataCache.length) {
+        this.readDataBytes(i, dataCache);
+        for (int j=0; j < dataCache.length; j++) {
+          if (dataCache[j] != serializedObj[idx++]) {
+            return false;
+          }
+        }
+      }
+      int bytesToRead = mySize-i;
+      if (bytesToRead > 0) {
+        // need to do one more read which will be less than dataCache.length
+        this.readDataBytes(i, dataCache, 0, bytesToRead);
+        for (int j=0; j < bytesToRead; j++) {
+          if (dataCache[j] != serializedObj[idx++]) {
+            return false;
+          }
+        }
+      }
+      return true;
+    }
+
+    
+    /**
+     * Throw an exception if this chunk is not allocated
+     */
+    public void checkIsAllocated() {
+      int originalBits = AddressableMemoryManager.readIntVolatile(this.memoryAddress+REF_COUNT_OFFSET);
+      if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) {
+        throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits));
+      }
+    }
+    
+    public void incSize(int inc) {
+      setSize(getSize()+inc);
+    }
+    
+    protected void beforeReturningToAllocator() {
+      
+    }
+
+    @Override
+    public int getSize() {
+      return getSize(this.memoryAddress);
+    }
+
+    public void setSize(int size) {
+      setSize(this.memoryAddress, size);
+    }
+
+    @Override
+    public long getAddress() {
+      return this.memoryAddress;
+    }
+    
+    @Override
+    public int getDataSize() {
+      return getDataSize(this.memoryAddress);
+    }
+    
+    protected static int getDataSize(long memoryAdress) {
+      int dataSizeDelta = AddressableMemoryManager.readInt(memoryAdress+REF_COUNT_OFFSET);
+      dataSizeDelta &= DATA_SIZE_DELTA_MASK;
+      dataSizeDelta >>= DATA_SIZE_SHIFT;
+      return getSize(memoryAdress) - dataSizeDelta;
+    }
+    
+    protected long getBaseDataAddress() {
+      return this.memoryAddress+HEADER_SIZE;
+    }
+    protected int getBaseDataOffset() {
+      return 0;
+    }
+    
+    @Override
+    @Unretained
+    public ByteBuffer createDirectByteBuffer() {
+      return AddressableMemoryManager.createDirectByteBuffer(getBaseDataAddress(), getDataSize());
+    }
+    @Override
+    public void sendTo(DataOutput out) throws IOException {
+      if (!this.isCompressed() && out instanceof HeapDataOutputStream) {
+        ByteBuffer bb = createDirectByteBuffer();
+        if (bb != null) {
+          HeapDataOutputStream hdos = (HeapDataOutputStream) out;
+          if (this.isSerialized()) {
+            hdos.write(bb);
+          } else {
+            hdos.writeByte(DSCODE.BYTE_ARRAY);
+            InternalDataSerializer.writeArrayLength(bb.remaining(), hdos);
+            hdos.write(bb);
+          }
+          return;
+        }
+      }
+      super.sendTo(out);
+    }
+    
+    @Override
+    public void sendAsByteArray(DataOutput out) throws IOException {
+      if (!isCompressed() && out instanceof HeapDataOutputStream) {
+        ByteBuffer bb = createDirectByteBuffer();
+        if (bb != null) {
+          HeapDataOutputStream hdos = (HeapDataOutputStream) out;
+          InternalDataSerializer.writeArrayLength(bb.remaining(), hdos);
+          hdos.write(bb);
+          return;
+        }
+      }
+      super.sendAsByteArray(out);
+    }
+       
+    /**
+     * Returns an address that can be used with AddressableMemoryManager to access this object's data.
+     * @param offset the offset from this chunk's first byte of the byte the returned address should point to. Must be >= 0.
+     * @param size the number of bytes that will be read using the returned address. Assertion will use this to verify that all the memory accessed belongs to this chunk. Must be > 0.
+     * @return a memory address that can be used to access this object's data
+     */
+    @Override
+    public long getAddressForReadingData(int offset, int size) {
+      assert offset >= 0 && offset + size <= getDataSize(): "Offset=" + offset + ",size=" + size + ",dataSize=" + getDataSize() + ", chunkSize=" + getSize() + ", but offset + size must be <= " + getDataSize();
+      assert size > 0;
+      long result = getBaseDataAddress() + offset;
+      // validateAddressAndSizeWithinSlab(result, size);
+      return result;
+    }
+    
+    @Override
+    public byte readDataByte(int offset) {
+      assert offset < getDataSize();
+      return AddressableMemoryManager.readByte(getBaseDataAddress() + offset);
+    }
+
+    @Override
+    public void writeDataByte(int offset, byte value) {
+      assert offset < getDataSize();
+      AddressableMemoryManager.writeByte(getBaseDataAddress() + offset, value);
+    }
+
+    @Override
+    public void readDataBytes(int offset, byte[] bytes) {
+      readDataBytes(offset, bytes, 0, bytes.length);
+    }
+
+    @Override
+    public void writeDataBytes(int offset, byte[] bytes) {
+      writeDataBytes(offset, bytes, 0, bytes.length);
+    }
+
+    @Override
+    public void readDataBytes(int offset, byte[] bytes, int bytesOffset, int size) {
+      assert offset+size <= getDataSize();
+      AddressableMemoryManager.readBytes(getBaseDataAddress() + offset, bytes, bytesOffset, size);
+    }
+
+    @Override
+    public void writeDataBytes(int offset, byte[] bytes, int bytesOffset, int size) {
+      assert offset+size <= getDataSize();
+      AddressableMemoryManager.writeBytes(getBaseDataAddress() + offset, bytes, bytesOffset, size);
+    }
+    
+    @Override
+    public void release() {
+      release(this.memoryAddress);
+     }
+
+    @Override
+    public int compareTo(OffHeapStoredObject o) {
+      int result = Integer.signum(getSize() - o.getSize());
+      if (result == 0) {
+        // For the same sized chunks we really don't care about their order
+        // but we need compareTo to only return 0 if the two chunks are identical
+        result = Long.signum(getAddress() - o.getAddress());
+      }
+      return result;
+    }
+    
+    @Override
+    public boolean equals(Object o) {
+      if (o instanceof OffHeapStoredObject) {
+        return getAddress() == ((OffHeapStoredObject) o).getAddress();
+      }
+      return false;
+    }
+    
+    @Override
+    public int hashCode() {
+      long value = this.getAddress();
+      return (int)(value ^ (value >>> 32));
+    }
+
+    public void setSerializedValue(byte[] value) {
+      writeDataBytes(0, value);
+    }
+    
+    public byte[] getDecompressedBytes(RegionEntryContext context) {
+      byte[] result = getCompressedBytes();
+      long time = context.getCachePerfStats().startDecompression();
+      result = context.getCompressor().decompress(result);
+      context.getCachePerfStats().endDecompression(time);      
+      return result;
+    }
+    
+    /**
+     * Returns the raw possibly compressed bytes of this chunk
+     */
+    public byte[] getCompressedBytes() {
+      byte[] result = new byte[getDataSize()];
+      readDataBytes(0, result);
+      //debugLog("reading", true);
+      SimpleMemoryAllocatorImpl.getAllocator().getStats().incReads();
+      return result;
+    }
+    protected byte[] getRawBytes() {
+      byte[] result = getCompressedBytes();
+      // TODO OFFHEAP: change the following to assert !isCompressed();
+      if (isCompressed()) {
+        throw new UnsupportedOperationException();
+      }
+      return result;
+    }
+
+    @Override
+    public byte[] getSerializedValue() {
+      byte [] result = getRawBytes();
+      if (!isSerialized()) {
+        // The object is a byte[]. So we need to make it look like a serialized byte[] in our result
+        result = EntryEventImpl.serialize(result);
+      }
+      return result;
+    }
+    
+    @Override
+    public Object getDeserializedValue(Region r, RegionEntry re) {
+      if (isSerialized()) {
+        // TODO OFFHEAP: debug deserializeChunk
+        return EntryEventImpl.deserialize(getRawBytes());
+        //assert !isCompressed();
+        //return EntryEventImpl.deserializeChunk(this);
+      } else {
+        return getRawBytes();
+      }
+    }
+    
+    /**
+     * We want this to include memory overhead so use getSize() instead of getDataSize().
+     */
+    @Override
+    public int getSizeInBytes() {
+      // Calling getSize includes the off heap header size.
+      // We do not add anything to this since the size of the reference belongs to the region entry size
+      // not the size of this object.
+      return getSize();
+    }
+
+    @Override
+    public int getValueSizeInBytes() {
+      return getDataSize();
+    }
+
+    @Override
+    public boolean isSerialized() {
+      return (AddressableMemoryManager.readInt(this.memoryAddress+REF_COUNT_OFFSET) & IS_SERIALIZED_BIT) != 0;
+    }
+
+    @Override
+    public boolean isCompressed() {
+      return (AddressableMemoryManager.readInt(this.memoryAddress+REF_COUNT_OFFSET) & IS_COMPRESSED_BIT) != 0;
+    }
+
+    @Override
+    public boolean retain() {
+      return retain(this.memoryAddress);
+    }
+
+    @Override
+    public int getRefCount() {
+      return getRefCount(this.memoryAddress);
+    }
+
+    public static int getSize(long memAddr) {
+      SimpleMemoryAllocatorImpl.validateAddress(memAddr);
+      return AddressableMemoryManager.readInt(memAddr+CHUNK_SIZE_OFFSET);
+    }
+    public static void setSize(long memAddr, int size) {
+      SimpleMemoryAllocatorImpl.validateAddressAndSize(memAddr, size);
+      AddressableMemoryManager.writeInt(memAddr+CHUNK_SIZE_OFFSET, size);
+    }
+    public static long getNext(long memAddr) {
+      SimpleMemoryAllocatorImpl.validateAddress(memAddr);
+      return AddressableMemoryManager.readLong(memAddr+HEADER_SIZE);
+    }
+    public static void setNext(long memAddr, long next) {
+      SimpleMemoryAllocatorImpl.validateAddress(memAddr);
+      AddressableMemoryManager.writeLong(memAddr+HEADER_SIZE, next);
+    }
+    
+    /**
+     * Fills the chunk with a repeated byte fill pattern.
+     * @param baseAddress the starting address for a {@link OffHeapStoredObject}.
+     */
+    public static void fill(long baseAddress) {
+      long startAddress = baseAddress + MIN_CHUNK_SIZE;
+      int size = getSize(baseAddress) - MIN_CHUNK_SIZE;
+      
+      AddressableMemoryManager.fill(startAddress, size, FILL_BYTE);
+    }
+    
+    /**
+     * Validates that the fill pattern for this chunk has not been disturbed.  This method
+     * assumes the TINY_MULTIPLE is 8 bytes.
+     * @throws IllegalStateException when the pattern has been violated.
+     */
+    public void validateFill() {
+      assert FreeListManager.TINY_MULTIPLE == 8;
+      
+      long startAddress = getAddress() + MIN_CHUNK_SIZE;
+      int size = getSize() - MIN_CHUNK_SIZE;
+      
+      for(int i = 0;i < size;i += FreeListManager.TINY_MULTIPLE) {
+        if(AddressableMemoryManager.readLong(startAddress + i) != FILL_PATTERN) {
+          throw new IllegalStateException("Fill pattern violated for chunk " + getAddress() + " with size " + getSize());
+        }        
+      }
+    }
+
+    public void setSerialized(boolean isSerialized) {
+      if (isSerialized) {
+        int bits;
+        int originalBits;
+        do {
+          originalBits = AddressableMemoryManager.readIntVolatile(this.memoryAddress+REF_COUNT_OFFSET);
+          if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) {
+            throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits));
+          }
+          bits = originalBits | IS_SERIALIZED_BIT;
+        } while (!AddressableMemoryManager.writeIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, originalBits, bits));
+      }
+    }
+    public void setCompressed(boolean isCompressed) {
+      if (isCompressed) {
+        int bits;
+        int originalBits;
+        do {
+          originalBits = AddressableMemoryManager.readIntVolatile(this.memoryAddress+REF_COUNT_OFFSET);
+          if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) {
+            throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits));
+          }
+          bits = originalBits | IS_COMPRESSED_BIT;
+        } while (!AddressableMemoryManager.writeIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, originalBits, bits));
+      }
+    }
+    public void setDataSize(int dataSize) { // KIRK
+      assert dataSize <= getSize();
+      int delta = getSize() - dataSize;
+      assert delta <= (DATA_SIZE_DELTA_MASK >> DATA_SIZE_SHIFT);
+      delta <<= DATA_SIZE_SHIFT;
+      int bits;
+      int originalBits;
+      do {
+        originalBits = AddressableMemoryManager.readIntVolatile(this.memoryAddress+REF_COUNT_OFFSET);
+        if ((originalBits&MAGIC_MASK) != MAGIC_NUMBER) {
+          throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(originalBits));
+        }
+        bits = originalBits;
+        bits &= ~DATA_SIZE_DELTA_MASK; // clear the old dataSizeDelta bits
+        bits |= delta; // set the dataSizeDelta bits to the new delta value
+      } while (!AddressableMemoryManager.writeIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, originalBits, bits));
+    }
+    
+    public void initializeUseCount() {
+      int rawBits;
+      do {
+        rawBits = AddressableMemoryManager.readIntVolatile(this.memoryAddress+REF_COUNT_OFFSET);
+        if ((rawBits&MAGIC_MASK) != MAGIC_NUMBER) {
+          throw new IllegalStateException("It looks like this off heap memory was already freed. rawBits=" + Integer.toHexString(rawBits));
+        }
+        int uc = rawBits & REF_COUNT_MASK;
+        if (uc != 0) {
+          throw new IllegalStateException("Expected use count to be zero but it was: " + uc + " rawBits=0x" + Integer.toHexString(rawBits));
+        }
+      } while (!AddressableMemoryManager.writeIntVolatile(this.memoryAddress+REF_COUNT_OFFSET, rawBits, rawBits+1));
+    }
+
+    public static int getRefCount(long memAddr) {
+      return AddressableMemoryManager.readInt(memAddr+REF_COUNT_OFFSET) & REF_COUNT_MASK;
+    }
+
+    public static boolean retain(long memAddr) {
+      SimpleMemoryAllocatorImpl.validateAddress(memAddr);
+      int uc;
+      int rawBits;
+      int retryCount = 0;
+      do {
+        rawBits = AddressableMemoryManager.readIntVolatile(memAddr+REF_COUNT_OFFSET);
+        if ((rawBits&MAGIC_MASK) != MAGIC_NUMBER) {
+          // same as uc == 0
+          // TODO MAGIC_NUMBER rethink its use and interaction with compactor fragments
+          return false;
+        }
+        uc = rawBits & REF_COUNT_MASK;
+        if (uc == MAX_REF_COUNT) {
+          throw new IllegalStateException("Maximum use count exceeded. rawBits=" + Integer.toHexString(rawBits));
+        } else if (uc == 0) {
+          return false;
+        }
+        retryCount++;
+        if (retryCount > 1000) {
+          throw new IllegalStateException("tried to write " + (rawBits+1) + " to @" + Long.toHexString(memAddr) + " 1,000 times.");
+        }
+      } while (!AddressableMemoryManager.writeIntVolatile(memAddr+REF_COUNT_OFFSET, rawBits, rawBits+1));
+      //debugLog("use inced ref count " + (uc+1) + " @" + Long.toHexString(memAddr), true);
+      if (ReferenceCountHelper.trackReferenceCounts()) {
+        ReferenceCountHelper.refCountChanged(memAddr, false, uc+1);
+      }
+
+      return true;
+    }
+    public static void release(final long memAddr) {
+      release(memAddr, null);
+    }
+    static void release(final long memAddr, FreeListManager freeListManager) {
+      SimpleMemoryAllocatorImpl.validateAddress(memAddr);
+      int newCount;
+      int rawBits;
+      boolean returnToAllocator;
+      do {
+        returnToAllocator = false;
+        rawBits = AddressableMemoryManager.readIntVolatile(memAddr+REF_COUNT_OFFSET);
+        if ((rawBits&MAGIC_MASK) != MAGIC_NUMBER) {
+          String msg = "It looks like off heap memory @" + Long.toHexString(memAddr) + " was already freed. rawBits=" + Integer.toHexString(rawBits) + " history=" + ReferenceCountHelper.getFreeRefCountInfo(memAddr);
+          //debugLog(msg, true);
+          throw new IllegalStateException(msg);
+        }
+        int curCount = rawBits&REF_COUNT_MASK;
+        if ((curCount) == 0) {
+          //debugLog("too many frees @" + Long.toHexString(memAddr), true);
+          throw new IllegalStateException("Memory has already been freed." + " history=" + ReferenceCountHelper.getFreeRefCountInfo(memAddr) /*+ System.identityHashCode(this)*/);
+        }
+        if (curCount == 1) {
+          newCount = 0; // clear the use count, bits, and the delta size since it will be freed.
+          returnToAllocator = true;
+        } else {
+          newCount = rawBits-1;
+        }
+      } while (!AddressableMemoryManager.writeIntVolatile(memAddr+REF_COUNT_OFFSET, rawBits, newCount));
+      //debugLog("free deced ref count " + (newCount&USE_COUNT_MASK) + " @" + Long.toHexString(memAddr), true);
+      if (returnToAllocator ) {
+       if (ReferenceCountHelper.trackReferenceCounts()) {
+          if (ReferenceCountHelper.trackFreedReferenceCounts()) {
+            ReferenceCountHelper.refCountChanged(memAddr, true, newCount&REF_COUNT_MASK);
+          }
+          ReferenceCountHelper.freeRefCountInfo(memAddr);
+        }
+        if (freeListManager == null) {
+          freeListManager = SimpleMemoryAllocatorImpl.getAllocator().getFreeListManager();
+        }
+        freeListManager.free(memAddr);
+      } else {
+        if (ReferenceCountHelper.trackReferenceCounts()) {
+          ReferenceCountHelper.refCountChanged(memAddr, true, newCount&REF_COUNT_MASK);
+        }
+      }
+    }
+    
+    @Override
+    public String toString() {
+      return super.toString() + ":<dataSize=" + getDataSize() + " refCount=" + getRefCount() + " addr=" + Long.toHexString(getAddress()) + ">";
+    }
+    
+    @Override
+    public State getState() {
+      if (getRefCount() > 0) {
+        return State.ALLOCATED;
+      } else {
+        return State.DEALLOCATED;
+      }
+    }
+    @Override
+    public MemoryBlock getNextBlock() {
+      throw new UnsupportedOperationException();
+    }
+    @Override
+    public int getBlockSize() {
+      return getSize();
+    }
+    @Override
+    public int getSlabId() {
+      throw new UnsupportedOperationException();
+    }
+    @Override
+    public int getFreeListId() {
+      return -1;
+    }
+    @Override
+    public String getDataType() {
+      return null;
+    }
+    @Override
+    public Object getDataValue() {
+      return null;
+    }
+    public StoredObject slice(int position, int limit) {
+      return new ObjectChunkSlice(this, position, limit);
+    }
+    @Override
+    public boolean hasRefCount() {
+      return true;
+    }
+  }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
index 584b6de..0198f02 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorImpl.java
@@ -224,20 +224,20 @@ public class SimpleMemoryAllocatorImpl implements MemoryAllocator {
     this.stats.incFreeMemory(this.freeList.getTotalMemory());
   }
   
-  public List<ObjectStoredInMemory> getLostChunks() {
-    List<ObjectStoredInMemory> liveChunks = this.freeList.getLiveChunks();
-    List<ObjectStoredInMemory> regionChunks = getRegionLiveChunks();
-    Set<ObjectStoredInMemory> liveChunksSet = new HashSet<>(liveChunks);
-    Set<ObjectStoredInMemory> regionChunksSet = new HashSet<>(regionChunks);
+  public List<OffHeapStoredObject> getLostChunks() {
+    List<OffHeapStoredObject> liveChunks = this.freeList.getLiveChunks();
+    List<OffHeapStoredObject> regionChunks = getRegionLiveChunks();
+    Set<OffHeapStoredObject> liveChunksSet = new HashSet<>(liveChunks);
+    Set<OffHeapStoredObject> regionChunksSet = new HashSet<>(regionChunks);
     liveChunksSet.removeAll(regionChunksSet);
-    return new ArrayList<ObjectStoredInMemory>(liveChunksSet);
+    return new ArrayList<OffHeapStoredObject>(liveChunksSet);
   }
   
   /**
    * Returns a possibly empty list that contains all the Chunks used by regions.
    */
-  private List<ObjectStoredInMemory> getRegionLiveChunks() {
-    ArrayList<ObjectStoredInMemory> result = new ArrayList<ObjectStoredInMemory>();
+  private List<OffHeapStoredObject> getRegionLiveChunks() {
+    ArrayList<OffHeapStoredObject> result = new ArrayList<OffHeapStoredObject>();
     RegionService gfc = GemFireCacheImpl.getInstance();
     if (gfc != null) {
       Iterator<Region<?,?>> rootIt = gfc.rootRegions().iterator();
@@ -253,7 +253,7 @@ public class SimpleMemoryAllocatorImpl implements MemoryAllocator {
     return result;
   }
 
-  private void getRegionLiveChunks(Region<?,?> r, List<ObjectStoredInMemory> result) {
+  private void getRegionLiveChunks(Region<?,?> r, List<OffHeapStoredObject> result) {
     if (r.getAttributes().getOffHeap()) {
 
       if (r instanceof PartitionedRegion) {
@@ -277,7 +277,7 @@ public class SimpleMemoryAllocatorImpl implements MemoryAllocator {
 
   }
   
-  private void basicGetRegionLiveChunks(LocalRegion r, List<ObjectStoredInMemory> result) {
+  private void basicGetRegionLiveChunks(LocalRegion r, List<OffHeapStoredObject> result) {
     for (Object key : r.keySet()) {
       RegionEntry re = ((LocalRegion) r).getRegionEntry(key);
       if (re != null) {
@@ -286,15 +286,15 @@ public class SimpleMemoryAllocatorImpl implements MemoryAllocator {
          */
         @Unretained(OffHeapIdentifier.GATEWAY_SENDER_EVENT_IMPL_VALUE)
         Object value = re._getValue();
-        if (value instanceof ObjectStoredInMemory) {
-          result.add((ObjectStoredInMemory) value);
+        if (value instanceof OffHeapStoredObject) {
+          result.add((OffHeapStoredObject) value);
         }
       }
     }
   }
 
-  private ObjectStoredInMemory allocateChunk(int size) {
-    ObjectStoredInMemory result = this.freeList.allocate(size);
+  private OffHeapStoredObject allocateChunk(int size) {
+    OffHeapStoredObject result = this.freeList.allocate(size);
     int resultSize = result.getSize();
     stats.incObjects(1);
     stats.incUsedMemory(resultSize);
@@ -309,7 +309,7 @@ public class SimpleMemoryAllocatorImpl implements MemoryAllocator {
   @Override
   public StoredObject allocate(int size) {
     //System.out.println("allocating " + size);
-    ObjectStoredInMemory result = allocateChunk(size);
+    OffHeapStoredObject result = allocateChunk(size);
     //("allocated off heap object of size " + size + " @" + Long.toHexString(result.getMemoryAddress()), true);
     return result;
   }
@@ -332,7 +332,7 @@ public class SimpleMemoryAllocatorImpl implements MemoryAllocator {
     if (addr != 0L) {
       return new TinyStoredObject(addr);
     }
-    ObjectStoredInMemory result = allocateChunk(v.length);
+    OffHeapStoredObject result = allocateChunk(v.length);
     //debugLog("allocated off heap object of size " + v.length + " @" + Long.toHexString(result.getMemoryAddress()), true);
     //debugLog("allocated off heap object of size " + v.length + " @" + Long.toHexString(result.getMemoryAddress()) +  "chunkSize=" + result.getSize() + " isSerialized=" + isSerialized + " v=" + Arrays.toString(v), true);
     result.setSerializedValue(v);
@@ -492,11 +492,11 @@ public class SimpleMemoryAllocatorImpl implements MemoryAllocator {
   }
 
   public synchronized List<MemoryBlock> getOrphans() {
-    List<ObjectStoredInMemory> liveChunks = this.freeList.getLiveChunks();
-    List<ObjectStoredInMemory> regionChunks = getRegionLiveChunks();
+    List<OffHeapStoredObject> liveChunks = this.freeList.getLiveChunks();
+    List<OffHeapStoredObject> regionChunks = getRegionLiveChunks();
     liveChunks.removeAll(regionChunks);
     List<MemoryBlock> orphans = new ArrayList<MemoryBlock>();
-    for (ObjectStoredInMemory chunk: liveChunks) {
+    for (OffHeapStoredObject chunk: liveChunks) {
       orphans.add(new MemoryBlockNode(this, chunk));
     }
     Collections.sort(orphans,

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SyncChunkStack.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SyncChunkStack.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SyncChunkStack.java
index 4c34441..83cb7df 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SyncChunkStack.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/SyncChunkStack.java
@@ -42,7 +42,7 @@ public class SyncChunkStack {
     assert e != 0;
     SimpleMemoryAllocatorImpl.validateAddress(e);
     synchronized (this) {
-      ObjectStoredInMemory.setNext(e, this.topAddr);
+      OffHeapStoredObject.setNext(e, this.topAddr);
       this.topAddr = e;
     }
   }
@@ -51,7 +51,7 @@ public class SyncChunkStack {
     synchronized (this) {
       result = this.topAddr;
       if (result != 0L) {
-        this.topAddr = ObjectStoredInMemory.getNext(result);
+        this.topAddr = OffHeapStoredObject.getNext(result);
       }
     }
     return result;
@@ -85,8 +85,8 @@ public class SyncChunkStack {
       concurrentModDetected = false;
       addr = headAddr;
       while (addr != 0L) {
-        int curSize = ObjectStoredInMemory.getSize(addr);
-        addr = ObjectStoredInMemory.getNext(addr);
+        int curSize = OffHeapStoredObject.getSize(addr);
+        addr = OffHeapStoredObject.getNext(addr);
         testHookDoConcurrentModification();
         long curHead = this.topAddr;
         if (curHead != headAddr) {
@@ -113,8 +113,8 @@ public class SyncChunkStack {
       result = 0;
       addr = headAddr;
       while (addr != 0L) {
-        result += ObjectStoredInMemory.getSize(addr);
-        addr = ObjectStoredInMemory.getNext(addr);
+        result += OffHeapStoredObject.getSize(addr);
+        addr = OffHeapStoredObject.getNext(addr);
         testHookDoConcurrentModification();
         long curHead = this.topAddr;
         if (curHead != headAddr) {

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/ChunkValueWrapperJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/ChunkValueWrapperJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/ChunkValueWrapperJUnitTest.java
index df48ac2..19b1ac1 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/ChunkValueWrapperJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/ChunkValueWrapperJUnitTest.java
@@ -29,7 +29,6 @@ import org.junit.experimental.categories.Category;
 
 import com.gemstone.gemfire.internal.cache.DiskEntry.Helper.OffHeapValueWrapper;
 import com.gemstone.gemfire.internal.cache.DiskEntry.Helper.Flushable;
-import com.gemstone.gemfire.internal.offheap.ObjectStoredInMemory;
 import com.gemstone.gemfire.internal.offheap.NullOffHeapMemoryStats;
 import com.gemstone.gemfire.internal.offheap.NullOutOfOffHeapMemoryListener;
 import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl;

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
index a63ce9a..84d7fc7 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/cache/OldValueImporterTestBase.java
@@ -26,7 +26,7 @@ import org.junit.Test;
 
 import com.gemstone.gemfire.internal.HeapDataOutputStream;
 import com.gemstone.gemfire.internal.cache.EntryEventImpl.OldValueImporter;
-import com.gemstone.gemfire.internal.offheap.ObjectStoredInMemory;
+import com.gemstone.gemfire.internal.offheap.OffHeapStoredObject;
 import com.gemstone.gemfire.internal.offheap.TinyStoredObject;
 import com.gemstone.gemfire.internal.offheap.NullOffHeapMemoryStats;
 import com.gemstone.gemfire.internal.offheap.NullOutOfOffHeapMemoryListener;
@@ -130,7 +130,7 @@ public abstract class OldValueImporterTestBase {
           SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)});
       try {
         byte[] baValue = new byte[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
-        ObjectStoredInMemory baValueSO = (ObjectStoredInMemory) sma.allocateAndInitialize(baValue, false, false);
+        OffHeapStoredObject baValueSO = (OffHeapStoredObject) sma.allocateAndInitialize(baValue, false, false);
         OldValueImporter omsg = createImporter();
         omsg.importOldObject(baValueSO, false);
         hdos = new HeapDataOutputStream(bytes);
@@ -166,7 +166,7 @@ public abstract class OldValueImporterTestBase {
       try {
         String baValue = "12345678";
         byte[] baValueBlob = BlobHelper.serializeToBlob(baValue);
-        ObjectStoredInMemory baValueSO = (ObjectStoredInMemory) sma.allocateAndInitialize(baValueBlob, true, false);
+        OffHeapStoredObject baValueSO = (OffHeapStoredObject) sma.allocateAndInitialize(baValueBlob, true, false);
         OldValueImporter omsg = createImporter();
         omsg.importOldObject(baValueSO, true);
         hdos = new HeapDataOutputStream(bytes);

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FragmentJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FragmentJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FragmentJUnitTest.java
index 3a3daf7..569b003 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FragmentJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FragmentJUnitTest.java
@@ -211,7 +211,7 @@ public class FragmentJUnitTest {
     Long fragmentAddress = fragment.getAddress();
     byte[] bytes = new byte[(int)OffHeapStorage.MIN_SLAB_SIZE];
     byte[] expectedBytes = new byte[(int)OffHeapStorage.MIN_SLAB_SIZE];
-    Arrays.fill(expectedBytes, ObjectStoredInMemory.FILL_BYTE);;
+    Arrays.fill(expectedBytes, OffHeapStoredObject.FILL_BYTE);;
     fragment.fill();
     AddressableMemoryManager.readBytes(fragmentAddress, bytes, 0, (int)OffHeapStorage.MIN_SLAB_SIZE);
     assertThat(bytes, is(equalTo(expectedBytes)));

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
index 4c91918..8beb6c8 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListManagerTest.java
@@ -104,12 +104,12 @@ public class FreeListManagerTest {
     setUpSingleSlabManager();
     int tinySize = 10;
 
-    ObjectStoredInMemory c = this.freeListManager.allocate(tinySize);
+    OffHeapStoredObject c = this.freeListManager.allocate(tinySize);
     
     validateChunkSizes(c, tinySize);
   }
   
-  private void validateChunkSizes(ObjectStoredInMemory c, int dataSize) {
+  private void validateChunkSizes(OffHeapStoredObject c, int dataSize) {
     assertThat(c).isNotNull();
     assertThat(c.getDataSize()).isEqualTo(dataSize);
     assertThat(c.getSize()).isEqualTo(computeExpectedSize(dataSize));
@@ -120,8 +120,8 @@ public class FreeListManagerTest {
     setUpSingleSlabManager();
     int tinySize = 10;
     
-    ObjectStoredInMemory c = this.freeListManager.allocate(tinySize);
-    ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    OffHeapStoredObject c = this.freeListManager.allocate(tinySize);
+    OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     c = this.freeListManager.allocate(tinySize);
 
     validateChunkSizes(c, tinySize);
@@ -132,8 +132,8 @@ public class FreeListManagerTest {
     setUpSingleSlabManager();
     int dataSize = 10;
     
-    ObjectStoredInMemory c = this.freeListManager.allocate(dataSize);
-    ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    OffHeapStoredObject c = this.freeListManager.allocate(dataSize);
+    OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     this.freeListManager.allocate(dataSize);
     // free list will now be empty
     c = this.freeListManager.allocate(dataSize);
@@ -146,7 +146,7 @@ public class FreeListManagerTest {
     setUpSingleSlabManager();
     int hugeSize = FreeListManager.MAX_TINY+1;
 
-    ObjectStoredInMemory c = this.freeListManager.allocate(hugeSize);
+    OffHeapStoredObject c = this.freeListManager.allocate(hugeSize);
 
     validateChunkSizes(c, hugeSize);
   }
@@ -156,8 +156,8 @@ public class FreeListManagerTest {
     setUpSingleSlabManager();
     int dataSize = FreeListManager.MAX_TINY+1;
     
-    ObjectStoredInMemory c = this.freeListManager.allocate(dataSize);
-    ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    OffHeapStoredObject c = this.freeListManager.allocate(dataSize);
+    OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     this.freeListManager.allocate(dataSize);
     // free list will now be empty
     c = this.freeListManager.allocate(dataSize);
@@ -170,8 +170,8 @@ public class FreeListManagerTest {
     setUpSingleSlabManager();
     int dataSize = FreeListManager.MAX_TINY+1+1024;
     
-    ObjectStoredInMemory c = this.freeListManager.allocate(dataSize);
-    ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    OffHeapStoredObject c = this.freeListManager.allocate(dataSize);
+    OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     dataSize = FreeListManager.MAX_TINY+1;
     c = this.freeListManager.allocate(dataSize);
     
@@ -188,8 +188,8 @@ public class FreeListManagerTest {
     setUpSingleSlabManager();
     int dataSize = 10;
     
-    ObjectStoredInMemory c = this.freeListManager.allocate(dataSize);
-    ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    OffHeapStoredObject c = this.freeListManager.allocate(dataSize);
+    OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     
     assertThat(this.freeListManager.getFreeTinyMemory()).isEqualTo(computeExpectedSize(dataSize));
   }
@@ -199,13 +199,13 @@ public class FreeListManagerTest {
     setUpSingleSlabManager();
     int dataSize = 10;
     
-    ObjectStoredInMemory c = this.freeListManager.allocate(dataSize);
-    ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    OffHeapStoredObject c = this.freeListManager.allocate(dataSize);
+    OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     
     int dataSize2 = 100;
     
-    ObjectStoredInMemory c2 = this.freeListManager.allocate(dataSize2);
-    ObjectStoredInMemory.release(c2.getAddress(), this.freeListManager);
+    OffHeapStoredObject c2 = this.freeListManager.allocate(dataSize2);
+    OffHeapStoredObject.release(c2.getAddress(), this.freeListManager);
     
     assertThat(this.freeListManager.getFreeTinyMemory()).isEqualTo(computeExpectedSize(dataSize)+computeExpectedSize(dataSize2));
   }
@@ -221,8 +221,8 @@ public class FreeListManagerTest {
     setUpSingleSlabManager();
     int dataSize = FreeListManager.MAX_TINY+1+1024;
     
-    ObjectStoredInMemory c = this.freeListManager.allocate(dataSize);
-    ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    OffHeapStoredObject c = this.freeListManager.allocate(dataSize);
+    OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     
     assertThat(this.freeListManager.getFreeHugeMemory()).isEqualTo(computeExpectedSize(dataSize));
   }
@@ -237,7 +237,7 @@ public class FreeListManagerTest {
   @Test
   public void freeFragmentMemorySomeOfFragmentAllocated() {
     setUpSingleSlabManager();
-    ObjectStoredInMemory c = this.freeListManager.allocate(DEFAULT_SLAB_SIZE/4-8);
+    OffHeapStoredObject c = this.freeListManager.allocate(DEFAULT_SLAB_SIZE/4-8);
     
     assertThat(this.freeListManager.getFreeFragmentMemory()).isEqualTo((DEFAULT_SLAB_SIZE/4)*3);
   }
@@ -245,13 +245,13 @@ public class FreeListManagerTest {
   @Test
   public void freeFragmentMemoryMostOfFragmentAllocated() {
     setUpSingleSlabManager();
-    ObjectStoredInMemory c = this.freeListManager.allocate(DEFAULT_SLAB_SIZE-8-8);
+    OffHeapStoredObject c = this.freeListManager.allocate(DEFAULT_SLAB_SIZE-8-8);
     
     assertThat(this.freeListManager.getFreeFragmentMemory()).isZero();
   }
   
   private int computeExpectedSize(int dataSize) {
-    return ((dataSize + ObjectStoredInMemory.HEADER_SIZE + 7) / 8) * 8;
+    return ((dataSize + OffHeapStoredObject.HEADER_SIZE + 7) / 8) * 8;
   }
 
   @Test(expected = AssertionError.class)
@@ -288,13 +288,13 @@ public class FreeListManagerTest {
         new SlabImpl(SMALL_SLAB), 
         new SlabImpl(MEDIUM_SLAB), 
         slab});
-    ArrayList<ObjectStoredInMemory> chunks = new ArrayList<>();
+    ArrayList<OffHeapStoredObject> chunks = new ArrayList<>();
     chunks.add(this.freeListManager.allocate(SMALL_SLAB-8+1));
     chunks.add(this.freeListManager.allocate(DEFAULT_SLAB_SIZE/2-8));
     chunks.add(this.freeListManager.allocate(DEFAULT_SLAB_SIZE/2-8));
     chunks.add(this.freeListManager.allocate(SMALL_SLAB-8+1));
-    for (ObjectStoredInMemory c: chunks) {
-      ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    for (OffHeapStoredObject c: chunks) {
+      OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     }
     this.freeListManager.firstCompact = false;
     assertThat(this.freeListManager.compact(DEFAULT_SLAB_SIZE+1)).isFalse();
@@ -310,13 +310,13 @@ public class FreeListManagerTest {
         new SlabImpl(SMALL_SLAB), 
         new SlabImpl(MEDIUM_SLAB), 
         slab});
-    ArrayList<ObjectStoredInMemory> chunks = new ArrayList<>();
+    ArrayList<OffHeapStoredObject> chunks = new ArrayList<>();
     chunks.add(this.freeListManager.allocate(SMALL_SLAB-8+1));
     chunks.add(this.freeListManager.allocate(DEFAULT_SLAB_SIZE/2-8));
     chunks.add(this.freeListManager.allocate(DEFAULT_SLAB_SIZE/2-8));
     chunks.add(this.freeListManager.allocate(SMALL_SLAB-8+1));
-    for (ObjectStoredInMemory c: chunks) {
-      ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    for (OffHeapStoredObject c: chunks) {
+      OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     }
     
     assertThat(this.freeListManager.compact(DEFAULT_SLAB_SIZE)).isTrue();
@@ -333,13 +333,13 @@ public class FreeListManagerTest {
         new SlabImpl(SMALL_SLAB), 
         new SlabImpl(MEDIUM_SLAB), 
         slab});
-    ArrayList<ObjectStoredInMemory> chunks = new ArrayList<>();
+    ArrayList<OffHeapStoredObject> chunks = new ArrayList<>();
     chunks.add(this.freeListManager.allocate(SMALL_SLAB-8+1));
     this.freeListManager.allocate(DEFAULT_SLAB_SIZE/2-8);
     chunks.add(this.freeListManager.allocate(DEFAULT_SLAB_SIZE/2-8));
     this.freeListManager.allocate(SMALL_SLAB-8+1);
-    for (ObjectStoredInMemory c: chunks) {
-      ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    for (OffHeapStoredObject c: chunks) {
+      OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     }
     
     assertThat(this.freeListManager.compact(DEFAULT_SLAB_SIZE/2)).isTrue();
@@ -348,7 +348,7 @@ public class FreeListManagerTest {
   @Test
   public void compactAfterAllocatingAll() {
     setUpSingleSlabManager();
-    ObjectStoredInMemory c = freeListManager.allocate(DEFAULT_SLAB_SIZE-8);
+    OffHeapStoredObject c = freeListManager.allocate(DEFAULT_SLAB_SIZE-8);
     this.freeListManager.firstCompact = false;
     assertThat(this.freeListManager.compact(1)).isFalse();
     // call compact twice for extra code coverage
@@ -359,46 +359,46 @@ public class FreeListManagerTest {
   @Test
   public void afterAllocatingAllOneSizeCompactToAllocateDifferentSize() {
     setUpSingleSlabManager();
-    ArrayList<ObjectStoredInMemory> chunksToFree = new ArrayList<>();
-    ArrayList<ObjectStoredInMemory> chunksToFreeLater = new ArrayList<>();
+    ArrayList<OffHeapStoredObject> chunksToFree = new ArrayList<>();
+    ArrayList<OffHeapStoredObject> chunksToFreeLater = new ArrayList<>();
     int ALLOCATE_COUNT = 1000;
-    ObjectStoredInMemory bigChunk = freeListManager.allocate(DEFAULT_SLAB_SIZE-8-(ALLOCATE_COUNT*32)-256-256);
+    OffHeapStoredObject bigChunk = freeListManager.allocate(DEFAULT_SLAB_SIZE-8-(ALLOCATE_COUNT*32)-256-256);
     for (int i=0; i < ALLOCATE_COUNT; i++) {
-      ObjectStoredInMemory c = freeListManager.allocate(24);
+      OffHeapStoredObject c = freeListManager.allocate(24);
       if (i%3 != 2) {
         chunksToFree.add(c);
       } else {
         chunksToFreeLater.add(c);
       }
     }
-    ObjectStoredInMemory c1 = freeListManager.allocate(64-8);
-    ObjectStoredInMemory c2 = freeListManager.allocate(64-8);
-    ObjectStoredInMemory c3 = freeListManager.allocate(64-8);
-    ObjectStoredInMemory c4 = freeListManager.allocate(64-8);
+    OffHeapStoredObject c1 = freeListManager.allocate(64-8);
+    OffHeapStoredObject c2 = freeListManager.allocate(64-8);
+    OffHeapStoredObject c3 = freeListManager.allocate(64-8);
+    OffHeapStoredObject c4 = freeListManager.allocate(64-8);
 
-    ObjectStoredInMemory mediumChunk1 = freeListManager.allocate(128-8);
-    ObjectStoredInMemory mediumChunk2 = freeListManager.allocate(128-8);
+    OffHeapStoredObject mediumChunk1 = freeListManager.allocate(128-8);
+    OffHeapStoredObject mediumChunk2 = freeListManager.allocate(128-8);
 
-    ObjectStoredInMemory.release(bigChunk.getAddress(), freeListManager);
+    OffHeapStoredObject.release(bigChunk.getAddress(), freeListManager);
     int s = chunksToFree.size();
     for (int i=s/2; i >=0; i--) {
-      ObjectStoredInMemory c = chunksToFree.get(i);
-      ObjectStoredInMemory.release(c.getAddress(), freeListManager);
+      OffHeapStoredObject c = chunksToFree.get(i);
+      OffHeapStoredObject.release(c.getAddress(), freeListManager);
     }
     for (int i=(s/2)+1; i < s; i++) {
-      ObjectStoredInMemory c = chunksToFree.get(i);
-      ObjectStoredInMemory.release(c.getAddress(), freeListManager);
+      OffHeapStoredObject c = chunksToFree.get(i);
+      OffHeapStoredObject.release(c.getAddress(), freeListManager);
     }
-    ObjectStoredInMemory.release(c3.getAddress(), freeListManager);
-    ObjectStoredInMemory.release(c1.getAddress(), freeListManager);
-    ObjectStoredInMemory.release(c2.getAddress(), freeListManager);
-    ObjectStoredInMemory.release(c4.getAddress(), freeListManager);
-    ObjectStoredInMemory.release(mediumChunk1.getAddress(), freeListManager);
-    ObjectStoredInMemory.release(mediumChunk2.getAddress(), freeListManager);
+    OffHeapStoredObject.release(c3.getAddress(), freeListManager);
+    OffHeapStoredObject.release(c1.getAddress(), freeListManager);
+    OffHeapStoredObject.release(c2.getAddress(), freeListManager);
+    OffHeapStoredObject.release(c4.getAddress(), freeListManager);
+    OffHeapStoredObject.release(mediumChunk1.getAddress(), freeListManager);
+    OffHeapStoredObject.release(mediumChunk2.getAddress(), freeListManager);
     this.freeListManager.firstCompact = false;
     assertThat(freeListManager.compact(DEFAULT_SLAB_SIZE-(ALLOCATE_COUNT*32))).isFalse();
     for (int i=0; i < ((256*2)/96); i++) {
-      ObjectStoredInMemory.release(chunksToFreeLater.get(i).getAddress(), freeListManager);
+      OffHeapStoredObject.release(chunksToFreeLater.get(i).getAddress(), freeListManager);
     }
     assertThat(freeListManager.compact(DEFAULT_SLAB_SIZE-(ALLOCATE_COUNT*32))).isTrue();
   }
@@ -407,14 +407,14 @@ public class FreeListManagerTest {
   public void afterAllocatingAndFreeingCompact() {
     int slabSize = 1024*3;
     setUpSingleSlabManager(slabSize);
-    ObjectStoredInMemory bigChunk1 = freeListManager.allocate(slabSize/3-8);
-    ObjectStoredInMemory bigChunk2 = freeListManager.allocate(slabSize/3-8);
-    ObjectStoredInMemory bigChunk3 = freeListManager.allocate(slabSize/3-8);
+    OffHeapStoredObject bigChunk1 = freeListManager.allocate(slabSize/3-8);
+    OffHeapStoredObject bigChunk2 = freeListManager.allocate(slabSize/3-8);
+    OffHeapStoredObject bigChunk3 = freeListManager.allocate(slabSize/3-8);
     this.freeListManager.firstCompact = false;
     assertThat(freeListManager.compact(1)).isFalse();
-    ObjectStoredInMemory.release(bigChunk3.getAddress(), freeListManager);
-    ObjectStoredInMemory.release(bigChunk2.getAddress(), freeListManager);
-    ObjectStoredInMemory.release(bigChunk1.getAddress(), freeListManager);
+    OffHeapStoredObject.release(bigChunk3.getAddress(), freeListManager);
+    OffHeapStoredObject.release(bigChunk2.getAddress(), freeListManager);
+    OffHeapStoredObject.release(bigChunk1.getAddress(), freeListManager);
     assertThat(freeListManager.compact(slabSize)).isTrue();
   }
   
@@ -422,8 +422,8 @@ public class FreeListManagerTest {
   public void compactWithEmptyTinyFreeList() {
     setUpSingleSlabManager();
     Fragment originalFragment = this.freeListManager.getFragmentList().get(0);
-    ObjectStoredInMemory c = freeListManager.allocate(16);
-    ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    OffHeapStoredObject c = freeListManager.allocate(16);
+    OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     c = freeListManager.allocate(16);
     this.freeListManager.firstCompact = false;
     assertThat(this.freeListManager.compact(1)).isTrue();
@@ -443,8 +443,8 @@ public class FreeListManagerTest {
         new SlabImpl(SMALL_SLAB), 
         new SlabImpl(MEDIUM_SLAB), 
         slab});
-    this.freeListManager.allocate(DEFAULT_SLAB_SIZE-8-(ObjectStoredInMemory.MIN_CHUNK_SIZE-1));
-    this.freeListManager.allocate(MEDIUM_SLAB-8-(ObjectStoredInMemory.MIN_CHUNK_SIZE-1));
+    this.freeListManager.allocate(DEFAULT_SLAB_SIZE-8-(OffHeapStoredObject.MIN_CHUNK_SIZE-1));
+    this.freeListManager.allocate(MEDIUM_SLAB-8-(OffHeapStoredObject.MIN_CHUNK_SIZE-1));
     
     assertThat(this.freeListManager.compact(SMALL_SLAB)).isTrue();
   }
@@ -672,9 +672,9 @@ public class FreeListManagerTest {
     Slab chunk = new SlabImpl(96);
     long address = chunk.getMemoryAddress();
     this.freeListManager = createFreeListManager(ma, new Slab[] {chunk});
-    ObjectStoredInMemory c = this.freeListManager.allocate(24);
-    ObjectStoredInMemory c2 = this.freeListManager.allocate(24);
-    ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    OffHeapStoredObject c = this.freeListManager.allocate(24);
+    OffHeapStoredObject c2 = this.freeListManager.allocate(24);
+    OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
 
     List<MemoryBlock> ob = this.freeListManager.getOrderedBlocks();
     assertThat(ob).hasSize(3);
@@ -692,8 +692,8 @@ public class FreeListManagerTest {
   public void allocatedBlocksEmptyAfterFree() {
     Slab chunk = new SlabImpl(96);
     this.freeListManager = createFreeListManager(ma, new Slab[] {chunk});
-    ObjectStoredInMemory c = this.freeListManager.allocate(24);
-    ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
+    OffHeapStoredObject c = this.freeListManager.allocate(24);
+    OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
     List<MemoryBlock> ob = this.freeListManager.getAllocatedBlocks();
     assertThat(ob).hasSize(0);
   }
@@ -702,7 +702,7 @@ public class FreeListManagerTest {
   public void allocatedBlocksHasAllocatedChunk() {
     Slab chunk = new SlabImpl(96);
     this.freeListManager = createFreeListManager(ma, new Slab[] {chunk});
-    ObjectStoredInMemory c = this.freeListManager.allocate(24);
+    OffHeapStoredObject c = this.freeListManager.allocate(24);
     List<MemoryBlock> ob = this.freeListManager.getAllocatedBlocks();
     assertThat(ob).hasSize(1);
     assertThat(ob.get(0).getAddress()).isEqualTo(c.getAddress());
@@ -712,8 +712,8 @@ public class FreeListManagerTest {
   public void allocatedBlocksHasBothAllocatedChunks() {
     Slab chunk = new SlabImpl(96);
     this.freeListManager = createFreeListManager(ma, new Slab[] {chunk});
-    ObjectStoredInMemory c = this.freeListManager.allocate(24);
-    ObjectStoredInMemory c2 = this.freeListManager.allocate(33);
+    OffHeapStoredObject c = this.freeListManager.allocate(24);
+    OffHeapStoredObject c2 = this.freeListManager.allocate(33);
     List<MemoryBlock> ob = this.freeListManager.getAllocatedBlocks();
     assertThat(ob).hasSize(2);
   }
@@ -731,10 +731,10 @@ public class FreeListManagerTest {
     Slab chunk = new SlabImpl(32);
     Slab chunk2 = new SlabImpl(1024*1024*5);
     this.freeListManager = createFreeListManager(ma, new Slab[] {chunk, chunk2});
-    ObjectStoredInMemory c = this.freeListManager.allocate(24);
-    ObjectStoredInMemory c2 = this.freeListManager.allocate(1024*1024);
-    ObjectStoredInMemory.release(c.getAddress(), this.freeListManager);
-    ObjectStoredInMemory.release(c2.getAddress(), this.freeListManager);
+    OffHeapStoredObject c = this.freeListManager.allocate(24);
+    OffHeapStoredObject c2 = this.freeListManager.allocate(1024*1024);
+    OffHeapStoredObject.release(c.getAddress(), this.freeListManager);
+    OffHeapStoredObject.release(c2.getAddress(), this.freeListManager);
     
     LogWriter lw = mock(LogWriter.class);
     this.freeListManager.logOffHeapState(lw, 1024);
@@ -785,7 +785,7 @@ public class FreeListManagerTest {
     
     when(spy.getUsedMemory()).thenReturn(1L);
     when(spy.getFragmentCount()).thenReturn(2);
-    when(spy.getFreeMemory()).thenReturn((long)ObjectStoredInMemory.MIN_CHUNK_SIZE * 2);
+    when(spy.getFreeMemory()).thenReturn((long)OffHeapStoredObject.MIN_CHUNK_SIZE * 2);
     
     assertThat(spy.getFragmentation()).isEqualTo(100);
   }
@@ -799,25 +799,25 @@ public class FreeListManagerTest {
     
     when(spy.getUsedMemory()).thenReturn(1L);
     when(spy.getFragmentCount()).thenReturn(4);
-    when(spy.getFreeMemory()).thenReturn((long)ObjectStoredInMemory.MIN_CHUNK_SIZE * 8);
+    when(spy.getFreeMemory()).thenReturn((long)OffHeapStoredObject.MIN_CHUNK_SIZE * 8);
     
     assertThat(spy.getFragmentation()).isEqualTo(50); //Math.rint(50.0)
     
     when(spy.getUsedMemory()).thenReturn(1L);
     when(spy.getFragmentCount()).thenReturn(3);
-    when(spy.getFreeMemory()).thenReturn((long)ObjectStoredInMemory.MIN_CHUNK_SIZE * 8);
+    when(spy.getFreeMemory()).thenReturn((long)OffHeapStoredObject.MIN_CHUNK_SIZE * 8);
     
     assertThat(spy.getFragmentation()).isEqualTo(38); //Math.rint(37.5)
     
     when(spy.getUsedMemory()).thenReturn(1L);
     when(spy.getFragmentCount()).thenReturn(6);
-    when(spy.getFreeMemory()).thenReturn((long)ObjectStoredInMemory.MIN_CHUNK_SIZE * 17);
+    when(spy.getFreeMemory()).thenReturn((long)OffHeapStoredObject.MIN_CHUNK_SIZE * 17);
     
     assertThat(spy.getFragmentation()).isEqualTo(35); //Math.rint(35.29)
     
     when(spy.getUsedMemory()).thenReturn(1L);
     when(spy.getFragmentCount()).thenReturn(6);
-    when(spy.getFreeMemory()).thenReturn((long)ObjectStoredInMemory.MIN_CHUNK_SIZE * 9);
+    when(spy.getFreeMemory()).thenReturn((long)OffHeapStoredObject.MIN_CHUNK_SIZE * 9);
     
     assertThat(spy.getFragmentation()).isEqualTo(67); //Math.rint(66.66)
   }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListOffHeapRegionJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListOffHeapRegionJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListOffHeapRegionJUnitTest.java
index eb4096b..0272e38 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListOffHeapRegionJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/FreeListOffHeapRegionJUnitTest.java
@@ -40,7 +40,7 @@ public class FreeListOffHeapRegionJUnitTest extends OffHeapRegionBase {
 
   @Override
   public int perObjectOverhead() {
-    return ObjectStoredInMemory.HEADER_SIZE;
+    return OffHeapStoredObject.HEADER_SIZE;
   }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNodeJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNodeJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNodeJUnitTest.java
index 255ee7c..e1c3f4e 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNodeJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryBlockNodeJUnitTest.java
@@ -352,7 +352,7 @@ public class MemoryBlockNodeJUnitTest {
     storedObject = createValueAsSerializedStoredObject(obj);
     StoredObject spyStoredObject = spy(storedObject);
     MemoryBlock mb = new MemoryBlockNode(ma, (MemoryBlock) spyStoredObject);
-    when(((ObjectStoredInMemory)spyStoredObject).getRawBytes()).thenCallRealMethod().thenThrow(new CacheClosedException("Unit test forced exception"));
+    when(((OffHeapStoredObject)spyStoredObject).getRawBytes()).thenCallRealMethod().thenThrow(new CacheClosedException("Unit test forced exception"));
     ByteArrayOutputStream errContent = new ByteArrayOutputStream();
     System.setErr(new PrintStream(errContent));
     softly.assertThat(mb.getDataValue()).isEqualTo("CacheClosedException:Unit test forced exception");
@@ -365,7 +365,7 @@ public class MemoryBlockNodeJUnitTest {
     storedObject = createValueAsSerializedStoredObject(obj);
     StoredObject spyStoredObject = spy(storedObject);
     MemoryBlock mb = new MemoryBlockNode(ma, (MemoryBlock) spyStoredObject);
-    when(((ObjectStoredInMemory)spyStoredObject).getRawBytes()).thenCallRealMethod().thenThrow(ClassNotFoundException.class);
+    when(((OffHeapStoredObject)spyStoredObject).getRawBytes()).thenCallRealMethod().thenThrow(ClassNotFoundException.class);
     ByteArrayOutputStream errContent = new ByteArrayOutputStream();
     System.setErr(new PrintStream(errContent));
     softly.assertThat(mb.getDataValue()).isEqualTo("ClassNotFoundException:null");