Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I am working on C# windows application, where in a button click I get large data from database using data table and I assign it to a grid.While doing this system utilizes lot of memory. When I close this form, system is not releasing memory immediately. In the mean time my end user opens the same form again and again
resulting in "Out Of Memory Exception".

Please suggest a solution for this issue.

What I have tried:

In form closing I tried this.Dispose(), After assigning data table to grid as data source i reinitialized data table as null
Posted
Updated 25-Feb-16 3:30am
Comments
dan!sh 25-Feb-16 6:53am    
Which database and which provider are you using? You will need to dispose all the disposable objects.
Ralf Meier 25-Feb-16 6:57am    
For me it's impossible to give you a generell advise. There is more Information necessary.

In fact - all objects which are instanced by you (with your code) have to disposed by you when closing the form. All objects which are instanced and created by the designer would be disposed automaticly.
Sergey Alexandrovich Kryukov 25-Feb-16 9:25am    
Do you understand the difference between managed and unmanaged memory? Dispose is related only to unmanaged, and other resources which have nothing to do with reclaiming memory. The rule is simple: you have to dispose everything which can be disposed.
—SA

If you are holding objects in your class, such as DataTables and so forth which contain a lot of memory or other resources (such as graphics handles and similar) then Dispose each of them individually when you are finished with it. Otherwise, the GC won't automatically dispose of memory hogging objects until you run out of memory.

But an "Out of memory" error for nulled objects in .NET is rare, even if you do use a lot of memory, the GC should free it up - it's a lot more likely that you are hogging resources instead. For example, check your form for painting: if you are creating fonts, graphics context, brushes, SQL connections, etc. and not disposing them then they will give the same error long before the GC needs to be activated by a "real" memory condition.
 
Share this answer
 
Right, it's impossible to give you a general advice, but you need to start with learning how memory is used and reclaimed, and what disposing is used for. Please see my past answers:
Memory leak in WPF DataBinding[^],
Memory management in MDI forms[^],
Best way to get rid of a public static List Causing an Out of Memory[^],
deferring varirable inside the loop can cuase memory leak?[^],
Garbage collectotion takes care of all the memory management[^].

Just one thought explained in detail in those answers: some think that memory leaks are impossible in memory managed systems, but this is not exactly so.

—SA
 
Share this answer
 

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