This is not because of ConstantArray, but is because of unpacking due to mixing different types. In short, packed arrays are more memory efficient and can contain only data of one type (Integer, Real or Complex) in regular lists (any dimension). That way, Mathematica need not store the type of each element.
a = ConstantArray[0., {10000, 100}];
Developer`PackedArrayQ@a
(* True *)
ByteCount[a]
(* 8000152 *)
When you add 1 (an Integer) to a list of Reals, you cause the list to be unpacked, which increases memory usage (since the type of each element is stored).
On["Packing"];
a[[1, 1]] = 1;
During evaluation of In[...]:= Developer`FromPackedArray::punpack1: Unpacking array with dimensions {10000,100}. >>
ByteCount[a]
(* 25037600 *)
Developer`PackedArrayQ@a
(* False *)
Try assigning a Real instead:
Clear@a
a = ConstantArray[0., {10000, 100}];
Developer`PackedArrayQ@a
(* True *)
a[[1, 1]] = 1.;
Developer`PackedArrayQ@a
(* True *)
ByteCount[a]
(* 8000152 *)
1.instead of1and see what happens. – Spawn1701D Jun 09 '13 at 19:02