Click here to Skip to main content
Sign Up to vote bad
good
See more: .NET3.5C#WinForm
Hi All,
 
I have a doubt here. My application is built on C# Winforms in multi threaded application. Recently after a long run of my application, I am facing " Out of memory exception" from an active window and asserting. What might be the reason ? Here I am using PInvoke calls
. Will it causes issues ? Is GC will not work properly on PIO calls ? Please suggest.
Posted 22 Sep '11 - 19:40
SKOTAJI1.2K


3 solutions

GC works properly, but this question has nothing to do with the problem.
Do you think that with GC you don't have memory leaks? If so, this is completely wrong. Of course it eliminates memory leaks you would make due to forgetting to release some memory, but it cannot help if you have wrong design. It's pretty easy to design code which leaks managed memory.
 
As to forgetting to release some resources, this is still a source of other kind of memory leaks, of unmanaged resources. In particular, you need guarantee that you properly call Dispose for all types implementing System.IDisposable, because many of such objects allocate unmanaged resource and free them only if you call Dispose. This is the possibility Mehdi mentioned in his solution. Use using statement (not to be mixed up with using clause).
 
Of course you can have memory leaks by design. One typical case is putting objects in some application-global collection (for the sake of fast search, for example) and forgetting to remove references when object is not used anymore. By the way, think about using System.WeakReference, see http://msdn.microsoft.com/en-us/library/system.weakreference.aspx[^].
 
So, first analyze your design for management of life cycle of all objects. Remember that an object is scheduled for destruction by GC only if all references to it in whole Application Domain becomes totally inaccessible form working code, but it does not apply to weak references. This mechanism if very cunning: if you have isolated path of circular reference, GC is clever enough to schedule all involving objects for destruction. Consider A referencing B, B referencing C and C referencing A. If all other references to each of these three objects are lost, they will be scheduled for destruction by GC anyway. [EDIT] The criteria for destruction is reachability, see http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Reachability_of_an_object[^].[END EDIT] Pretty neat, isn't it?
 
After that you may need to use the good advice by Mehdi and use a memory profiler. A good memory profiler is quite a powerful weapon.
 
—SA
  Permalink  
Comments
Mehdi Gholam - 23 Sep '11 - 2:46
great advise a always, 5!
SAKryukov - 23 Sep '11 - 2:49
Thank you very much, Mehdi. "Always" is of course a great exaggeration... --SA
SKOTAJI - 14 Oct '11 - 4:11
Thanks for your suggestions !
SAKryukov - 14 Oct '11 - 12:30
My pleasure. (I added one more link, see [EDIT] in the updated solution). Good luck, call again. --SA
Simon Bang Terkildsen - 14 Oct '11 - 12:31
Good walkthrough, +5
SAKryukov - 14 Oct '11 - 13:39
Thank you, Simon. --SA
Your best bet is to use a memory profiler (RedGate has a good one) to see where your memory leaks are, it is hard to say what it could be from your description.
 
Pinvokes could cause this if not disposed properly.
  Permalink  
Comments
SAKryukov - 23 Sep '11 - 2:41
Yes those are good points, but it looks like you only suspect unmanaged memory to leak. This is not all. It's easy to leak even managed memory. I voted 4 and explain this problem in my solution, with some other advice, please see. --SA
SAKryukov - 23 Sep '11 - 9:44
By the way, I added some important information to my solution and credited your solution which -- sorry about that! -- I forgot to do in first place. :-) --SA
Mehdi Gholam - 23 Sep '11 - 9:55
Cheers man
SKOTAJI - 14 Oct '11 - 4:11
Thanks for your suggestions !
The best way to code while working on multi-threaded apps is to enclose your code blocks into 'using'. This is not ofcourse the best or fool-proof method, but will avoid most of the memory related problems.
 
Tell us if the suggestion works in your case
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 365
1 OriginalGriff 315
2 Slacker007 240
3 Dave Kreskowiak 212
4 Aarti Meswania 210
0 Sergey Alexandrovich Kryukov 8,893
1 OriginalGriff 7,134
2 CPallini 3,678
3 Rohan Leuva 3,036
4 Maciej Los 2,428


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid