Page view counter
Memory leak again Subscribe to this thread
Last post 01-24-2008 5:25 AM by rambler.elf. 11 replies.
Sort Posts:
01-20-2008 11:58 PM
Memory leak again

 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! 

Imagine there's no countries
It isn't hard to do
Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace

haughtycool

Loading...
Joined on 12-18-2007
Viet Nam
Posts 35
01-22-2008 3:41 PM
Marked as Answer
Re: Memory leak again

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 Stick out tongue

for the second project just change i<10000 to i<100 Stick out tongue (this causes an exception again)

now check with GC.GetTotalMemory - there is no memory leak now...

ASP.NET Blog

rambler.elf

Loading...
Joined on 12-28-2007
Posts 141
01-23-2008 1:21 AM
Re: Memory leak again

Thank you so muchGiftWilted Flower 

Imagine there's no countries
It isn't hard to do
Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace

haughtycool

Loading...
Joined on 12-18-2007
Viet Nam
Posts 35
01-23-2008 10:31 PM
Re: Memory leak again

How about this case. It seems the memory leak Stick out tongue 

 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();

Imagine there's no countries
It isn't hard to do
Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace

haughtycool

Loading...
Joined on 12-18-2007
Viet Nam
Posts 35
01-23-2008 10:40 PM
Re: Memory leak again

haughtycool:
Thank you so muchBroken Heart
 

How come your heart is broken? :P 

 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Best Regards,
Michael Sync

Microsoft WPF & Silverlight Insider
Blog : http://michaelsync.net


mchlSync

Loading...
Joined on 09-16-2005
Singapore
Posts 2,478
01-24-2008 3:07 AM
Re: Memory leak again

haughtycool:

How about this case. It seems the memory leak Stick out tongue 

 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? Confused

ASP.NET Blog

rambler.elf

Loading...
Joined on 12-28-2007
Posts 141
01-24-2008 3:23 AM
Re: Re: Memory leak again

Yes, when I refresh the page, the memory increases about 7Mb 

Imagine there's no countries
It isn't hard to do
Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace

haughtycool

Loading...
Joined on 12-18-2007
Viet Nam
Posts 35
01-24-2008 3:53 AM
Re: Re: Memory leak again

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.

ASP.NET Blog

rambler.elf

Loading...
Joined on 12-28-2007
Posts 141
01-24-2008 4:08 AM
Re: Re: Re: Memory leak again

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();

Imagine there's no countries
It isn't hard to do
Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace

haughtycool

Loading...
Joined on 12-18-2007
Viet Nam
Posts 35
01-24-2008 4:35 AM
Re: Re: Re: Memory leak again

Hmmm, I didn't see your code completely and I thought you're removing parent canvas... Stick out tongue

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...

ASP.NET Blog

rambler.elf

Loading...
Joined on 12-28-2007
Posts 141
01-24-2008 5:14 AM
Re: Re: Re: Re: Memory leak again

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. 

Imagine there's no countries
It isn't hard to do
Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace

haughtycool

Loading...
Joined on 12-18-2007
Viet Nam
Posts 35
01-24-2008 5:25 AM
Re: Re: Re: Re: Memory leak again

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! Stick out tongue

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 Smile

ASP.NET Blog

rambler.elf

Loading...
Joined on 12-28-2007
Posts 141
Microsoft Communities