Pacerier
Pacerier

Reputation: 89723

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 ?

Upvotes: 6

Views: 182

Answers (6)

Peter Lawrey
Peter Lawrey

Reputation: 533660

Primitives and references always occupy the same amount of space.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502086

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.

Upvotes: 7

wmorrison365
wmorrison365

Reputation: 6318

No, you need to nullify the object slot in the array to prevent the leak. If the object is still referenced by your array then it can't be GC'd - so the leak you refer to.

Primitives on the other hand are allocated on the stack anyway, not the heap, so aren't GC'd anyway. Primitives that are instance vars of classes are stored as fields of the relevant object and cleaned up when the object is GC'd.

Additionally, the JLS indicates that the size of a primitive is VM-specific but most (all?) VMs currently support 4byte ints. See the JLS for more info:

Upvotes: 1

Jesper
Jesper

Reputation: 206896

... 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.

This isn't true. Setting variables to null is not something that is always necessary and not doing so does not mean that you have a memory leak.

However, if my array list is a primitive int array list, does it make sense to set it to 0?

No, for primitives it doesn't matter at all, 0 or \u0000 (for a char) is just a value like any other value. It doesn't take up less space.

Upvotes: 2

Ingo
Ingo

Reputation: 36349

You can't have an ArrayList<int> nor any other container class with primitives. Regarding plain arrays, see Jon Skeets answer.

Upvotes: 1

Sanjay T. Sharma
Sanjay T. Sharma

Reputation: 23218

No, it's not necessary to do so with primitive types (i.e. set them to 0) since the only reason "slots" are explicitly nulled out are to prevent fake references to them thereby screwing around with garbage collection.

Upvotes: 1

Related Questions