I looked into the implementation of Array.Resize() and noticed that a new array is created and returned. I’m aiming for zero memory allocation during gameplay and so I need to avoid creating any new reference types. Does resizing an array trigger the Garbage Collector on the previous array? I’m creating my own 2D array resizer, but it essentially functions in the same way as the .NET Resize() method.
If the new array is smaller than the previous one, but excess objects have already been placed back into a generic object pool, will this invoke the GC?
Arrays will constantly be created in my game loop, so I need to try and make it as efficient as possible. I’m trying to create an array pool as such, so that there’s no need to keep creating them ingame. However, if the resize method does the same thing, then it makes little sense to not just instantiate a new array instead of having the pool.
Thanks for the help
Array.Resizedoesn’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:
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.