G
Gomaw Beoyr
Hello
Is there any explanation why Microsoft chose to implement
arrays as objects allocated on the heap instead of structs
allocated on the stack?
For "mathematical stuff", one normally wishes to avoid
unnecessary garbage collection, but that is exactly what's
going to happen if a lot of arrays, matrixes etc are allocated
on the heap.
Consider e.g. this line at the beginning of a function:
double[,] a = new double[n, n];
If this function is called 10000 times with n = 100, that
yields around 800 MB to garbage-collect.
Ok, I realize that the case when arrays on the heap are obviously
better than arrays on the stack, is when the array is "reallocated"
to a bigger array, e.g.:
double[] a = new double[1000];
...
a = new double[2000];
This would obviously "break" the original array. So is this the
most significant reason for the decision to put all arrays on the
heap? (There are some alternative solutions to this particular
problem, but they are all somewhat "kludgy".)
/Gomaw
Is there any explanation why Microsoft chose to implement
arrays as objects allocated on the heap instead of structs
allocated on the stack?
For "mathematical stuff", one normally wishes to avoid
unnecessary garbage collection, but that is exactly what's
going to happen if a lot of arrays, matrixes etc are allocated
on the heap.
Consider e.g. this line at the beginning of a function:
double[,] a = new double[n, n];
If this function is called 10000 times with n = 100, that
yields around 800 MB to garbage-collect.
Ok, I realize that the case when arrays on the heap are obviously
better than arrays on the stack, is when the array is "reallocated"
to a bigger array, e.g.:
double[] a = new double[1000];
...
a = new double[2000];
This would obviously "break" the original array. So is this the
most significant reason for the decision to put all arrays on the
heap? (There are some alternative solutions to this particular
problem, but they are all somewhat "kludgy".)
/Gomaw