Does array resizing invoke the GC? Does array resizing invoke the GC? arrays arrays

Does array resizing invoke the GC?


Array.Resize doesn't actually change the original array at all - anyone who still has a reference to it will be able to use it as before. Therefore there's no optimization possible. Frankly it's a badly named method, IMO :(

From the docs:

This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.

So no, it's not going to reuse the original memory or anything like that. It's just creating a shallow copy with a different size.


Yes, using Array.Resize causes a new array to be allocated and the old one to eventually be collected (unless there are still references to it somewhere).

A more low-level array resizer could possibly do some minor optimization in some cases (for example when the array is being made smaller or there happens to be memory available right after the array), but .NET's implementation doesn't do that.