declaring and instantiating variables

Soldato
Joined
18 Oct 2002
Posts
15,861
Location
NW London
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.

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)?
 
Memory is allocated when the 'new' keyword is used, so point 2.

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?
It depends on how many if blocks you would need. There will be a point of diminishing returns against time saved allocating memory/checking if object instantiated.

Personally, I'd go for option C, instantiate at the point of the program where the variable is needed. If you're concerned about memory use you could always write a new garbage collector :)
 
Personally, I'd go for option C, instantiate at the point of the program where the variable is needed.

The problem with this technique is the need to add a whole bunch of if - notNull, blocks.

This will be quite labour intensive.

Previously, I declared and instantiated, right at the start, for all objects.
I recently tried to postpone the instantiation of some objects, however, this is requiring the heavy use of the check, 'if not null'.
Yesterday, I spent 3 hours debugging and the debugging is not yet over. The NullException error is causing the program to have a fit.
The program itself currently contains about 61k lines of code, so debugging is not straight forward.
 
I often use lazy loading on properties, e.g:

Code:
private SomeClass Object
{
    get { return _object ?? (_object = new SomeClass()); }  
}

That way "Object" will only be instantiated the first time it's accessed.
 
Lazy loading. I like that.
I've never used this before, so I might experiment with this.

I don't think I am being premature. The reason is that each of these objects hold many variables/data. And I am trying to avoid having junk (unused objects) taking up valuable memory. I prefer to build from the ground up with one eye on performance, as opposed to revisiting mature code and then re-writing it with optimisations (and then possibly breaking the code, which results in me having to go through a rigorous testing process again).

By the way, is there a way to check how much memory a particular object is using?
 
Back
Top Bottom