Hi,
Lately i've been (and still am) fixing some memory leaks problems in the project i just took over
when i got this new job. Among the other issues i've noticed that for localy created objects it
makes difference to explicitly put them to null after working with them is done - it somehow makes
the GC collect them sooner, like here:
void SomeFunc()
{
MyClass c = new MyClass();
c.CallSomeOther Func();
c = null;
}
If i don't put "c" to null time to get it collected is way longer than if i explicitly dereference
the object. It's really strange, as it's obvoius that object's lifetime is limited by body of
"SomeFunc". But anyway, my question is: is there any difference in the first example compared to this:
void SomeFunc()
{
using (MyClass c = new MyClass())
{
c.CallSomeOther Func();
}
}
The second way looks nicer for me and avoids forgetting to explicitly dereference the object,
but i'm not sure if this second case is being treated the same way as the first one.
Any suggestions/ideas would be highly appreciated!
Thnak you,
Andrey
Lately i've been (and still am) fixing some memory leaks problems in the project i just took over
when i got this new job. Among the other issues i've noticed that for localy created objects it
makes difference to explicitly put them to null after working with them is done - it somehow makes
the GC collect them sooner, like here:
void SomeFunc()
{
MyClass c = new MyClass();
c.CallSomeOther Func();
c = null;
}
If i don't put "c" to null time to get it collected is way longer than if i explicitly dereference
the object. It's really strange, as it's obvoius that object's lifetime is limited by body of
"SomeFunc". But anyway, my question is: is there any difference in the first example compared to this:
void SomeFunc()
{
using (MyClass c = new MyClass())
{
c.CallSomeOther Func();
}
}
The second way looks nicer for me and avoids forgetting to explicitly dereference the object,
but i'm not sure if this second case is being treated the same way as the first one.
Any suggestions/ideas would be highly appreciated!
Thnak you,
Andrey
Comment