Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am having two forms mentioned below.

1.frmNewMain
2.frmDetailReferrals

Here
Form #1 is the static form which is always opened.
Form #2 is a modal form which is opened via ShowDialog Method.

Form #1 creates instance for Form #2

C#
 frmDetailReferrals frmDetail = 
                    new frmDetailReferrals(this, LastOpenedCaseID, "Medication");

 frmDetail.ShowDialog();

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();


Initially 23 MB for Form #1
After Second Form 50 MB

How to remove all memory resources other than Form #1

Thanks in advance
Posted
Comments
Joan Magnet 6-Aug-15 5:48am    
frmDetail.Dispose()

https://msdn.microsoft.com/en-us/library/aw58wzka(v=vs.110).aspx

1 solution

You can't. You have no control over the memory allocation: once it's allocated to the framework for your application, you can't force anything to release it.
ANd your code doesn't even Dispose the second form: there is a still a reference to it, so the Garbage Collector will not do anything with it.

You should not be trying to force the GC anyway: why do you want to do this? It's not at all normal...
 
Share this answer
 
Comments
KUMAR619 7-Aug-15 2:42am    
Then how to solve this kind of problem.
OriginalGriff 7-Aug-15 3:06am    
What problem?
When the GC runs out of memory, it will release the resources that are currently unused.

You can manually Dispose the second firm, and you can write a Dispose override which releases any resources you have added within the form - but under normal circumstances that isn't necessary. What are you doing that you think you need this?
KUMAR619 7-Aug-15 5:28am    
Can I get a sample code for this.
OriginalGriff 7-Aug-15 5:58am    
For what? :laugh:
Have a look at Dispose and IDisposable on MSDN: they should have instructions and examples.
KUMAR619 9-Aug-15 0:28am    
I found a solution which works fine.
I've posted in solution 2.
Please review and share your comments.

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