Skip to main content

Microsoft Silverlight

Answered Question Avoid memory leak in silverlight applicationRSS Feed

(0)

Upendran
Upendran

Member

Member

0 points

4 Posts

Avoid memory leak in silverlight application

Hi

    How to avoid the memory leak in silverlight application?

    How to clear the object?

    Can anybody provide sample code for avoiding the memory leak in silverlight 2.0

Thanks and regards,

Upendran P.V

Sergey.Lutay
Sergey.L...

Contributor

Contributor

7256 points

1,354 Posts

Re: Avoid memory leak in silverlight application

Hi,

You can use Garbage Collector for clean memory.

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

Blog

Twitter

Sincerely,
Sergey Lutay

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Avoid memory leak in silverlight application

Hello,

Actually, this is very big topic and you will need to conside a lot of things. .NET GC did very good job for cleaning up the object but of course, you have to be very careful when you are writing the code. The most of memory leak issues in WPF or Silverlight are becase of subscribing event handlers carelessly .

here is a nice discussion about this question in Stackoverflow. http://stackoverflow.com/questions/794466/how-to-avoid-memory-leaks

Hope you will get some idea from this link.

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

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Avoid memory leak in silverlight application

Sergey.Lutay:
You can use Garbage Collector for clean memory.

IMO, developers should not use GC.Collect without knowing the details of GC because GC is not going to collect as long as your object has some references. Instead, it will just upgrade the generation (e.g. gen0 to gen1). You may interest to read this discussioon <link>

I never recommend any developer to use GC.Collect even we found some code as below in  System.Web.ISAPIRuntime :)

public void DoGCCollect()
{
   
for (int i = 10; i > 0; i--)
   
{
        GC
.Collect();
   
}
}

 

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

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


Upendran
Upendran

Member

Member

0 points

4 Posts

Re: Avoid memory leak in silverlight application

Thank u for ur reply.

I had the used methods like GC.Collect() and GC.SupressFinalize(). But it's not working.

Can u provide a sample code for clearing the object of particular User Control. 

Thanks and regards,

Upendran P.V.

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP
Answered Question

Re: Avoid memory leak in silverlight application

Please read our answers first. Thanks. 

Upendran:
Can u provide a sample code for clearing the object of particular User Control.

1. What did you write in User Control?

2. How did you check whether those objects are already clear or not? Are you using any profiling tool?

 

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

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


silverstarter
silverst...

Member

Member

216 points

146 Posts

Re: Avoid memory leak in silverlight application

hmm.. ok dont use gc.collect()

but what can i do if the used memory (on hdd) runs into 4+ GB ?

i am removing each existing eventhandler in that program in a foreach loop with -=

and then call children.clear()

when i am rebuilding the UI (quite a lot functions, something around 500-1500 including eventhandler and no i cant do it another way ;) )

is there any way to check if theres something missing?

on the testsystem the C: hdd is almost full and if that prog is tested it runs into a proxy error cause the hdd is overused

if i try it on local system the (local) hdd-memory usage goes up do ish 5GB then it seems to find an end (not growing ahead)

besides that increddible memory usage the program works realy nice!

 

could any one help me ?

 

greetings

Silverstarter

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities