Re: How does "new" work in a loop?

Tech-Archive recommends: Speed Up your PC by fixing your registry



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


.



Relevant Pages

  • Re: Fast string operations
    ... > is why people use unsafe code now and then to use pointer arithmetic to ... > loop over arrays without all the unnecessary bounds checking. ... don't return the trimmed string". ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Performance Improvement of complex data structure (hash of hashes of hashes)
    ... Anno Siegel wrote: ... >> Here is the code that I'm using to build up this data structure. ... loop through and increment the ... I know that memory allocation in C is expensive, ...
    (comp.lang.perl.misc)
  • Re: Cost of calling a standard library function
    ... It accesses/reads memory using esi 4 ... > safly move it within the cache, without having to go via ebx. ... try it the same thing on a different earlier CPU, ... should check it out...for "tight inner loop" stuff, ...
    (alt.lang.asm)
  • Re: Restoring a NorthStar Horizon, problems with SRAM board
    ... A-D FDC is a memory mapped IO device so I would expect some behavior ... loop read/write functions - which puts the system into a two instruction ... SRAM on the Vector Graphics Flashwriter video card located at $F000 ...
    (comp.sys.northstar)
  • Re: Bimini Twist in 50lb Power Pro
    ... > fishing I do not normally choose to use any loop to loop connection ... > my bass taper fly line and continuous mono leader. ... Does Power Pro retain memory? ... I've never experienced any memory with PP, whether using it in fresh water ...
    (rec.outdoors.fishing.bass)