David Jacob
David Jacob

Reputation: 213

Memory Leak Issue in Windows Phone Develoment - Silver Light Framework

I am creating one game in Windows phone using c# and silver light platform. I am new in this technology and currently facing memory leak issue.

As per research and study I have done, I have tried to do all the things including events, string and usage of garbage collector.

Can any one please give common tips to best utilize garbage collector and memory management since it seems issue right now. When my garbage collector reaches 5 lac size, it stop collecting new things and application is getting crash.

I also tried empty the garbage collectore passing parameter 0 in gc collect but it is crashing the app.

Can you please guide and help for basic things to take care, process to follow to avoid such issues and best use of GC collect?

Thanks in advance, Jacob

Upvotes: 1

Views: 538

Answers (2)

Richard Szalay
Richard Szalay

Reputation: 84754

In general, you should never have to call GC.Collect yourself as unused objects will be automatically collected every few seconds.

As for what can prevent objects from being collected, it comes down to them being "rooted". Roots include:

  • Any static references
  • Any references held by the run loop (your Application is the closest thing here)
  • Anything being displayed on the current page or any page behind it
  • Anything referenced by any of the above (including UI events), or referenced by anything that is referenced by any of the above (etc).

In the above scenarios, those objects and any objects they hold a reference to cannot be GC'd. So as for advice:

  • Avoid defining anything as static
  • Be careful how many objects are held by Application
  • Avoid a navigation model that allows your back stack to grow to ulimited levels
  • Potentially look at setting references to large data sets to null in your page/viewmodel's OnNavigatedFrom method and re-initialise them in OnNavigatedTo

I'd recommend using the Windows Phone Profiler, which comes with the 7.1 SDK. It will tell you what objects are in memory and why.

Upvotes: 3

TheGeekYouNeed
TheGeekYouNeed

Reputation: 7539

Without seeing any of your code, it is difficult to give specific advice.

However, I strongly suggest you run a memory profiling tool like ANTS Memory Profiler or .Net Memory Profiler. These tools will show you what portions of your code are never released and are very helpful in making the adjustments that you need.

Upvotes: 0

Related Questions