473,326 Members | 2,255 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,326 software developers and data experts.

.NET Windows Form Doesn't Release Memory

12
I have a button click event on my main application window that opens a form. The form has a lot of controls, loads a lot of data, and uses a lot of memory. When I close the form the memory is no deallocated. Every time I open the form, 7 megabytes of RAM and 7 megabytes of virtual memory are eaten up.

Here are the only lines of code in the button click event:

Dim frminv As New FrmInvoice()
frminv.MdiParent = Me
frminv.Show()


As soon as the form is closed, all objects should be eligible for garbage collection, but they are never garbage collected. (I have run a loop to open the form and close it hundreds of times--the garbage collector never runs).

What can I be doing wrong? What could possibly be referencing the FrmInvoice that prevents it from being deallocated?
Oct 11 '07 #1
8 11573
Plater
7,872 Expert 4TB
You are forgetting to call
frminv.Dispose();

GC will not collect it until you explicitly release the "frminv" object.



Optionally, you could probably get away with doing a GC.Collect(), but .Dispose() is what you should be using
Oct 11 '07 #2
jimmarq
12
Thanks for the quick reply.

I tried your suggestion, but I still wasn't able to free up any memory.

Neither of these worked for me:

Dim frminv As New FrmInvoice()
frminv.MdiParent = Me
frminv.Show()
frminv.Close()
frminv.Dispose()

or

Dim frminv As New FrmInvoice()
frminv.MdiParent = Me
frminv.Show()
frminv.Dispose()

Calling GC.Collect() doesn't do anything for me, either (even when I use it with GC.WaitForPendingFinalizers()).
Oct 11 '07 #3
Plater
7,872 Expert 4TB
How do you know it's not freeing up memory?
Is memory allocated growing continuously?

Does that form have any threads of custom objects that might also need to be closed/disposed of?
Oct 11 '07 #4
jimmarq
12
Well, I create a loop that created a new form, showed it, and closed it 500 times.

My process used 1.2GB of RAM, about the same amount of my page file.

I stressed my computer until I thought I would force the garbage collector to run by using all physical memory, but then the page file just started to grow. Even letting the computer sit for a an hour to wait for garbage collection didn't work.

Are there objects in the form that need to be disposed? Good question. I thought that in .NET, once an object's parent is disposed, the object would be garbage collected. So, in effect, closing the form should have a cascading effect on all child objects.

I know I'm closing my DB connections, I'm closing all streams, but I seem to be missing something fairly big. Any ideas?
Oct 11 '07 #5
jimmarq
12
I used the SOS debugger to look at my form object after it was closed to see what is staying around. Can anybody decipher this and tell me what I am doing wrong?

!dumpheap -type FrmInvoice
Address MT Size
01513f1c 03bc749c 1008
total 1 objects
Statistics:
MT Count TotalSize Class Name
03bc749c 1 1008 FrmInvoice
Total 1 objects

!gcroot 01513f1c
Note: Roots found on stacks may be false positives. Run "!help gcroot" for
more info.
Error during command: warning! Extension is using a feature which Visual Studio does not implement.

Scan Thread 1744 OSTHread 6d0
Scan Thread 3052 OSTHread bec
Scan Thread 2524 OSTHread 9dc
Scan Thread 216 OSTHread d8
Scan Thread 5288 OSTHread 14a8
Scan Thread 3708 OSTHread e7c
DOMAIN(0014C5D8):HANDLE(WeakSh):ae1604:Root:0152f5 4c(System.Windows.Forms.Control+ControlNativeWindo w)->
0152f47c(System.Windows.Forms.Button)->
0152f5e8(System.ComponentModel.EventHandlerList)->
0152f5f8(System.ComponentModel.EventHandlerList+Li stEntry)->
0152f5c8(System.EventHandler)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae1608:Root:0152f6 dc(System.Windows.Forms.Control+ControlNativeWindo w)->
0152f60c(System.Windows.Forms.Button)->
0152f778(System.ComponentModel.EventHandlerList)->
0152f788(System.ComponentModel.EventHandlerList+Li stEntry)->
0152f758(System.EventHandler)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae1610:Root:0152f9 c4(System.Windows.Forms.Control+ControlNativeWindo w)->
0152f8f4(System.Windows.Forms.Button)->
0152fa60(System.ComponentModel.EventHandlerList)->
0152fa70(System.ComponentModel.EventHandlerList+Li stEntry)->
0152fa40(System.EventHandler)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae16c0:Root:01526c a8(System.Windows.Forms.Control+ControlNativeWindo w)->
01526bd8(System.Windows.Forms.Button)->
01526d44(System.ComponentModel.EventHandlerList)->
01526d54(System.ComponentModel.EventHandlerList+Li stEntry)->
01526d24(System.EventHandler)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae16c4:Root:01526e 38(System.Windows.Forms.Control+ControlNativeWindo w)->
01526d68(System.Windows.Forms.Button)->
01526ed4(System.ComponentModel.EventHandlerList)->
01526ee4(System.ComponentModel.EventHandlerList+Li stEntry)->
01526eb4(System.EventHandler)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae182c:Root:015285 68(System.Windows.Forms.Control+ControlNativeWindo w)->
015284d4(System.Windows.Forms.HScrollBar)->
015285cc(System.ComponentModel.EventHandlerList)->
015298b4(System.ComponentModel.EventHandlerList+Li stEntry)->
01529880(System.ComponentModel.EventHandlerList+Li stEntry)->
01529790(System.ComponentModel.EventHandlerList+Li stEntry)->
01529758(System.Windows.Forms.ScrollEventHandler)->
015282a0(System.Windows.Forms.DataGridView)->
0163178c(System.Windows.Forms.DataGridView+DataGri dViewDataConnection)->
01522390(System.Windows.Forms.BindingSource)->
01522464(System.ComponentModel.EventHandlerList)->
01523f2c(System.ComponentModel.EventHandlerList+Li stEntry)->
01523f0c(System.ComponentModel.AddingNewEventHandl er)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae188c:Root:0152b4 f8(System.Windows.Forms.Control+ControlNativeWindo w)->
0152b440(System.Windows.Forms.TextBox)->
0152b594(System.ComponentModel.EventHandlerList)->
0152b5a4(System.ComponentModel.EventHandlerList+Li stEntry)->
0152b574(System.EventHandler)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae1930:Root:0152d4 18(System.Windows.Forms.Control+ControlNativeWindo w)->
0152d360(System.Windows.Forms.TextBox)->
0152d4d4(System.ComponentModel.EventHandlerList)->
0152d4e4(System.ComponentModel.EventHandlerList+Li stEntry)->
0152d4b4(System.EventHandler)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae1958:Root:01532b 18(System.Windows.Forms.ToolStrip)->
015333f0(System.Windows.Forms.ToolStripItemCollect ion)->
01533408(System.Collections.ArrayList)->
0153c0dc(System.Object[])->
01533a5c(System.Windows.Forms.ToolStripButton)->
01533b60(System.ComponentModel.EventHandlerList)->
01533b70(System.ComponentModel.EventHandlerList+Li stEntry)->
01533b40(System.EventHandler)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae19c8:Root:015325 78(System.Windows.Forms.Control+ControlNativeWindo w)->
015324a8(System.Windows.Forms.Button)->
01532614(System.ComponentModel.EventHandlerList)->
01532624(System.ComponentModel.EventHandlerList+Li stEntry)->
015325f4(System.EventHandler)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae1c10:Root:01523c c8(System.Windows.Forms.Control+ControlNativeWindo w)->
01523bf8(System.Windows.Forms.Button)->
01523d64(System.ComponentModel.EventHandlerList)->
01523d74(System.ComponentModel.EventHandlerList+Li stEntry)->
01523d44(System.EventHandler)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae1c38:Root:01522e c0(System.Windows.Forms.Control+ControlNativeWindo w)->
01522d98(System.Windows.Forms.LinkLabel)->
01522f24(System.ComponentModel.EventHandlerList)->
01522ffc(System.ComponentModel.EventHandlerList+Li stEntry)->
01522fac(System.Windows.Forms.LinkLabelLinkClicked EventHandler)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae1c84:Root:01513f 1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakSh):ae1c88:Root:015143 34(System.Windows.Forms.Control+ControlNativeWindo w)->
01513f1c(FrmInvoice)
DOMAIN(0014C5D8):HANDLE(WeakLn):ae1df0:Root:0154ac 34(System.Windows.Forms.NativeMethods+WndProc)->
01514334(System.Windows.Forms.Control+ControlNativ eWindow)
Oct 12 '07 #6
Plater
7,872 Expert 4TB
Are there objects in the form that need to be disposed? Good question. I thought that in .NET, once an object's parent is disposed, the object would be garbage collected. So, in effect, closing the form should have a cascading effect on all child objects.
It disposes objects that are in it's .Controls collection, but not others made. Streams, file handles, socket handles, and *especially* any threads created will NOT be closed or disposed of when the form is closed.
Oct 12 '07 #7
jimmarq
12
Plater--I appreciate your help.

The problem is with my datasets that I'm using to populate combox items with. If I comment out the parts of the form's load event that fill the datasets with data, the memory problem goes away. I loaded the form and closed it a few hundred times in a loop, and the memory never went above 50 megabytes (a bif difference from the 1.2 gigabytes it used previously).

Now I just have to find a way to destroy the datasets when the form closes...
Oct 12 '07 #8
Plater
7,872 Expert 4TB
You could try this little test.
Whereas if you have something like this:
Expand|Select|Wrap|Line Numbers
  1. DataSet myDataset;
  2. myDataset = SomeFunctionThatFillsIt();
  3. myobjetc.DataSource=myDataset;
  4.  
Modify it to this
Expand|Select|Wrap|Line Numbers
  1. DataSet myDataset;
  2. myDataset = SomeFunctionThatFillsIt();
  3. myDataset.Dispose();
  4. myDataset=null;
  5. myobjetc.DataSource=myDataset;
  6.  
And see if there is a difference in memory use (it *should* match the situation you described where you never populate the dataset at all)
Oct 12 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike | last post by:
I recently posted a question regarding a table built using an action query in VBA. I am still having trouble. I have tried saving a record as suggested by one post. The problem A form uses...
2
by: JC Carette | last post by:
Hello I am experiencing extremely long time for loading a windows form in my Visual C++ .NET project This Windows form has just 40 labels, 4 checkBoxes, 2 buttons and 11 numericUpDown. It takes...
0
by: Pyramis | last post by:
Sometimes (once or twice by day) ISS raise a out of memory exception and get a dead lock state. I have seen that Garbage Collector doesn´t free memory when i don't use it, so Virtual Bytes grow and...
4
by: bob lambert | last post by:
Help I am trying to deploy to another pc a vb.net std 2002 windows form application. I am confused. I created a project - windows form I built form, compiled and debugged. I created a...
9
by: Shrage H. Smilowitz | last post by:
I have created an empty windows forms project, added one form and loaded it, i checked in task manager and the application takes 18,748 MB of ram, why does it take som much to load just one form? ...
4
by: S Anand | last post by:
Hi, We are using vb.net in our "windows application" and are using datasets heavily (100s of them). We declare them in our methods and use them and currently are not doing anything to...
16
by: eflatunn | last post by:
Hi all, my problem is that the application doesn't terminate when I close the form using the close box on the caption bar. Actually it seems as if the program is closed but task manager's process...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
2
by: mina | last post by:
hi i am using vb.net 2005 to my desktop application. my problem is like this when i am running my project it occupies 100% cpu at 1st form load. then again it release memory upto 95%. when i am...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.