Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Memory leak again
11 replies. Latest Post by rambler.elf on January 24, 2008.
(0)
haughtycool
Member
81 points
35 Posts
01-20-2008 11:58 PM |
I found 2 memory leak case:
1. I created a page which loads and unloads a dynamic resource, when I refresh the page, the memory increase about 7Mb
Please download source code here : http://rapidshare.com/files/85335888/TestContext.zip and give me an advice if you can
2. I create a page which loads and unloads many custom controls, and the memory doesn't decrease when the page unloads the custom control
Please download source code here : http://rapidshare.com/files/85335555/TestContext.rar and give me an advice if you can
PS: You can use Windows Task Manager to monitor the usage memory of the SilverLight page
Thanks for any reply!
rambler.elf
Participant
806 points
141 Posts
01-22-2008 3:41 PM |
Hi,
That's Simple!, in the first project you've some bugs like changing the collection in your foreach statements which causes an exception, and objects remains in memory for longer time... (you have just to correct them! - did you even tried that for more than one image object!, it doesn't work due to issue mentioned -)
another thing (which doesn't related to the problem) is : if(!(obj is Image)) c.Children.Remove(obj as Visual);// was obj as Image
for the second project just change i<10000 to i<100 (this causes an exception again)
now check with GC.GetTotalMemory - there is no memory leak now...
01-23-2008 1:21 AM |
Thank you so much
01-23-2008 10:31 PM |
How about this case. It seems the memory leak
String script = "<Canvas><Image Source=\"bg.jpg\"/></Canvas>"; Object objApp = XamlReader.Load(script); Canvas c = objApp as Canvas; for (int i = c.Children.Count - 1; i >= 0; i--) { Image img = c.Children[ i ] as Image; try { c.Children.RemoveAt(i); this.Children.Add(img); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message); } } c.Children.Clear();
mchlsync
Star
14566 points
2,730 Posts
01-23-2008 10:40 PM |
haughtycool:Thank you so much
How come your heart is broken? :P
01-24-2008 3:07 AM |
haughtycool: How about this case. It seems the memory leak String script = "<Canvas><Image Source=\"bg.jpg\"/></Canvas>"; Object objApp = XamlReader.Load(script); Canvas c = objApp as Canvas; for (int i = c.Children.Count - 1; i >= 0; i--) { Image img = c.Children[ i ] as Image; try { c.Children.RemoveAt(i); this.Children.Add(img); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message); } } c.Children.Clear();
No, I don't think so, GC works fine in that case, I've tested it and there was no memory leak...
could you tell me why you think there is memory leak in that case?
01-24-2008 3:23 AM |
Yes, when I refresh the page, the memory increases about 7Mb
01-24-2008 3:53 AM |
are you using GC.GetTotalMemory(true) or you're using task manager?
GC.GetTotalMemory returns best available approximation of the number of bytes currently allocated in managed memory. I've tested your case with GC and there was no memory leak on managed code...
but if you're using the task manager, you may see some memory leaks after reloading page continuously; unfortunately, most memory leaks are directly related to the IE garbage collector.
01-24-2008 4:08 AM |
Maybe you're right. If I repair the source code as follow, the memory of IE doesn't increase
String script = "<Canvas><Image Source=\"bg.jpg\"/></Canvas>"; Object objApp = XamlReader.Load(script); Canvas c = objApp as Canvas; this.Children.Add(c); for (int i = c.Children.Count - 1; i >= 0; i--) { Image img = c.Children[ i ] as Image; try { c.Children.RemoveAt(i); this.Children.Add(img); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message); } } c.Children.Clear();
01-24-2008 4:35 AM |
Hmmm, I didn't see your code completely and I thought you're removing parent canvas...
I think the problem returns to the silverlight plug-in cos at the end you're calling c.Children.Clear() and in the for statement you're just adding childs so the parent canvas remains in memory(objApp and c pointers) and loaded to the temporary xaml object tree, but if you set c and objApp to null at the end of method, there will be no memory leak so far.(it could be the reason why we see no leak in managed code)
I believe these object which is loaded by XamlReader and are no longer available causes the memory leak...
01-24-2008 5:14 AM |
However when I add the statement this.Children.Remove(c); at the end of code block, the memory increases when I refresh the page. Is this funny
Thank you. Our discussion is very interesting.
01-24-2008 5:25 AM |
haughtycool: However when I add the statement this.Children.Remove(c); at the end of code block, the memory increases when I refresh the page. Is this funny
Idon't know but if your code is something like :
String script = "<Canvas><Image Source=\"bg.jpg\"/></Canvas>"; Object objApp = XamlReader.Load(script); Canvas c = objApp as Canvas; for (int i = c.Children.Count - 1; i >= 0; i--) { Image img = c.Children[ i ] as Image; try { c.Children.RemoveAt(i); this.Children.Add(img); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message); } } c.Children.Clear(); then this.Children.Remove(c); does not work cos you're not going to add it to the page before removing it!
and if you're adding it to page, there will be no memory leak cos the GC will automatically collects them and this.Children.Remove(c); is unnecessary.
haughtycool: Thank you. Our discussion is very interesting.
no problems, glad to help you