Answered by:
ShowDialog Dispose Memory Leak

Question
-
Hello,
I am having some trouble with the ShowDialog option in my application and I know this is an issue many have encountered. However, I haven't been able to find a concrete answer. So any help would be greatly appreciated!
I have a MainForm and I am calling another Form (SelectOrderForm) using ShowDialog, which works fine. However, when I close the form, I don't see it release the memory. Does anyone know why that is?
Here is my code:Dim MySelectOrderForm As New SelectOrderForm MySelectOrderForm.ShowDialog() MySelectOrderForm.Dispose()
Monday, July 26, 2010 4:08 PM
Answers
-
After Form opened by ShowDialog get closed, you can still access the public variable and properties. So it still remains in the memory. Garbage collector will automatically release it once there is no reference to that object. But if you want to release it you can try following code
Dim MySelectOrderForm As New SelectOrderForm MySelectOrderForm.ShowDialog() MySelectOrderForm.Dispose() MySelectOrderForm = Nothing
And if you feel there is some memory leak then you can read following article to detect it
http://www.codeproject.com/KB/dotnet/Memory_Leak_Detection.aspx
Gaurav Khanna- Marked as answer by Helen Zhou Monday, August 2, 2010 2:32 AM
Monday, July 26, 2010 5:48 PM -
Hello DaNuGai,
On msdn:
Unlike modeless forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is not closed, you must call the Dispose method of the form when the form is no longer needed by your application.Memory Leak Detection in .NET shows how to distinguish managed memory leak and unmanaged memory leak.
If it is a managed memory leak, CLR Profiler is a good tool that helps you to find out the root cause, see Detecting High Memory consuming functions in .NET code for how to use CLR Profiler.
You can also refer to this post:
http://channel9.msdn.com/forums/TechOff/136011-Can-FormShowDialog-cause-memory-leaks-when-not-Disposed/
http://www.pcreview.co.uk/forums/thread-1307277.phpHope this helps.
Regards,
Helen ZhouThis response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.- Marked as answer by Helen Zhou Monday, August 2, 2010 2:43 AM
Monday, August 2, 2010 2:32 AM
All replies
-
After Form opened by ShowDialog get closed, you can still access the public variable and properties. So it still remains in the memory. Garbage collector will automatically release it once there is no reference to that object. But if you want to release it you can try following code
Dim MySelectOrderForm As New SelectOrderForm MySelectOrderForm.ShowDialog() MySelectOrderForm.Dispose() MySelectOrderForm = Nothing
And if you feel there is some memory leak then you can read following article to detect it
http://www.codeproject.com/KB/dotnet/Memory_Leak_Detection.aspx
Gaurav Khanna- Marked as answer by Helen Zhou Monday, August 2, 2010 2:32 AM
Monday, July 26, 2010 5:48 PM -
The garbage collector doesn't necessarily release the memory immediately. It will queue up cleanup tasks until it reaches a certain point and then do them all at once. You can confirm this by continuously opening and closing that dialog. You will see your memory usage rise, and rise, and rise, and then fall sharply as all those previous instances are cleaned up. So the behavior you are seeing is normal. And as long as you are calling Form.Dispose, you're doing everything you should.
Monday, July 26, 2010 6:08 PM -
Thanks guys. I have been testing my application constantly by opening and closing the form and after I did it about 25 times, I gave up. I didn't see any memory open-up, it just kept on expanding. Now, I'd hate to assume that the GC would kick-in at some point and seeing that it didn't for me, I don't know how else to test it.
I did add MySelectOrderForm = Nothing to my code.
Monday, July 26, 2010 8:21 PM -
The garbage collector does not necessarily free memory unless your application needs it. Often times it is more optimal to simply let the available to be freed memory to just "hang loose" out there, and the GC will collect it when necessary.
--
MikeMonday, July 26, 2010 8:27 PM -
Hello DaNuGai,
On msdn:
Unlike modeless forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is not closed, you must call the Dispose method of the form when the form is no longer needed by your application.Memory Leak Detection in .NET shows how to distinguish managed memory leak and unmanaged memory leak.
If it is a managed memory leak, CLR Profiler is a good tool that helps you to find out the root cause, see Detecting High Memory consuming functions in .NET code for how to use CLR Profiler.
You can also refer to this post:
http://channel9.msdn.com/forums/TechOff/136011-Can-FormShowDialog-cause-memory-leaks-when-not-Disposed/
http://www.pcreview.co.uk/forums/thread-1307277.phpHope this helps.
Regards,
Helen ZhouThis response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.- Marked as answer by Helen Zhou Monday, August 2, 2010 2:43 AM
Monday, August 2, 2010 2:32 AM -
Without GC.Collect, MySelectOrderForm objects are still loaded in memory.This is working form me :
Dim MySelectOrderForm As New SelectOrderForm MySelectOrderForm.ShowDialog() MySelectOrderForm.Dispose()
GC.Collect()
MySelectOrderForm = NothingMonday, January 22, 2018 11:34 AM