public void Expand(int expandMinSize) { var limit = MaxSize; if (expandMinSize >= limit) { throw new OutOfMemoryException("HGlobal cache expand size exceeds limit."); } if (Capacity >= limit) { throw new OutOfMemoryException("HGlobal cache size exceeds limit."); } var size = Capacity * 2 + expandMinSize; if (gc.IsAllocated) { gc.Free(); } Array.Resize(ref array, size); gc = GCHandle.Alloc(array, GCHandleType.Pinned); Address = Unsafe.AsIntPtr(ref array[0]); }