Hi guys.
I am looking into memory usage and the exact point at which memory is allocated.
Lets say I have a class, called SomeClass().
I have the following code.
At which point is the memory allocated: [1] or [2]?
If I use [1] (so at this point theObject = null), would the memory usage be minimal?
I am planning to have [1] in my code, and then only if required (at a much later point in time) I will use [2].
The only problem with this is that if I delay the instantiation of theObject, I will need to write a lot of null checking if blocks, which could slow things down:
Is it better to delay the instantiation (only instantiate when required) and use lots of if blocks (see above) OR is it better to declare and instantiate at the start of the program and do away with the all the if blocks?
Which is best for memory usage and speed (speed is important)?
I am looking into memory usage and the exact point at which memory is allocated.
Lets say I have a class, called SomeClass().
I have the following code.
Code:
SomeClass theObject; //.....................[1]
theObject = new SomeClass(); //..........[2]
At which point is the memory allocated: [1] or [2]?
If I use [1] (so at this point theObject = null), would the memory usage be minimal?
I am planning to have [1] in my code, and then only if required (at a much later point in time) I will use [2].
The only problem with this is that if I delay the instantiation of theObject, I will need to write a lot of null checking if blocks, which could slow things down:
Code:
if (theObject != null)
{
//do something
}
Is it better to delay the instantiation (only instantiate when required) and use lots of if blocks (see above) OR is it better to declare and instantiate at the start of the program and do away with the all the if blocks?
Which is best for memory usage and speed (speed is important)?