I was just wondering if there are any performance differences (specifically memory usage) when I do the following...
Instead of doing:
ArrayList array = new ArrayList();
object obj = new object();
for(int x = 0; x < 100; x++)
{
array.Add(obj);
}
I do this:
ArrayList array = new ArrayList();
for(int x = 0; x < 100; x++)
{
array.Add(new object());
}
Or is it exactly the same thing?
As I understand it, the system holds a reference to the obj variable in memory, once the method finishes, the variable gets disposed and the memory is freed
So, if I call ArrayList.Add(new Object()) on each loop, then there is no memory being allocated to a referenced variable?
The first one stores the same object reference in all 100 slots of the array.
The second version creates 100 different objects, and store them (the reference to these objects) in the array.
First one: 1 object, 100 references
Second one: 100 objects, 100 references?
It's definitely not the same thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With