Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do arrays of "non-zero" primitives require more memory?

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 ?

like image 902
Pacerier Avatar asked Jan 19 '12 12:01

Pacerier


1 Answers

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.

like image 135
Jon Skeet Avatar answered Sep 20 '22 23:09

Jon Skeet