code: public static long sizeOnHeapOf(ByteBuffer buffer)
      {
          if (buffer.isDirect())
              return BUFFER_EMPTY_SIZE;
          // if we're only referencing a sub-portion of the ByteBuffer, don't count the array overhead (assume it's slab
          // allocated, so amortized over all the allocations the overhead is negligible and better to undercount than over)
          if (buffer.capacity() > buffer.remaining())
              return buffer.remaining();
          return BUFFER_EMPTY_SIZE + sizeOfArray(buffer.capacity(), 1);
      }
docs: Memory a byte buffer consumes
  @param buffer ByteBuffer to calculate in memory size
  @return Total in-memory size of the byte buffer