Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    while (true)
    {
        using (Form2 X = new Form2())
        {
            X.Show();
        }
    }
}
Posted

Because you are constantly instantiating a new Form2 instance in a never-ending while loop.

Did you learn how to debug? It would have allowed you to spot the problem very quickly.
 
Share this answer
 
Comments
Jerzy Kowalewski 24-Nov-15 10:41am    
Although using ?
phil.o 24-Nov-15 10:45am    
Yes. Garbage collector has not a deterministic way of acting, it runs whenever it wants to.
Technically, a .NET application will tend to use as much RAM as possible/needed; so, instantiating a heavy object like a form in a forever-loop can never be a good idea :)
Sergey Alexandrovich Kryukov 24-Nov-15 14:47pm    
You don't understand it. "Using" is unrelated to memory leaks, it's only the syntactic sugar used to guarantee the calls to IDisposable.Dispose. In contrast to that, GC is based on the concept "reachable". You hold all instances reachable by showing the forms. Isn't it obvious? And you should not dispose a form which is being used.
—SA
Gautham Prabhu K 24-Nov-15 11:15am    
What exactly are you looking for? There is no memory leak in code.

The GC collects in deterministic way, even if its disposed the memory will not go down easily as GC will not give it back to OS immediately.
Sergey Alexandrovich Kryukov 24-Nov-15 14:51pm    
All wrong! It all depends on understanding what the "memory leak" is. In it has nothing to do with "immediately". In managed systems, you cannot create accidental memory leak (not counting the use of unmanaged resources), but you can collect unused objects in memory "by design". It's enough to collect references in some reachable collection. GC works by the concept "reachable".

But, as to "immediate", it's good to take it into account. Anyway, one should not make conclusions based in Task Manager figures. :-)

—SA
My 5 cents:

Garbage collection is explained very well here: https://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

As you can see, it all is based on the concept "unreachable", which is not trivial at all: https://en.wikipedia.org/wiki/Unreachable_memory[^].

Say, take 3 objects: A references B, B references C, C references A. If you lose any other references to all of them, will they be garbage collected? They do hold each other. Yes! GC is intellectual enough to resolve all such situations.

Now, it all is unrelated to IDisposable.Dispose which can be implemented by any type, to handle some clean-up actions called explicitely (reclaiming of unmanaged resources is only one of possible applications, it can be anything; for example, setting a mouse cursor to its default glyph), in contrast to constructors which you never call in the application code. Generally, there is a very little use of constructors in managed code, because GC can cope with most situations without them, and because the moment of time of the constructor call is not controllable by the application. So, constructors can be called in "I don't care when" situations.

As to "using" statement (not to be mixed up with with "using" directive), this is nothing but syntactic sugar for calling IDisposable.Dispose, very convenient though.

Please see:
https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900