Re: How does "new" work in a loop?
- From: "John J. Hughes II" <no@xxxxxxxxxxx>
- Date: Fri, 7 Jul 2006 09:02:57 -0400
Well Jon you can site what is supposed to happen but I have to deal with
what really happens. I write services that run constantly and in some
cases don't return much idle time back to the system for days. I have
found that <var>=null on non-disposable values and using(<statement>) allows
my program to maintain an even memory allocation and stops the memory creep.
I will grant you in my code I am probably using them to excess but having my
customers tell me of memory errors after running my program for X+/- days
depending on load can be really hard to track down, this stopped after
adding the the set to null statements and using statements.
Note in forms applications I normally don't use them as much being as the
system is normally idle.
As you say "IMO" ;>
Regards,
John
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1f1793092052ae6c98d2cd@xxxxxxxxxxxxxxxxxxxxxxx
John J. Hughes II <no@xxxxxxxxxxx> wrote:
The memory is allocated during each loop and in concept released when the
loop ends. In reality the memory is marked to be released by GC
(garbage
collection) as some future point in time. I have found that if you were
to
do this loop only a few times and the program was idle the memory would
be
freed but if this loop involves a lot of files and very little idle time
is
returned to the system you will run out of memory.
That being said I would at the very least place fs=null and inputbuffer =
null at the end of the loop.
Why? It serves no purpose - and code which serves no purpose is just
distracting, IMO.
A better solution for fs would be the using statment which would force GC
and return the memory.
It wouldn't return the memory - but it *would* close/dispose the stream
in all situations, whether or not there's an exception.
Closing/disposing the stream doesn't return any memory, but it releases
the handle on the file.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- References:
- How does "new" work in a loop?
- From: Tony Sinclair
- Re: How does "new" work in a loop?
- From: John J. Hughes II
- Re: How does "new" work in a loop?
- From: Jon Skeet [C# MVP]
- How does "new" work in a loop?
- Prev by Date: Re: vb to c# question
- Next by Date: Re: Simple printing and graphics
- Previous by thread: Re: How does "new" work in a loop?
- Next by thread: Re: How does "new" work in a loop?
- Index(es):
Relevant Pages
|