How many bytes is a reference?

I have need of making a huge array. Like 1k x 1k.

Clearly the size of the elements is of concern, because for each byte in size, the array will consume 1 meg of RAM.

To minimalize this (because most of the array will be empty) I was thinking of just making an array of "object" which is essentally a pointer or reference. Id assume its either 32 bits or 64 bits.

Id also like to know the size of an empty list of 'object' if anyone can tell me. If it is only a little bigger then I might go with an array of empty lists.
[560 byte] By [DeepT] at [2007-11-20 11:04:43]
# 1 Re: How many bytes is a reference?
I have need of making a huge array. Like 1k x 1k.

Clearly the size of the elements is of concern, because for each byte in size, the array will consume 1 meg of RAM.

To minimalize this (because most of the array will be empty) I was thinking of just making an array of "object" which is essentally a pointer or reference. Id assume its either 32 bits or 64 bits.

Id also like to know the size of an empty list of 'object' if anyone can tell me. If it is only a little bigger then I might go with an array of empty lists.
Each 'object' takes 16 bytes minimum (at least on a 32bit system anyway). There's the memory needed for the pointer, the sync lock and one or two other things i can't remember. An array of 'object' is no different (memory-wise) to an array of string, or an array of any other class. The actual array will take up exactly the same memory. A pointer is a pointer regardless of the type it points to. What will change memory consumption is the type of object that the array contains,

Obviously enough if MyClass takes 20 bytes but MyOtherClass takes 200 bytes, storing 1000 instances of MyOtherClass will take up more memory. A 1000x1000 array is going to take up a fair bit of memory no matter what type you're holding on to. But if 90% of the indicies in that array are 'null', then you won't actually consume too much extra memory.
Mutant_Fruit at 2007-11-9 11:36:15 >
# 2 Re: How many bytes is a reference?
also you may use 'dynamic allocation' if you really want to save memory space.
Thread1 at 2007-11-9 11:37:16 >