Hmmm, looks like you don't know how the GC works, I tried to explain what
basically happens when you run this sample, for more details please refer to
my other reply in this thread.
Also, did you actually run the code?, and did you watch memory consumption
using perfmon?
The ReadLine is executed after the loop terminates, the GC cannot run while
in ReadLine(), but, while executing the loop, the GC kicks in several times
(per ~40 StreamWriter instances), so what you should see is a sawtooth-like
memory allocation pattern, starting at a certain level (~5Mb) and going up
to a maximum of ~6Mb (your mileage may vary).
Anyway, none of the programs posted are leaking.
Willy.
|I think I found the source of your problem:
|
| try this:
| for(int i=0; i<1000000; i++)
| {
| using(StreamWriter newMasterfile =
| new StreamWriter(@"c:\XXX.tmp"))
| {
| newMasterfile.Close();
| }
| }
|
| Console.WriteLine("done"); //<<<----
|
| Console.ReadLine();
|
| Does the memory usage grow after you see "done" on the console?
| Didn't you just think the program was on the ReadLine line while it was
| still in the loop?
| It takes quite long to execute the whole loop - the memory usage is rising
| all the time and then it remains the same - because GC can't run because
the
| only thread is stuck on the ReadLine() and it has no reason to run,
because
| there's no memory pressure.
|
| | > Good morning and thank you Greg!
| >
| > you'll find the files there:
| >
http://savefile.com/projects/831693
| >
| > There are 2 files:
| > - MemoryLeak1: File with the original problem as described
| > - MemoryLeak2: A threading test with free main thread
| >
| > Just start the compiled release.
| > Is there anyone who can reproduce my problem?
| >
| > Martin
| >
|
|