473,383 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Memory leak while Hiding a form

Hello all,

I have a main form(say "form1") .i want to display another form(say
"form2") on occuring of an event (say a button click) and want to hide
it after some time so that it will again displays while occuring of the
same event.I develop it by creating an object of the form2 and
displays it in the event by calling form2.Show() and hide it by calling
form2.Hide().

the problem is that while displaying the form2 the memory usage of my
application increases by a huge amount.it wont decrease during the
hiding of the form2. Then the application eats memory every
times,during the occurnce of the event.

when i tried "form2.WindowState = FormWindowState.Minimized" instead of
hiding it, the memory usage decreases .But my aim is to hide the form
so that it will displays every time the event occurs
please suggest any method for solving this problem.

regards,
Alex

Dec 26 '06 #1
11 3127
Hi Alex,

Can you Close form2 and then Show it again each time it's required?

Closing the Form will Dispose of its unmanaged resources, hopefully freeing
up some memory. If it doesn't then you may have a bigger problem.

Make sure that any unmanaged components that you are using are freed in the
Dispose method of the Form as well.

--
Dave Sexton
http://davesexton.com/blog

"Alex" <al******@gmail.comwrote in message
news:11**********************@42g2000cwt.googlegro ups.com...
Hello all,

I have a main form(say "form1") .i want to display another form(say
"form2") on occuring of an event (say a button click) and want to hide
it after some time so that it will again displays while occuring of the
same event.I develop it by creating an object of the form2 and
displays it in the event by calling form2.Show() and hide it by calling
form2.Hide().

the problem is that while displaying the form2 the memory usage of my
application increases by a huge amount.it wont decrease during the
hiding of the form2. Then the application eats memory every
times,during the occurnce of the event.

when i tried "form2.WindowState = FormWindowState.Minimized" instead of
hiding it, the memory usage decreases .But my aim is to hide the form
so that it will displays every time the event occurs
please suggest any method for solving this problem.

regards,
Alex

Dec 26 '06 #2
Thanks for your immediate reply,

Actually if i close the form instead of Hiding it, when the event
occurs on the next time i want to create the object( ie Form2 form2 =
new Form2 ) again, so i prefered the form2 to Hide instead of closing
it.

I also tried a simple application that displays a simple form on every
button click but It also shows the same problem.
Any idea for solving this?

regards,
Alex

Dave Sexton wrote:
Hi Alex,

Can you Close form2 and then Show it again each time it's required?

Closing the Form will Dispose of its unmanaged resources, hopefully freeing
up some memory. If it doesn't then you may have a bigger problem.

Make sure that any unmanaged components that you are using are freed in the
Dispose method of the Form as well.

--
Dave Sexton
http://davesexton.com/blog

"Alex" <al******@gmail.comwrote in message
news:11**********************@42g2000cwt.googlegro ups.com...
Hello all,

I have a main form(say "form1") .i want to display another form(say
"form2") on occuring of an event (say a button click) and want to hide
it after some time so that it will again displays while occuring of the
same event.I develop it by creating an object of the form2 and
displays it in the event by calling form2.Show() and hide it by calling
form2.Hide().

the problem is that while displaying the form2 the memory usage of my
application increases by a huge amount.it wont decrease during the
hiding of the form2. Then the application eats memory every
times,during the occurnce of the event.

when i tried "form2.WindowState = FormWindowState.Minimized" instead of
hiding it, the memory usage decreases .But my aim is to hide the form
so that it will displays every time the event occurs
please suggest any method for solving this problem.

regards,
Alex
Dec 26 '06 #3
Hi,
"Alex" <al******@gmail.comwrote in message
news:11**********************@42g2000cwt.googlegro ups.com...
Hello all,

I have a main form(say "form1") .i want to display another form(say
"form2") on occuring of an event (say a button click) and want to hide
it after some time so that it will again displays while occuring of the
same event.I develop it by creating an object of the form2 and
displays it in the event by calling form2.Show() and hide it by calling
form2.Hide().

the problem is that while displaying the form2 the memory usage of my
application increases by a huge amount.
How big we are talking about?
when i tried "form2.WindowState = FormWindowState.Minimized" instead of
hiding it, the memory usage decreases
Can you try minimaze & later hide it?
--
Ignacio Machin
machin AT laceupsolutions com
Dec 26 '06 #4
Thanks,
I have already tried it but by doing it doesnt look good.ie the form
shows the minimizing towards taskbar (ie not disappearing atonce) and
also when the event occurs more times exception occurs.
any solution to solve it
regards,
Alex
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,
"Alex" <al******@gmail.comwrote in message
news:11**********************@42g2000cwt.googlegro ups.com...
Hello all,

I have a main form(say "form1") .i want to display another form(say
"form2") on occuring of an event (say a button click) and want to hide
it after some time so that it will again displays while occuring of the
same event.I develop it by creating an object of the form2 and
displays it in the event by calling form2.Show() and hide it by calling
form2.Hide().

the problem is that while displaying the form2 the memory usage of my
application increases by a huge amount.

How big we are talking about?
when i tried "form2.WindowState = FormWindowState.Minimized" instead of
hiding it, the memory usage decreases

Can you try minimaze & later hide it?
--
Ignacio Machin
machin AT laceupsolutions com
Dec 26 '06 #5
Hi Alex,
Actually if i close the form instead of Hiding it, when the event
occurs on the next time i want to create the object( ie Form2 form2 =
new Form2 ) again, so i prefered the form2 to Hide instead of closing
it.

I also tried a simple application that displays a simple form on every
button click but It also shows the same problem.
Any idea for solving this?
What do you mean by "simple application"? What is the actual problem that
you are having?

Could you post a short but complete application that can help us to repro
the issue?

(See Jon Skeet's article for guidance on short but complete programs:
http://www.yoda.arachsys.com/csharp/complete.html)

A memory leak and normal memory consumption are two very different things.
Even if you hide a Form it will have to keep state in memory so that it can
be shown again without having to be recreated. If you don't want to
recreate the Form each time it's required then you'll have to live with at
least some memory consumption while the Form is hidden. One option would be
to just Dispose and recreate some components instead of recreating the
entire Form, but I suspect that might be, ultimately, not even worth the
effort.

--
Dave Sexton
http://davesexton.com/blog

Dec 26 '06 #6
Hello,
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.
i dont understand "recreate some components instead of recreating the
entire Form" please mention it also
regards,
Alex
Dave Sexton wrote:
Hi Alex,
Actually if i close the form instead of Hiding it, when the event
occurs on the next time i want to create the object( ie Form2 form2 =
new Form2 ) again, so i prefered the form2 to Hide instead of closing
it.

I also tried a simple application that displays a simple form on every
button click but It also shows the same problem.
Any idea for solving this?

What do you mean by "simple application"? What is the actual problem that
you are having?

Could you post a short but complete application that can help us to repro
the issue?

(See Jon Skeet's article for guidance on short but complete programs:
http://www.yoda.arachsys.com/csharp/complete.html)

A memory leak and normal memory consumption are two very different things.
Even if you hide a Form it will have to keep state in memory so that it can
be shown again without having to be recreated. If you don't want to
recreate the Form each time it's required then you'll have to live with at
least some memory consumption while the Form is hidden. One option would be
to just Dispose and recreate some components instead of recreating the
entire Form, but I suspect that might be, ultimately, not even worth the
effort.

--
Dave Sexton
http://davesexton.com/blog
Dec 26 '06 #7
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
Dec 26 '06 #8
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
Dec 27 '06 #9
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

Dec 27 '06 #10
Hi Alex,

Since it's really simple, you should post a short but complete program so we
can repro the problem you are having. Take a look at the article by Jon
Skeet that I posted in a previous response to this thread.

It's not going to be extremely helpful to you to receive guesses as to what
your problem is, which is what respondents must do without seeing a sample
of the problem.

--
Dave Sexton
http://davesexton.com/blog

"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

Dec 27 '06 #11
Alex,
You stated that you are creating a new Form2 each time and then hiding
it.
>>I develop it by creating an object of the form2 ( ie Form2 form2 =
new Form2 ) and
displays it in the event by calling form2.Show() and hide it by calling
form2.Hide()
Since you are never closing the old one and you are creating a new
one, it would seem likely that you are going to increase memory usage.

If you want to hide the old one, you had better make sure that you can
"reshow" the old one.

Barry

On 26 Dec 2006 07:19:59 -0800, "Alex" <al******@gmail.comwrote:
>Thanks,
I have already tried it but by doing it doesnt look good.ie the form
shows the minimizing towards taskbar (ie not disappearing atonce) and
also when the event occurs more times exception occurs.
any solution to solve it
regards,
Alex
Ignacio Machin ( .NET/ C# MVP ) wrote:
>Hi,
"Alex" <al******@gmail.comwrote in message
news:11**********************@42g2000cwt.googlegr oups.com...
Hello all,

I have a main form(say "form1") .i want to display another form(say
"form2") on occuring of an event (say a button click) and want to hide
it after some time so that it will again displays while occuring of the
same event.I develop it by creating an object of the form2 and
displays it in the event by calling form2.Show() and hide it by calling
form2.Hide().

the problem is that while displaying the form2 the memory usage of my
application increases by a huge amount.

How big we are talking about?
when i tried "form2.WindowState = FormWindowState.Minimized" instead of
hiding it, the memory usage decreases

Can you try minimaze & later hide it?
--
Ignacio Machin
machin AT laceupsolutions com
bceggersATcomcastDOTnet
Jan 10 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon Richardson | last post by:
I recently found that one of my application was leaking memory every time I closed a MDIChild form. The garbage collector never reclaimed it and my windows handles kept increasing and increasing...
13
by: Boni | last post by:
I use 3-d party component. In this component I must pass a reference to my object. The problem is that this component has an ugly bug.When this component is disposed, it incorrectly don't delete...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
8
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that...
6
by: Andrew Poulos | last post by:
If I code something like the following it results in a memory leak in IE (as Leak 0.5 tells me): var frm = document.createElement("FORM"); document.body.appendChild(frm); fDeleteForm =...
1
by: cdmsenthil | last post by:
I have an Infragistics UltrawebGrid . Each Row in the grid is attached to a context menu using Infragistics CSOM Upon click on the menu, I am creating an Iframe dynamically which points to...
2
by: Jay | last post by:
I have a web app running on the windows CE device. In one of the asp.net pages - it has javascript code. That seems to have a memory leak. When I run the web app - in about one hour, the app hangs....
11
by: dhtml | last post by:
(originally mis-posted on m.p.s.jscript...) I've just closed all windows in Firefox and its using 244MB of memory. I have no idea why. I had GMail open, a page from unicode, the CLJ FAQ. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.