newbie: cleanup ?

  • Thread starter Thread starter dtdev
  • Start date Start date
D

dtdev

hi again,

If i create ex. an array with the "new()" operator - am i responsible for
cleaning it up again just like in C++ ? ive seen examples where no cleanup
is done, and ive seen examples where people set the pointer's to NULL - but
?

Thanks.
 
dtdev said:
If i create ex. an array with the "new()" operator - am i responsible for
cleaning it up again just like in C++ ? ive seen examples where no cleanup
is done, and ive seen examples where people set the pointer's to NULL - but
?

You generally don't need to do anything. If an object implements
IDisposable, you should usually call its Dispose method.

If you have an instance or static variable whose value is no longer
needed, you can set the variable to null to stop the variable from
preventing the object from being garbage collected - but this is rarely
useful.

In a very few cases, setting a local variable to null can let the
garbage collector clean up an object earlier than it would otherwise -
but the JIT normally "notices" when a variable isn't going to be used
any more (in release mode).
 
In reality, No.

The CLR uses a Garbage Collector modell that tracks references to a certaian
object, when it finds out that no valid "root" references still points to
that object, it finlizes it and reclaims the memory.

Valid "root" references are variable(local, global or other variable
reachable from the application) stored on the stack (local variables, and
pointers in the FReachable queue (which is used by the GC).

If you declare a local variable pointing to an array, that variable will go
out of scope when the block it was defined in ends (eg, method, if statement
etc) and removed from the stack and no there will be no valid references to
the object.

Some people perfer to clear the variable themselves by declaring it to be
Null, but with the Optimizer running this would only serve to be clarifying,
you won't se any real impact on memory managment.
 
Back
Top