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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Well, "new" always create a new instance of the object. The object was
assigned to the variable during the previous cycle will be flagged so the
garbage collector can release memory. Now, since the GC does not clear
memory immediately, there is a period of time when the old object stays in
memory so there is the change that you can use too much memory before the GC
is invoked, but not a great chance. It is not something that I would worry
about unless you plan to join files where each part is a gig in size. :)




"Tony Sinclair" <no@xxxxxxxx> wrote in message
news:jvqqa2h237ekpg60jjn3ahc7i17lum8787@xxxxxxxxxx
I'm just learning C#. I'm writing a program (using Visual C# 2005 on
WinXP) to combine several files into one (HKSplit is a popular
freeware program that does this, but it requires all input and output
to be within one directory, and I want to be able to combine files
from different directories into another directory of my choice).

My program seems to work fine, but I'm wondering about this loop:


for (int i = 0; i < numFiles; i++)
{
// read next input file

FileStream fs = new FileStream(fileNames[i],
FileMode.Open, FileAccess.Read, FileShare.Read);
Byte[] inputBuffer = new Byte[fs.Length];

fs.Read(inputBuffer, 0, (int)fs.Length);
fs.Close();

//append to output stream previously opened as fsOut

fsOut.Write(inputBuffer, 0, (int) inputBuffer.Length);
progBar.Value++;
} // for int i

As you can see, the objects fs and inputBuffer are both created as
"new" each time through the loop, which could be many times. I didn't
think this would work; I just tried it to see what kind of error
message I would get, and I was surprised when it ran. Every test run
has produced perfect results.

So what is happening here? Is the memory being reused, or am I piling
up objects on the heap that will only go away when my program ends, or
am I creating a huge memory leak?

I can see that fs might go away after fs.Close(), but I don't
understand why I'm allowed to recreate the byte array over and over,
without ever disposing of it. I have verifed with the debugger that
the array has a different size each time the input file size changes,
so it really is being reallocated each time through the loop, rather
than just being reused. I've tried to find explanations of how "new"
works in a loop, but I haven't been able to so far. Any help,
including pointers to the VS docs or a popular book on C#, would be
appreciated.


.



Relevant Pages

  • Re: Innovative Systems FPE...
    ... window so one could loop using one register for copy in/out but I ... suppose you can simply unroll the loop. ... I mean't swap the order of the bytes in the hardware ... would be a graphical memory map, but even then, I usually rotate it ...
    (comp.sys.apple2)
  • 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: Read a huge text file in MATLAB
    ... I had the similar problem with an image before, where if I read it at once I get the memory error messages but when I read it in parts and at last combine it, it was perfectly working that's why I thought to read the txt file in segments. ... Do you have any idea how i can append the values in the same variable within the loop or how I can store each column from txt file into a seprate variable in MATLAB? ... Of course you can concatenate pieces until you do run out of memory; I fail to see much of any circumstance where that wouldn't, in fact, require at least as much if not more overhead memory as reading in a single array. ...
    (comp.soft-sys.matlab)
  • Re: Delete a line
    ... - read next record from input fileinto memory ... awk will not by itself modify an input file in place. ... keeps the results in an array in memory; ... This will fail if the amount of data doesn't fit in memory, ...
    (comp.lang.awk)
  • 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)