I’ve done my reading on this article and this relates to my client question, “Why is your FLV Converter Consume 50% of System Resources Although there’s no Video/Task to convert?”..
Lets dig into this.
“Memory Leak is a consumption of memory by a program where the program is unable to release the memory it has acquired”
Is it possible to have a memory leak on .Net? Yes, although we have the GC.. the garbage management
How? Remember the Our garbage man rule? it can only collect an object that is Not Referenced Any More (Unreachable).
So if we still have an object Link To (reference) some Event/Object Than That is the source of evil.
based on the article, what is the common mistake?:
- Static Field
- Static Event
- Improperly Disposed object.
- Event not unregistered
- etc
The point is after you add Event myclass.SomeEvent+=Someevent_Handler
You need to unregistered it properly on disposed event. myclass.SomeEvent-=Someevent_Handler
Static Events
For instance , if we subscribe to System.Events.some of the events are static .
“When subscribing to a static event, it is important to remember to detach from the event before your application closes or the object subscribing to the event is disposed. Failing to do so can result in memory leaks for your application since the event subscription will not be broken automatically when the object closes.”
after you closed it It stays and live… Think about that. because GC can’t collect whatever still alive. Static type Lives Forever
The problem becomes bigger, because 1 object reference another, and that another contains lots of object, if the root stays and so is the descendent.
This Leak can happen on most application type,winform, wpf, prism pattern, mvp pattern.it’s not about the type but more to how .net works :) so understanding will be the Key here to write better code and Not bloating the memory. :)
So i try to use the Jetbrain dottrace, although it’s explained in graphical and legend , i seems to quite not happy about it.so i use .Net memory Profiler.. It’s better , i like the way it Shows error similar to visual studio.
ex: it says “This object type is not properly disposed but being collected”, and i put a dispose in the end, or use Using on c#
and i can see another dumb of my coding.
If you use Console the Main Must be Static, and you can only access static type Member/method from static method. First thinking, make everything static. Yeah right, you are making living monster.
Example So instead of
1: class program
2:
3: {
4:
5: Private Static List<string> GetListVideo(){
6:
7: //do bla bla bla
8:
9: };
10:
11: Static Main(arg[] string)
12:
13: {
14:
15: mylistvideo=GetListVideo();
16:
17: //do something with mylistvideo
18:
19: }
20:
21: }
22:
Put on another class or make it initiate able
1: class program
2:
3: {
4:
5: Private List<string> GetListVideo(){
6:
7: //do bla bla bla
8:
9: };
10:
11: Static Main(arg[] string)
12:
13: {
14:
15: mylistvideo=new program().GetListVideo();
16:
17: //do something with mylistvideo
18:
19: }
20:
21: }
we can also use weakreferences.
So i need to Detail my application More, Do more Reading and Back Update with report on this writing.
19/11/2009
Solved
After doing profiling on my application using windbg(?? ugh hard one), dottrace,.NetMemprofiler(yes, more human)
I put dispose on the place that it should,Put The close of Process (because i open an external process on task) on the finally clause and remove static to something initiate able.
but what really solve is .. something really back to basic
On my loop forever, add a sleep
1: While(true)
2: {
3: //check Task Collection
4: //Initiate SubThread
5: //Give every sub thread a Signal call back using autoreset event
6: //Start the Task
7:
8: Thread.Sleep(300);
9: }
This cool down the CPU time.. When there is no task .
Btw FYI FFmpeg will use 50% of your cpu during convertion and 50% of other on dual core processor. you can force ffmpeg to use full use 100% available resouce with a param ‘-thread2’