0

First I'm sorry that I'm asking this totally ambiguous question. I have encountered a very confusing problem that I don't know how to solve.

It is not even possible to write some sample code here because it is related to my application as a whole. I'll try to explain this though.

I have two Canvas objects:

  • Canvas A
  • Canvas B

that I'm using in my application to draw Line objects on them. I also have two Button objects that I use to switch between Canvas A and Canvas B.

Now When I switch to Canvas A I start drawing on Canvas A and I clear all of the children in Canvas B and when I switch to Canvas B I start drawing on Canvas B and I clear the children of Canvas A.

After some time going back and forth between these two canvases, my program becomes very slow. I tried Ant Profiler and took memory snapshots. Once when I start the program and once when I feel that it has become slow.

The difference in size is around 135 KB. Is this a memory leak? 135 KB doesn't seem to be that much? What else can cause the problem?

Do the Line objects that I draw each time when I switch reside and aggregate in the memory despite the fact that I clear the children of the canvas? Since the number of the Line objects are high they may cause a problem if they reside and aggregate each time. How can I make sure?

Any suggestions are truly appreciated.

Update:

when I switch to CanvasB this is the code I use to clean the CanvasA:

CanvasA.Children.Clear();

I'm using DrawingVisual to draw the lines.

Vahid
  • 5,144
  • 13
  • 70
  • 146
  • I'd suspect it has something to do with how you're clearing the canvas'. You should add the relevant code. – Sayse May 08 '14 at 08:06
  • How do you Draw, how do you Clear? – H H May 08 '14 at 08:06
  • @Sayse @ Henk I updated the question. – Vahid May 08 '14 at 08:09
  • If you are using `DrawingVisual` instead of `Canvas.Children.Add(line)`, I'm not sure, but shouldn't you deal with `DrawingContext` rather than clear `Canvas`? – Bolu May 08 '14 at 08:37
  • I suggest you see if forcing the garbage collector to run fixes the problem or maybe makes it not as bad. See http://stackoverflow.com/questions/4257372/how-to-force-garbage-collector-to-run and http://msdn.microsoft.com/en-us/library/ee787088(v=vs.110).aspx – philologon May 08 '14 at 08:41
  • @philologon I tried it too. It doesn't happen to solve the problem either. What has confused me is that the difference between the two memory snapshots is very small. So it is not memory leak is it? – Vahid May 08 '14 at 08:52
  • @Bolu I'm using `using` when drawing on the `DrawingVisual` so I guess it is disposed after that right? – Vahid May 08 '14 at 08:53
  • 1
    Ok guys after so much trouble I think I found the culprit. It was the `Event Listeners`. Thank you all. – Vahid May 08 '14 at 08:56

0 Answers0