Thursday, July 29, 2010

Windows Phone 7 App Development: When does the GC run


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

Wednesday, May 12, 2010

Inside the Windows Phone Emulator


Learn from the dev lead and PM of Windows Phone 7 Emulator on how it works and delivers the awesome performance.

Some key points

  1. The emulator is essentially a x86 based Virtual Machine running full image of Windows Phone 7 OS
  2. It emulates various peripherals (e.g. audio, networking), the list hopefully will grow in the future
  3. Supports multi-touch if your host PC has a touch screen
  4. It uses the GPU on the host PC to accelerate the graphics
  5. Shameless plug: One of the reason it’s possible to emulate x86 and still have managed apps running as-is, is because the runtime (.NET Compact Framework) supports both x86 and ARM seamlessly (provides JITer for both architectures and runs the same managed code on both seamlessly)

Get Microsoft Silverlight

Wednesday, April 28, 2010

Working in the United States

Eucalyptus trees-  Trek in the hills

I am sure everyone has read about the “you cannot possibly pronounce or spell” volcano in Iceland throwing up in the air and screwing up the entire flight system of this planet. While most people were reading about it from the comfort of their home I was doing the same while waiting in airports with a 5 year old tagged along. Anyways, 48 hours and a switch in the direction of flights got me to some countries I didn’t really plan to visit and over the Pacific to land in Seattle.

Going forward I will be working out of the Microsoft Redmond offices, doing pretty much the same stuff I was doing before (working on the .NET Compact Framework runtime).

Right now I am settling down with my family and my daughter is excited to see snow for the first time (she got lucky as there was a small snowfall on Saturday in Snoqualmie pass). The changes we need to adjust to is humongous, we left Hyderabad when it was 43° Celsius and now it’s 6° Celsius in Redmond. Oh sorry I should’ve said it was 110°F and now it’s  43°F in Redmond.

Wish me luck and hopefully finally I will be able to attend some nerddinners.

We Believe in Sharing

Good Morning

In Building NETCF for Windows Phone 7 series we put in couple of features to enhance startup performance and ensure that the working set remains small. One of these features added is code/data sharing.

Native applications have inherent sharing where multiple processes can share the same executable code. However, in case of managed code running on NETCF 3.5 even when multiple applications use the same assembly, the managed code in them are JITed in context of each process separately. This results in suboptimal resource utilization because of the following

  1. Repeated JITing of the same code
  2. Memory overhead of having identical copies of the same JITed code for every process running them. The execution engine also maintains records of the various types loaded and even though two (or more) processes may be loading the same types from the same assemblies; per process copies are maintained for them.

Together the overhead can be significant.

In the latest version of the runtime shipping with Windows Phone 7 (.NET Compact Framework 3.7) we added a feature to share both the JITed code and these type information across multiple managed processes.


The runtime essentially uses a client server architecture. We added a new kernel mode server
which is responsible of maintaining a system wide shared heap. The server on receiving requests
from the various client processes (each managed app is a client) JITs code and type information
into this shared heap. The shared heap is accessed Read-only from all the client apps and
Read/write from the server.

When a process requests for the same type to be loaded or code to be JITed it just re-uses the already loaded type info or JITed code from the shared heap.
What is shared

  1. Various Execution engine data-structures like loader type-information
  2. JITed code from a predefined list of platform assemblies (user code is not shared).

Do note that user code is not shared and neither is all platform code. Sharing arbitrarily would actually increase cost if they ultimately didn’t get reused in a lot of applications. The list of assemblies/data shared was carefully chosen and tuned to ensure only those are shared that provided the maximum performance benefit.

Sharing provides the following advantages

  1. Warm startup time is significantly reduced. When a new application is coming up a large
    percentage of the initial code and type-info is already found on the shared heap and hence
    there is significant perf boost. The exact number depends on the applications but it’s common
    to get around 30% gains
  2. Significant reduction in net working set as a lot of commonly used platform assembly JITed code is re-used
  3. Obviously there are other goodness like less JITing means less processing on the device

Like user code the system is also capable of pitching the shared code when there is a low memory situation on the device.

Wednesday, March 17, 2010

Comparing Windows Phone with .NET Compact Framework 3.5

Bidar - Fort

In the comments of my previous post Windows Phone 7 Series Programming Model and elsewhere (e.g. twitter, WP7 forum) there seems to be a lot of confusion around how NETCF 3.5 compares against the new WP7 programming model powered by NETCF 3.7. People are asking why some API got removed from 3.5, or is 3.7 a subset of NETCF 3.5 as they are not seeing some familiar 3.5 APIs.

First I’d urge you to go visit the Windows Phone 7 Series Programming Model to see what it offers.

Essentially NETCF 3.5 can be broken down to

  1. NETCF 3.5 runtime (the Execution Engine)
  2. NETCF 3.5 BCL
  3. NETCF 3.5 UI programming model (WinForm based)

Now each of this has been updated (in order) as follows

  1. NETCF 3.7 runtime (hugely updated Execution Engine)
  2. NETCF 3.7 BCL (essentially offering the Silverlight 3 BCL Apis with a small delta)
  3. The UI model has been replaced and offers two options
    1. Silverlight 3 UI + Windows Phone device specific features (e.g. accelerometer , location service)
    2. XNA features with Windows Phone device specific features (e.g. accelerometer , location service)

So the runtime is hugely updated (and hopefully you’d feel the improvements, especially in performance).

For #2 and #3 the level of difference is same in magnitude as you compare WinForm with Silverlight on desktop. They are simply two different programming models.

In case you hit a 3.5 API  that’s missing then it’s either because it’s (a) WinForm based API that got totally replaced, or (b) a BCL API that the Silverlight profile doesn’t support. The workaround is to search for how to do the same thing in Silverlight (e.g. use XDocument instead of XmlDocument). In some cases you’d get into situations where there is no direct alternative. E.g. there is no P/Invoke and if your code say P/Invoked to get data from a bar-code scanner you cannot do so now.

Also keep in mind that the bits available right now is just early preview bits and subject to a lot of change. The whole purpose of having CTPs is to hear our customers and ensure we use the remaining time before release in fixing issues that helps our customers the most. Hopefully some of the gaps will get closed before the final release.

Silverlight on Nokia S60 platform

Bidar - Fort

Today at MIX 2010 we announced Silverlight beta for Nokia S60 phones. Go download the beta from

Currently the supported platform is S60 5th Edition phones and in particular the 5800 XpressMusic, N97 and N97 mini. It works in-browser and only in the Nokia default web-kit based browser.

Silverlight on S60 uses .NET Compact Framework (NETCF) underneath it. I work in the dev team for the Execution Engine of NETCF and I was involved at the very beginning with getting NETCF to work on the S60 platform.

NETCF is extremely portable (more about that in later posts) and hence we didn’t have a lot of trouble getting it onto S60. We abstract away the platform by coding against a generic platform abstraction layer (PAL), but even then we had some challenges. Getting .NET platform on a non-Windows OS is challenging because so much of our code relies on Windows like functionality (semantics) which might not be available directly on other platforms.

As I was digging through emails I found the following screenshot that I had sent out to the team when I just got NETCF to work on the S60 Emulator…


As you can see the first code to run was a console application and it was not Hello World :). We have come a long way since then as evident from the touch based casual game running on the N97 shown below


Windows Phone 7 Series Programming Model

Bidar - Barid Shahi tombs

Just sometime back I posted on the MIX 2010 announcements. One of the major aspects of the announcement was

“The application model supported on Windows Phone 7 series will be managed only and will leverage Silverlight, XNA and the .NET Framework”

That’s a mouthful and includes 3 framework names in once sentence :). This was already disclosed and has resulted in some flutter over the web and twitter. Let me try to explain how these 3 interplays in the application model with the following diagram


Managed only

First of all the application model allows users to write only managed code (to repeat native code is not allowed). That means they do not have direct access to any native APIs and cannot use platform-invoke to call into native user or system code. So all resources including phone capabilities have to be accessed using the various managed APIs provided.

Two Flavors of applications (XNA and Silverlight)

There are 2 variants or flavors of applications users can write, XNA Games and Silverlight applications. Obviously the former is for writing games and the later is for your typical phone applications (nothing stops you from writing a Silverlight animation based game though). So you cannot mix-match SL and XNA UI in the same application.

However, do note that the traditional NETCF development using WinForm based UI is not supported.

Common Services available to both

That said, beyond the UI rendering and controls there is the common services which both SL and XNA applications can use. This includes all the phone capabilities like microphone, location, accelerometer, sound, media, networking, etc... Some of these features come from SL and others from XNA but land up in the common pool usable by either types of applications.

Core Runtime is .NET Compact Framework 3.7

The core runtime for both SL and XNA applications is .NET Compact Framework (henceforth called NETCF). This is the team I work for.  .NETCF is a highly portable and compact (as in smaller size and footprint) implementation of the .NET specification. It is designed to run on resource constrained devices and on disparate HW and SW configurations.I will have a post later on the detailed architecture or .NETCF.

Over the last two years a lot of work has gone into .NETCF to scale it in terms of features, performance and stress to meet the requirements of Windows Phone and other newer platforms on which it is being targeted. We also added a lot of features which I hope you will enjoy. Some of the features is just to reach parity with the desktop CLR and others are device specific and not available on the desktop.

Over this blog in the course of the coming months I’ll try to share with you what all we did to reach here.

MIX 2010 Announcements

Bidar Trip - Mohamad Gawan Madrasa

Today is a very big day for me and my team. The stuff we have been working on is finally went live on stage MIX 2010.

Windows Phone 7 is easily the most dramatic re-entry/reset Microsoft has undertaken in any field it’s in. We announced the phone in MWC and the reception it got was phenomenal. However, in the new connected devices world, the application platform+market story is as or more important than the device itself. Today at MIX we just disclosed details of that.

  1. The application model supported on Windows Phone 7 series will be managed only and will leverage Silverlight, XNA and the .NET Compact Framework. So basically you can reuse your C#/.NET and Silverlight skills to build amazing experience on the phone. I will get into details of this in the coming posts
  2. Microsoft just announced free Windows Phone Developer Tools (preview bits available for download head over to )
    1. This builds on the amazing development experience brought in by the Visual Studio 2010 shell.
    2. You can either get a standalone Visual Studio 2010 Express for Windows Phone Or an addin for your previously installed VS 2010
    3. Windows Phone 7 series emulator (yes you do not even need a device to develop for Windows Phone)
  3. A version of Expression Blend is also demonstrated for the Windows Phone (a free version will be available for download in the coming months)
  4. A new Windows Phone Marketplace is being put into place
    1. Developers can earn 70% revenue from the applications they sell of the marketplace
    2. The registration to the market place is not free but it’ll be free for the DreamSpark program for students

Our team was involved with building the IDE, the emulator and the core .NET runtime that powers the applications. So watch out and in the coming posts I’ll deep dive into these areas.

Sunday, January 17, 2010

Bidar road trip from Hyderabad

Just went for a short day trip to Bidar from Hyderabad on Jan 16, 2010. This post is to document the whole plan and quick tips  Since we travelled with GPS and Google maps have excellent coverage of the area we didn’t face any issues.

The way to Bidar

The route maps on Google here. The total distance came out to beBidar Trip - On the way around 115kms from Miyapur. The route is pretty straight forward. Get onto NH9 which goes from Hyderabad to Pune and onward to Mumbai. Drive around 85km and then take a right onto SH4 and drive around 26 km to reach Bidar (which is in Karnataka). The right turn is well marked out and clearly points that it goes to Bidar.

We started around 7:00 a.m from Hyderabad and even though we expected around 2 hours of driving it turned out to be 3 hours as we took a pit-stop in the Haritha Restaurant (APTDC) for breakfast. The restaurant came around 60-65 kms from Hyderabad. The choice of food was limited but the Poori and Idli we had was steaming hot and sumptuous. The bathrooms were clean as well :). The stop is highly recommended.

Bidar Trip - On the wayThe road was excellent all the way and traffic was sparse, I could drive easily at 100kmph. Just before reaching Bidar there is a reserve forest and when we were driving through a tiger leapt in front of our car. Ok I was kidding the forest is there and my daughter was trying to see a tiger :)



In Bidar

We took the following route plan inside Bidar


Bidar - BidriJust after entering Bidar (A) you’d see an old Fort gate on the right, get into it and you’ve reached the old city. After that you just need continue on the straight road. The un-mistakable clock tower (chaubara) would be infront out you.



Bidar Trip - Mohamad Gawan Madrasa

Our first stop was the remains of the ancient theological college (B), Mohamad Gawan Madrasa. Built in 1472 by Gawan, a Persian exile and scholar of the Bahmani court, this was one of the greatest centers of Islamic learning of its time, attracting students from all over. We got some nice shots of the place and the beautiful green parrots hovering around added to the color.

Bidar - Fort

From this we drove another km to the Bidar fort (C), which comes straight ahead. After getting through another set of fort gates we got into the fort.

Do note that the whole idea of fort gates is to make entry difficult and not allow you to see what is ahead, so drive carefully and honk (there is a sign that proclaims horn-mandatory).

Inside the fort we first went to the museum and then asked the folks their to show us around the fort. The fort is kept under lock-key to prevent people from scribbling pappu-loves-pinki on the walls and you need to explicitly ask to be shown around (and tip them at the end). We were shown around the various Mahals, especially the Rangeen Mahal took our breadth away. The museum also has some interesting pieces like locks which are bigger than my door and huge guns which could be carried only by people who anyway didn’t need them.

Just outside the museum is the canteen where we grabbed some food and coffee. The whole area around Bidar cultivates sugarcane, and if you are not explicit about it they’d put all of that in your cup of coffee. Energized with all that sucrose we ventured out to the back of the museum to see the diwan-e-aam and diwan-e-khaas.

After that we drove down to the Barid Shahi tombs (D). It has a nice little park and our daughter enjoyed the welcome break from seeing old buildings and enjoyed the slides and slings.

Bidar - GurudwaraOur last stop was the Gurudwara Nanak Jhira Saheb (E). Finding the was easy as there is a gate on the SH4 from where you need to get into the road that leads to the Gurudwara. Do remember to carry shawls for women and handkerchief for men to cover the hair which is mandatory to enter the Gurudwara. In case you forget you can always get them at the Gurudwara but putting cloth which hundreds have used before might be a bit yucky :).

We paid Rs10 to get some very tasty halwa which was loaded with Ghee. The Gurudwara had clean pay-n-use loo which helped :) 


We asked the Sardarji security guard for some pointers for good food and we pointed to a fantastic restaurant. It’s called Rohit Restaurant and is on the road that leads to the Gurudwara from SH4 and is just beside the police chowki. It’s Punjabi cuisine, vegetarian and is clean and the food proved to be excellent (their naan/daal-fry can hands down beat any 5-star restaurant). Highly recommended.

image On the way back we again went back to the Clock tower (choubara) area to pick up some Bidriware which is specialty of this City. These are hand made by putting in silver threads into copper, zinc allow casts. It’s pleasure to actually see them being made and buy from the artisans directly. Do not buy Bidri from the main road as they’d loot you. Ask for choubara and then when you reach that area ask around for the shops.

It was around 3:30 when we finished our Bidar tour and drove back to Hyd.


  1. Carry a map (print-out from Google) for Bidar city if you do not have GPS
  2. Start early so that you can finish the fort before it gets too hot
  3. Ask to be shown around inside Bidar fort and tip at the end
  4. Carry cloth to cover head if you plan to visit the Gurudwara
  5. The Rohit restaurant close to the Gurudwara is highly recommended.
  6. Do buy some Bidriware (small pieces comes for around Rs.200). However, you need to go to choubara area to buy them.

If you visit Bidar and want to get something added/updated to this post do leave a comment.

Friday, January 01, 2010

.NETCF: The mini Y2K

Kolkata Trip 2009

I am sure most people haven’t yet forgotten the Y2K problem. This year our team faced a mini Y2K, but don’t worry we anticipated it and all is well.

If you head over to NETCF Wikipedia page you’d notice the NETCF versions look as follows


A sample version number is


This represents .NETCF version 3.5 build on the 283rd day of 2007. I guess by now you can guess the problem. We use a single digit to represent the year. By that nomenclature, the version of the build churned out today would be 3.5.0001.0 which is lower than the one generated the year before and would fail to install.

These numbers are automatically generated by scripts on the server that churns out daily builds. The numbering system was invented a long time ago in early 2000 and no one bothered to fix it. We anticipated that it’s going to fail as we move into the new decade and have updated it to now have 2 digits for the year (and yes we know it will break again in the future, but hopefully that’s too far out to care right now).

Happy new Year.