다음을 통해 공유


Windows Phone 7 App Development: When does the GC run

Cub

If you are looking for information on the new Generational GC on Windows Phone Mango please visit https://blogs.msdn.com/b/abhinaba/archive/2011/06/14/wp7-mango-the-new-generational-gc.aspx

Many moons ago I made a post on When does the .NET Compact Framework Garbage Collector run. Given that a lot of things have changed since then, it’s time to make another post about the same thing.

For the developers coming to Windows Phone 7 (WP7) from the Windows desktop let me first clarify that the runtime (CLR) that is running on the WP7 is not the same as the one running on the desktop. The WP7 runtime is known as .NET Compact Framework (NETCF) and it works differently than the “desktop CLR”. For 90% of cases this is irrelevant as the WP7 developer targets the XNA or Silverlight programming model and hence what is running underneath is really not important. E.g when you drive you really do not care about the engine. This post is for the other 5% where folks do run into issues (smoke coming out of the car).

Moreover do note that when the GC is run is really an implementation detail that is subject to change.

Now that we have all the disclaimers behind us lets get down to the list.

The Garbage Collector is run in the following situations

  1. After some significant allocation:
    When an application tries to allocate managed memory the allocator first checks a counter that indicates the number of bytes of managed data allocated since the last GC. If this counter crosses a threshold (which is changeable and set to 1MB currently) then GC is fired (and the counter is obviously reset).
    The basic idea is that there has been significant allocation since the last GC and hence do it again.
  2. Resource allocation failure
    If some internal native allocation fails, like loadlibrary fails or JIT buffer allocation fails due to out-of-memory condition then GC is started to free up some memory and the allocation is re-attempted (only 1 re-attempt)
  3. User code can trigger GC
    Using the managed API System.GC.Collect(), user code can force a GC
  4. Sharing server initiated
    One of the new features in the WP7 CLR is the sharing server (see my post https://blogs.msdn.com/b/abhinaba/archive/2010/04/28/we-believe-in-sharing.aspx for details). In WP7 there is a central server coordinating all the managed processes. If this sharing server is notified of low memory condition, it starts GC in all the managed processes in the system.

The GC is NOT run in the following cases (I am explicitly calling these out because in various conferences and interactions I’ve heard folks thinking it might be)

  1. GC is not run on some timer. So if a process is not allocating any memory and there is no low-memory situation then GC will never be fired irrespective of how long the application is running
  2. The phone is never woken up by the CLR to run GC. GC is always in response to an active request OR allocation failure OR low memory notification.
  3. In the same lines there is no GC thread or background GC on WP7

 

For folks migrating from NETCF 3.5 the list below gives you the changes

  1. WinForm Application going to background used to fire GC. On WP7 this is no longer true
  2. Sharing server based changes are obviously new
  3. The GC quantum cannot be changed by user

Comments

  • Anonymous
    August 11, 2010
    Hi, How can i change .Net Compact Framework GC threshold value 1MB to more.is there any api's support or any register setting need to change? thanks.

  • Anonymous
    August 12, 2010
    Which platform are you referring to? In WP7 you cannot, in prior platforms you can change, see blogs.msdn.com/.../net-compact-framework-gc-quantum.aspx

  • Anonymous
    October 18, 2010
    Hello, do u have Carbide V  for Visual basic Net? I did lot of google but unable to find for VB.NET environment. I will appreciate if you could send me installation file to abdul.lateef@ymail.com Thank You

  • Anonymous
    October 18, 2010
    @khan IAre you asking about Carbide used for Nokia S60 development? In that case Carbide doesn't support VB or rather I'd better say VB doesn't work on S60.

  • Anonymous
    November 11, 2010
    What about Silverlight PhoneApplicationPage instances ? Are the pages garbage collected after the user presses the Back button ? If so, when does this collection usually occur ? Thanks.

  • Anonymous
    November 12, 2010
    Gabriel there is no special garbage collection of the pages. They are collected along with other objects using the triggers explained in this post.

  • Anonymous
    November 14, 2010
    I see. And is it safe to assume that once the user presses the back button on a page, that page is marked for garbage collection (together with the associated tree of visual elements) ? I've watched part of a MIX10 video that would indicate so, however it was dated March 2010 and i wanted to make sure this hasn't changed since then. Thanks again !

  • Anonymous
    November 15, 2010
    Gabriel, GC doesn't work at page level. The runtime in whose context GC is running isn't even aware of something called the page. GC just knows objects and references going out of them. So in your code if you have a reference to the page from some global object then it will not be collected inspite of you moving to the previous page with back button. The way to think of this would be, if the page is no longer used in the application, then it will get garbage collected sometime later and developer need not care about explicitely destroying it.

  • Anonymous
    August 17, 2011
    Since the GC has changed for Mango - we at least have generations now - can you update this post, or make a new one about what else has changed for Mango?

  • Anonymous
    August 17, 2011
    Aha - I found the updated article here blogs.msdn.com/.../wp7-mango-the-new-generational-gc.aspx You might want to edit this older one to have a link to that.

  • Anonymous
    August 17, 2011
    Thanks for the feedback Zman, I have updated this post with the link