To get this straight in my head:
You have a form (form1) that has a button on it.
When the button is clicked you show another form (form2)
After a period of time you hide form2
When you click the button on form1 again form2 is shown again,
but the amount of 'memory' resource the program is using increases.
This 'memory' increase continues ad infinitum each time you click the
button on form1.
The questions that need to be asked are:
How do you determine that it is time to hide form2?
How and where do you actually hide form2?
How do you actually show form2?
I suspect that you are showing from2 in form1 by using a handler on the
button click event something like this:
private void button1_Click(object sender, EventArgs e)
{
(new form2()).Show();
}
or
private void button1_Click(object sender, EventArgs e)
{
form2 _f = new form2();
_f.Show();
}
and in a timer event handler in form2 that you have something like:
private void timer1_Elapsed(object sender, EventArgs e)
{
timer1.Stop();
this.Hide();
}
If this, (or something close to it), is the case then you problem lies with
showing the form rather than hiding it.
Each time you click the button a new instance of the form is created and
shown, and, immediately, your only reference to it goes out of scope. The
instance just created still exists but you have no way of getting at that
instance. It appears that you think that the single instance of form2 will
be reused but this is not the case.
Once you have clicked the button 20 times you will have 20 instances of the
form in memory, all of which are 'orphans' as far as your code is concerned.
What you need is to modify the code in form1 to something like this:
private form2 m_form2 = null;
private void button1_Click(object sender, EventArgs e)
{
if (m_form2 == null) m_form2 = new form2();
_f.Show();
_f.timer1.Start();
}
This way, only 1 instance of form2 will ever be created and that instance
will be reused each time you click the button.
"Alex" <al******@gmail.comwrote in message
news:11**********************@i12g2000cwa.googlegr oups.com...
Thanks for your reply
Actually "simple" i meant that the form with no controls or components
on it.Then also it eats memory every times it is shown after hiding it.
Please help me for finding out a solution for solving it.
regards,
Alex
Dave Sexton wrote:
>Hi Alex,
After hiding the form and again when the event occurs it again eats
memory while showing the form.
"Simple application" that i meant that displaying a simple form.
Does "simple" mean completely empty, with no other Controls or Components
on
it and with absolutely no developer-code? If not then you should post a
short but complete program so that we can reproduce the issue. If you
can't
create a short but complete program then it will be almost impossible to
diagnose.
i dont understand "recreate some components instead of recreating the
entire Form" please mention it also
If you are creating a COM object when form2 loads, for example, then
dispose
of it when you hide form2 and recreate it when you show form2 again.
That
way you would be freeing some resources when they are no longer needed to
hopefully get rid of your memory leak, but that's something you'd have to
test for just to be sure.
The problem you are having may not be as easily solved as that, depending
on
what your "simple" form is actually doing and what components it
contains.
If form2 recreates an object every time it's shown but doesn't release it
every time it's hidden then that could be your memory leak.
--
Dave Sexton
http://davesexton.com/blog