Hi all when writing an array list implementation, I understand it is important to set Item(x)
to null when it is removed (instead of simply quantity -= 1
) so as to prevent memory leaks.
However, if my array list is a primitive int array list (backed by a int[]
), does it make sense to set it to 0?
Similary, for an array list of primitive chars (backed by a char[]
), when RemoveRange() is called, does it make sense to fill that range with \u0000
? Or is it totally fine simply to update the length
and pointers without modifying the backing array?
Is an array of ints filled with zeros possibly less memory-occupying than an equal length array filled with integer values because the runtime could make optimizations ?
Is an array of ints filled with zeros possibly less memory-occupying than an equal length array filled with integer values?
Assuming in both cases we're dealing with an int[]
- no. Two arrays of the same type and the same length will always occupy the same amount of memory.
There's no need to overwrite your "now empty" array elements with 0. It wouldn't do any harm (beyond a tiny performance benefit), and may even make things simpler when debugging, but you don't need to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With