"using" vs "= null" and object lifetime
From: MuZZy (leyandrew_at_yahoo.com)
Date: 02/17/05
- Next message: Mattias Sjögren: "Re: format string modifier for actual number of decimals"
- Previous message: aaa: "Re: Create XMLDocument on the fly."
- Next in thread: Tom Porterfield: "Re: "using" vs "= null" and object lifetime"
- Reply: Tom Porterfield: "Re: "using" vs "= null" and object lifetime"
- Reply: Nicholas Paldino [.NET/C# MVP]: "Re: "using" vs "= null" and object lifetime"
- Reply: Sherif ElMetainy: "Re: "using" vs "= null" and object lifetime"
- Reply: MuZZy: "Re: "using" vs "= null" and object lifetime"
- Reply: Jon Skeet [C# MVP]: "Re: "using" vs "= null" and object lifetime"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 17 Feb 2005 10:02:52 -0500
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.CallSomeOtherFunc();
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.CallSomeOtherFunc();
}
}
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
- Next message: Mattias Sjögren: "Re: format string modifier for actual number of decimals"
- Previous message: aaa: "Re: Create XMLDocument on the fly."
- Next in thread: Tom Porterfield: "Re: "using" vs "= null" and object lifetime"
- Reply: Tom Porterfield: "Re: "using" vs "= null" and object lifetime"
- Reply: Nicholas Paldino [.NET/C# MVP]: "Re: "using" vs "= null" and object lifetime"
- Reply: Sherif ElMetainy: "Re: "using" vs "= null" and object lifetime"
- Reply: MuZZy: "Re: "using" vs "= null" and object lifetime"
- Reply: Jon Skeet [C# MVP]: "Re: "using" vs "= null" and object lifetime"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|