Memory leak
-
hi all, i have a program that make a lot of graphical drawing so it consumes a very big space of memory (i think memory leak) Is any one know how to find where is the leak comes from (i means where is the unreachable code) thanx Generator
-
hi all, i have a program that make a lot of graphical drawing so it consumes a very big space of memory (i think memory leak) Is any one know how to find where is the leak comes from (i means where is the unreachable code) thanx Generator
I always make sure to dispose of Graphics objects as they have a bad habit of causing leaks if not thrown away after use. liberal sprinklings of the using keyword don't hurt either. Russ
-
I always make sure to dispose of Graphics objects as they have a bad habit of causing leaks if not thrown away after use. liberal sprinklings of the using keyword don't hurt either. Russ
hi , i draw alot of rectangles(one at each invalidate at a moment ) i draw in OnPaint but in it i cannot make dispose (what can i do) also where i can use GC.Collect Generator
-
hi , i draw alot of rectangles(one at each invalidate at a moment ) i draw in OnPaint but in it i cannot make dispose (what can i do) also where i can use GC.Collect Generator
Hi, if you create some objects (a Pen, Brush or any other instance of a class that has a public Dispose() method) inside your OnPaint method, you MUST call Dispose for them. Additionally, if you need the same object over and over (such as new Pen(myColor)) then I suggest you create it only once and keep it in a class member (rather than continuously creating and disposing and collecting them). And finally, if the objects you need correspond to system objects (such as the ones available in SystemPens, SystemBrushes,...) I suggest you use these (without new, without Dispose !). And I do not recommend you call GC.Collect at all (the gc works fine all by itself), and certainly not from inside any OnPaint method ! (GC.Collect is expensive and should not be called by the UI thread, it might ruin the responsiveness). :)
Luc Pattyn [My Articles]
-
Hi, if you create some objects (a Pen, Brush or any other instance of a class that has a public Dispose() method) inside your OnPaint method, you MUST call Dispose for them. Additionally, if you need the same object over and over (such as new Pen(myColor)) then I suggest you create it only once and keep it in a class member (rather than continuously creating and disposing and collecting them). And finally, if the objects you need correspond to system objects (such as the ones available in SystemPens, SystemBrushes,...) I suggest you use these (without new, without Dispose !). And I do not recommend you call GC.Collect at all (the gc works fine all by itself), and certainly not from inside any OnPaint method ! (GC.Collect is expensive and should not be called by the UI thread, it might ruin the responsiveness). :)
Luc Pattyn [My Articles]
Hi, from debug of my program i noticed that Update(); is making bad effect and may be the reason of memory leak so can any one tell me what is the disadvantages of it thanx Generator
-
Hi, from debug of my program i noticed that Update(); is making bad effect and may be the reason of memory leak so can any one tell me what is the disadvantages of it thanx Generator
Hi, if you mean Control.Update() I dont know why you think you need it. In all the programs I have ever developed I never felt the need for it: correct program design (including the use of background threads for longwinding tasks) results in good responsiveness without problems. On the other hand, Update does NOT cause memory leaks; only errors in your code ( or in .NET itself, rather unlikely) can do that, e.g. not calling Dispose(). If you want us to be able to help you, you should explain more about your app, and publish the pieces of code you suspect yourself, rather than using obscure observations (a very big space of memory, unreachable code, bad effect). :)
Luc Pattyn [My Articles]
-
Hi, if you mean Control.Update() I dont know why you think you need it. In all the programs I have ever developed I never felt the need for it: correct program design (including the use of background threads for longwinding tasks) results in good responsiveness without problems. On the other hand, Update does NOT cause memory leaks; only errors in your code ( or in .NET itself, rather unlikely) can do that, e.g. not calling Dispose(). If you want us to be able to help you, you should explain more about your app, and publish the pieces of code you suspect yourself, rather than using obscure observations (a very big space of memory, unreachable code, bad effect). :)
Luc Pattyn [My Articles]
hi, ok you are right i am making a packet that moved on a line when i click a button and stopped when i click stop so i use thread but the stop button be not responsive and the app hang up so one here advice me to use BackgroundWorker so i use it but i have a very large consuming of memory when i click the first button i donot no why so i try to solve it hardly so last i remove update then the memory consuming decreased but not every time i start my application run i work well some times it work bad so i tried also so to ask a question if i called a method for a thread or backgroundworker and in this method other method is called the second method would be run by the program process or by this thread if by program then i will correct my code else i will publish the pieces of code to see(i miss understanding) thanx alot Generator
-
hi, ok you are right i am making a packet that moved on a line when i click a button and stopped when i click stop so i use thread but the stop button be not responsive and the app hang up so one here advice me to use BackgroundWorker so i use it but i have a very large consuming of memory when i click the first button i donot no why so i try to solve it hardly so last i remove update then the memory consuming decreased but not every time i start my application run i work well some times it work bad so i tried also so to ask a question if i called a method for a thread or backgroundworker and in this method other method is called the second method would be run by the program process or by this thread if by program then i will correct my code else i will publish the pieces of code to see(i miss understanding) thanx alot Generator
Hi, I did not understand very much of what you wrote. It seems like you want some animation to start when a "Start" button is pressed, and stopped when a "Stop" button is pressed, and furthermore the "Stop" button does not (always) work reliably. There can be several causes for this: - you might be doing too much stuff in the UI thread - CPU load might be high at or above the priority of the UI thread - some logical error in the communication bewteen the "Stop" button handler, and the thread that does the animation. Maybe this info will help: methods are executed on the same thread as the code that calls them (unless you do very special things, such as calling Invoke). Some asynchronous stuff is executed on another method automatically (e.g. a tick handler for all timers, except a Windows.Forms.Timer). if you try to modify the user interface (i.e. add/remove/modify a Control) from a thread other than the UI thread (the one you have at start) then: - under .NET 1.1 your program may freeze, or behave unpredictably - under .NET 2.0/3.0 you will get some "CrossThread" Exception (you could set some flag false to skip the checks, but then the app may freeze again). You may want to create your own background thread, so you can fully control it (set priority, kill/abort it, etc). You may want to find and read an MSDN article entitled "How to: Use a Background Thread to Search for Files " which probably explains more on InvokeRequired, Invoke, threads, etc. :)
Luc Pattyn [My Articles]
-
hi, ok you are right i am making a packet that moved on a line when i click a button and stopped when i click stop so i use thread but the stop button be not responsive and the app hang up so one here advice me to use BackgroundWorker so i use it but i have a very large consuming of memory when i click the first button i donot no why so i try to solve it hardly so last i remove update then the memory consuming decreased but not every time i start my application run i work well some times it work bad so i tried also so to ask a question if i called a method for a thread or backgroundworker and in this method other method is called the second method would be run by the program process or by this thread if by program then i will correct my code else i will publish the pieces of code to see(i miss understanding) thanx alot Generator
private void btn_simulate_Click(object sender, EventArgs e) { this.btn_simulate.Enabled = false; this.btn_stop.Enabled = true; closeThreadRequested = false; if (!packetsMovingThread.IsBusy) packetsMovingThread.RunWorkerAsync(); } /// /// When press the button stop simulation /// private void btn_stop_Click(object sender, EventArgs e) { closeThreadRequested = true; this.btn_stop.Enabled = false; this.btn_simulate.Enabled = true; if (packetsMovingThread.IsBusy) packetsMovingThread.CancelAsync(); } /// /// An event handler that called when thread of background task begin /// private void packetsMovingThread_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (worker.CancellationPending) e.Cancel = true; else dataPacket.simulate(lineArray, closeThreadRequested, ref source_pnt, ref destination_pnt, packetsMovingThread); } /// /// An event handler that called when thread of background task Aborted /// for any reason(Error,Cancel,Normal Completion) /// private void packetsMovingThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) MessageBox.Show(e.Error.Message); else if (e.Cancelled) MessageBox.Show("Cancelled"); } /// /// An event handler that called when thread of background task has changes in /// its state or by calling ReportChanges() method /// private void packetsMovingThread_ProgressChanged(object sender, ProgressChangedEventArgs e) { if (closeThreadRequested == false && !(source_pnt.IsEmpty)) { dataPacket.drawPacket(this.CreateGraphics(), source_pnt); } Invalidate(drawingFrame); // update here make bad effect on memory //Update(); }
public void drawPacket(Graphics grfx, Point location) { Pen pen = new Pen(type); SolidBrush solidBrush =
-
private void btn_simulate_Click(object sender, EventArgs e) { this.btn_simulate.Enabled = false; this.btn_stop.Enabled = true; closeThreadRequested = false; if (!packetsMovingThread.IsBusy) packetsMovingThread.RunWorkerAsync(); } /// /// When press the button stop simulation /// private void btn_stop_Click(object sender, EventArgs e) { closeThreadRequested = true; this.btn_stop.Enabled = false; this.btn_simulate.Enabled = true; if (packetsMovingThread.IsBusy) packetsMovingThread.CancelAsync(); } /// /// An event handler that called when thread of background task begin /// private void packetsMovingThread_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (worker.CancellationPending) e.Cancel = true; else dataPacket.simulate(lineArray, closeThreadRequested, ref source_pnt, ref destination_pnt, packetsMovingThread); } /// /// An event handler that called when thread of background task Aborted /// for any reason(Error,Cancel,Normal Completion) /// private void packetsMovingThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) MessageBox.Show(e.Error.Message); else if (e.Cancelled) MessageBox.Show("Cancelled"); } /// /// An event handler that called when thread of background task has changes in /// its state or by calling ReportChanges() method /// private void packetsMovingThread_ProgressChanged(object sender, ProgressChangedEventArgs e) { if (closeThreadRequested == false && !(source_pnt.IsEmpty)) { dataPacket.drawPacket(this.CreateGraphics(), source_pnt); } Invalidate(drawingFrame); // update here make bad effect on memory //Update(); }
public void drawPacket(Graphics grfx, Point location) { Pen pen = new Pen(type); SolidBrush solidBrush =
Hi, your code contains this.CreateGraphics() which CREATES a Graphics, and hence requires a matching Dispose() which is not present. I expect this is your main problem. I dont see how exactly your backgroundworker is working, how iterations are achieved, and how the drawing is handled by the UI thread; so there might be additional causes for problems. ADDED: drawing a packet should be part of your paint handler (so it gets called whenever anything needs to be repainted, i.e. also when something that was temporarily hiding your form gets removed, or when your app gets restored from being minimized); your animation should just modify the parameters that control the drawing (such as coordinates) and then make sure a repaint gets ordered (typiucally by calling Invalidate). :) -- modified at 17:48 Sunday 8th April, 2007
Luc Pattyn [My Articles]
-
Hi, your code contains this.CreateGraphics() which CREATES a Graphics, and hence requires a matching Dispose() which is not present. I expect this is your main problem. I dont see how exactly your backgroundworker is working, how iterations are achieved, and how the drawing is handled by the UI thread; so there might be additional causes for problems. ADDED: drawing a packet should be part of your paint handler (so it gets called whenever anything needs to be repainted, i.e. also when something that was temporarily hiding your form gets removed, or when your app gets restored from being minimized); your animation should just modify the parameters that control the drawing (such as coordinates) and then make sure a repaint gets ordered (typiucally by calling Invalidate). :) -- modified at 17:48 Sunday 8th April, 2007
Luc Pattyn [My Articles]
hi Luc Pattyn, actually i wanna to thank u for ur efforts to help me. first i think the problem is that i donot understand BackgroundWorker well (it is very complicated) so i tried before using it to use normal thread but i failed but now i knew my error and i fix it in the normal thread not the BackgroundWorker Thread and now no(unreachable code,memory leak ...)these things are not found now any way thanx to u very much bye Generator
-
hi Luc Pattyn, actually i wanna to thank u for ur efforts to help me. first i think the problem is that i donot understand BackgroundWorker well (it is very complicated) so i tried before using it to use normal thread but i failed but now i knew my error and i fix it in the normal thread not the BackgroundWorker Thread and now no(unreachable code,memory leak ...)these things are not found now any way thanx to u very much bye Generator
You're welcome.
Luc Pattyn [My Articles]