I know the answer (it's 42)

A blog on coding, .NET, .NET Compact Framework and life in general....

July, 2010

Posts
  • I know the answer (it's 42)

    Windows Phone 7 App Development: When does the GC run

    • 11 Comments

    Cub

    If you are looking for information on the new Generational GC on Windows Phone Mango please visit http://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 http://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
  • I know the answer (it's 42)

    Date format

    • 7 Comments

    Let me start by saying that using mm-dd-yyyy is just plain wrong. No really it just doesn’t make any sense to me. Neither does it make any sense to most people world-over if you go my the date-format map up at http://en.wikipedia.org/wiki/File:Date_upd1.PNG

    If one uses dd-mm-yyyy it makes sense because it’s in decreasing order of granularity (kind of LSB first). yyyy-mm-dd makes ever more sense because

    1. It’s in decreasing order of granularity
    2. Natural ordering in many ways like telephone numbers (country-code, area-code, local-code, number) or IP address
    3. String sorting automatically sorts by that date. E.g. I can easily have folders/files named in this order and DIR lists them nicely sorted out (yea I know I can sort by date/time as well).

    d:\MyStuff\Personal\Pictures>dir 2010*
    Volume in drive D is Data
    Volume Serial Number is 3657-F386

    Directory of d:\MyStuff\Personal\Pictures

    06/17/2010  01:06 PM    <DIR>          2010_0501
    06/17/2010  01:07 PM    <DIR>          2010_0504
    06/17/2010  01:16 PM    <DIR>          2010_0508
    06/17/2010  01:20 PM    <DIR>          2010_0509
    06/17/2010  01:24 PM    <DIR>          2010_0515
    06/17/2010  01:29 PM    <DIR>          2010_0517
    06/17/2010  01:30 PM    <DIR>          2010_0523
    06/17/2010  01:33 PM    <DIR>          2010_0528
    06/17/2010  01:37 PM    <DIR>          2010_0529
    06/17/2010  01:43 PM    <DIR>          2010_0605
    06/17/2010  01:47 PM    <DIR>          2010_0606
    06/21/2010  08:40 PM    <DIR>          2010_0616
    06/28/2010  10:33 PM    <DIR>          2010_0619
                   0 File(s)              0 bytes
                  13 Dir(s)  55,925,829,632 bytes free

    But I just cannot fathom why would anyone use mm/dd/yyyy. In what way is that intuitive?

Page 1 of 1 (2 items)