Wednesday, September 02, 2009

NETCF: GC and thread blocking

Hyderabad Zoological Park

One of our customers asked us a bunch of questions on the NETCF garbage collector that qualifies as FAQ and hence I thought I’d put them up here.

Question: What is the priority of the GC thread
Answer: Unlike some variants of the desktop GC and other virtual machines (e.g. JScript) we do not have any background or timer based GC. The CLR fires GC on the same thread that tried allocation and either the allocation failed or during pre-allocation checks the CLR feels that time to run GC has come. So the priority of the GC thread is same as that of the thread that resulted in GC.

Question: Can the GC freeze managed threads that are of higher priority than the GC thread?
Answer: Yes GC can freeze managed threads which has higher priority than itself. Before actually running GC the CLR tries to go into a “safe point”. Each thread has a suspend event associated with it and this event is checked by each thread regularly. Before starting GC the CLR enumerates all managed threads and in each of them sets this event. In the next point when the thread checks and finds this event set, it blocks waiting for the event to get reset (which happens when GC is complete). This mechanism is agnostic of the relative priority of the various threads.

Question: Does it freeze all threads or only Managed threads?
Answer: The NETCF GC belongs to the category “stop-the-world GC”. It needs to freeze threads so that when it is iterating and compacting managed heap nothing on it gets modified. This is unlike some background/incremental GC supported by other virtual machines.

GC freezes only managed threads and not other native threads (e.g. COM or host-threads). However, there are two exclusions even on the managed threads

  1. It doesn’t freeze the managed thread that started GC because the GC will be run on that thread (see answer to first question)
  2. Managed threads that are currently executing in native p/invoke code is not frozen either. However, the moment they try to return to managed execution they get frozen. (I actually missed this subtle point in my response and Brian caught it :))

Hope this answers some of your questions. Feel free to send me any .NET Compact Framework CLR questions. I will either answer them myself or get someone else to do it for you :)

3 comments:

Anonymous said...

Well, If you have ever considered writing some script to automate those things that you do many times every day, then you have a great and easy way to automate the entir e process. It comes without saying that a few imacros and firefox add-ons can automate all actions connected to a browser (Mozilla). If you wish to automate your system as such, then you'll need another script, simple commands can automate your system. Even a beginner will automate things using a simple software called as sikuli all you need to do is to tell Sikuli what to do by giving some screenshots and straightforward commands.
A GUI will be used with Sikuli. Sikuli is an open siurce scripting application that will use a mix of straightforward commands like click, type, wait and so on. There's no internal API support, it simply searches the screen for the image in the screenshot-that implies that you can use it for anything. There is literally no limit on how you utilize it to automate things.
This is too good to be true, but if you watch the following video, you will come to know that this can be very easy. If you visit their home page, you get a lot of tutorials and support; it will be a cake walk even for beginners. You'll be able to check the video as well as get the software at [url=http://technoages.com/operating-system/apple-mac-os/automate-everything-through-a-simple-script-make-your-computer-listen-to-you/]TechnoAges.com[/url]

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.