Saturday, January 21, 2006

Excellent Garbage Collection Blog - Chris Lyon's Blog

This page has lots of indepth info about the GC. Very good reading in deed. http://blogs.msdn.com/clyon/ Here are some excerpts: A few GC links that Chris has collected : http://blogs.msdn.com/clyon/archive/2004/09/14/229477.aspx Patterns and Practices for Dispose and Finalization: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconfinalizedispose.asp About Dispose: At what point in an object’s lifetime does the GC call Dispose. The answer: Never.

Dispose is just a method, like any other. Its purpose is allow deterministic resource cleanup, much like C++’s destructors, but without freeing the object’s memory. Like a C++ destructor, the developer puts whatever cleanup code is necessary to be performed when the object’s lifetime is up. Unlike a destructor, the object lives on after being "disposed of". This means an object may be in an invalid state after being disposed, and developers should consider using ObjectDisposedExceptions when methods in a disposed object are called.

What happens when you call Dispose()

Any cleanup your object need done in a timely fashion should be done in Dispose. Things like closing database connections, closing files, releasing bitmaps, etc. Unmanaged resources in particular should be released in Dispose. This is because the GC has no knowledge of anything not allocated on the managed heap. For example, a Bitmap object that encapsulates a 2MB image file reports only its managed size (the size of the managed object) to the GC. The GC knows nothing about the 2MB unmanaged image, thus will not collect it from memory. By calling Dispose, you tell the Bitmap object that you are finished with it, and it will release the image itself.

What about the finalizer?

There are a few reasons why not to rely on the finalizer to clean up resources that I’ll cover in a future blog entry on finalizers. The main reasons you should be concerned with are performance and determinism. Finalizers are expensive to run, and there’s no order (or even guarantee) that they will be run (for example, the finalizer thread may time out, or be killed on AppDomain unload).

That being said, the finalizer should be your last chance to clean up resources, in case someone using your class forgets to dispose of it when done. A call to Dispose inside the finalizer reduces duplication of code. However, you want to make sure the resources don’t get released twice. In your Dispose method, you want to make sure you call GC.SuppressFinalize(this) after your clean up code. If you suppress the finalizer before the clean up, you’re limiting your ability to recover from failures during the cleanup.

Some of you may have noticed a race condition in my description above. Consider a situation where the call to Dispose is the last time your object is being referenced. As soon as Dispose is entered, the object is eligible for collection by the GC. Before that happens, the object’s finalizer gets called, which calls Dispose. You now have two threads inside Dispose, possibly double-freeing resources (this could be bad, as in the case of GCHandles). Make sure you follow the pattern I linked to above and use a disposing flag to avoid problems like this.

.net CLR garbage collection

While looking through our application on how to optimize it, I had several questions about the debugger, like: 1. When does the CLR decide to run the garbage collection (GC). 2. What is the size of the managed heap. How does it resize 3. What are the implications of GC on speed of the application. Found all my answers and more at these websites. The following 2 give an excellent introduction to the GC algorithm that .NET uses. (recommended read) http://msdn.microsoft.com/msdnmag/issues/1100/GCI/ http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/ http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetGCbasics.asp Did you know that in tests, the CLR was quicker in allocating memory then normal win32 allocations. Apparently, the reason is that one can always be assured that when the CLR is requested for memory consecuitevely the space will be contigous and hence could all be in the CPU's memory, whereas in Win32, the memory could be fragmented and all over the place. Under the normal course of events, the GC performs periodic collections after every 1MB of allocations, the heap is compacted if it gets too fragmented, and empty segments are returned to the operating system as long as the collector’s 1 MB cache is full (this is for the .NET compact framework) http://blogs.msdn.com/stevenpr/archive/2005/12/14/503818.aspx Another page that is very good (this is a white paper) http://www.gotdotnet.com/team/libraries/whitepapers/resourcemanagement/resourcemanagement.aspx The following pages(blogs) have lots of in depth info about the operations of the garbage collector in .NET Using GC Efficiently – Part 1 by Maoni http://blogs.msdn.com/maoni/archive/2004/06/15/156626.aspx Using GC Efficiently – Part 2 by Maoni http://blogs.msdn.com/maoni/archive/2004/09/25/234273.aspx Using GC Efficiently – Part 3 by Maoni http://blogs.msdn.com/maoni/archive/2004/12/19/327149.aspx Using GC Efficiently – Part 4 by Maoni http://blogs.msdn.com/maoni/archive/2005/05/06/415296.aspx Using performance counters http://blogs.msdn.com/maoni/archive/2004/06/03/148029.aspx Tools for debugging memory related problems http://blogs.msdn.com/maoni/archive/2004/11/08/254288.aspx Finalization and GC http://blogs.msdn.com/maoni/archive/2004/11/04/252697.aspx

Tuesday, January 03, 2006

Corpscon Version 6.0

Corpscon Version 6.0: "Corpscon, Version 6.0, is a MS-Windows-based program which allows the user to convert coordinates between Geographic, State Plane, Universal Transverse Mercator (UTM) and US National Grid systems on the North American Datum of 1927 (NAD 27), the North American Datum of 1983 (NAD 83) and High Accuracy Reference Networks (HARNs). Corpscon uses the National Geodetic Survey (NGS) program Nadcon to convert between NAD 27, NAD 83 and HARNs. Corpscon, Version 6.0, performs vertical conversions to and from the National Geodetic Vertical Datum of 1929 (NGVD 29) and the North American Vertical Datum of 1988 (NAVD 88). Vertical conversions are based on the NGS program Vertcon and can be performed for the continental U.S. only. Corpscon, Version 6.0, will also calculate geoid-ellipsoid separations based on the NGS program GeoidXX (XX = 90, 93, 96, 99, and 03). Geoid-ellipsoid separations can be calculated for the Continental U.S., Alaska, Hawaii and Puerto Rico/U.S. Virgin Islands."

Wednesday, December 28, 2005

Windows Installer Property Reference

Windows Installer Property Reference There are many properties that can be set during an installation. The following page list most of them with their functions and, in many cases, the syntax needed to put these properties into action. Many of the properties can be set through the IDE, while others are initialized by the Windows Installer service at run time.

Monday, December 26, 2005

Hotmail's new interface

Its called Live Mail, and I just love it. Its got a very intutive interface and its very similar to the interface of any old email client such as Outlook. The whole Live series of sites I think are very cool. The only site that I dont think is the best out there is their Local.Live (I think Google maps has a much better interface for viewing maps). Live mail is currently in its Beta. If you would like to be included in the beta, you can do so from this website http://imagine-msn.com/minisites/hotmail/default.aspx?locale=en-us. I am sure when it releases, its going to be a super hit! And can you imagine all the behavior you are seeing is basically through scripts in internet stuff such as DHTML, very cool indeed.

Wednesday, December 21, 2005

Delta3D - Open source gaming & simulation engine backed by the U.S. Military

Delta3D - Open source gaming & simulation engine backed by the U.S. Military A well-supported and fully-funded open source project, Delta3D is a full-function game engine appropriate for a wide variety of modeling & simulation applications. Delta3D's modular design integrates other well-known Open Source projects such as Open Scene Graph(OSG), Open Dynamics Engine(ODE), Character Animation Library (CAL3D), and OpenAL, integrating them in an easy-to-use API.

Tuesday, December 20, 2005

.Net Design Time Properties for Controls

Ever wanted to add custom properties for your control in .NET. I needed to and found these two classes that can be used to do many different things. UITypeEditor and TypeConverter. The TypeConverter: I had a property that took a string value. By default this property showed up in the designer. But what I wanted to do was constrain the user into selecting from a list of values. To do this I created a typeConvertor similar to the one shown below: 
public class PatternConvertor : TypeConverter {
  static System.ComponentModel.TypeConverter.StandardValuesCollection _svc;
  public PatternConvertor() {
    if (_svc == null) {
      ArrayList values;
      values = new ArrayList();
      values.Add("Value 1");
      values.Add("Value 2");
      _svc = new StandardValuesCollection(values);
    }
  }
  public override bool GetStandardValuesSupported(
      ITypeDescriptorContext context) {
    return true;
  }
  public override System.ComponentModel.TypeConverter.StandardValuesCollection
  GetStandardValues(ITypeDescriptorContext context) {
    return _svc;
  }
}

Next, I had to tell the property to use this Convertor. To do this I added the following attribute: [TypeConverter(typeof(PatternConvertor)) ], to the property

[TypeConverter(typeof(PatternConvertor))]
public string MyValue {
  get { return _value; }
  set { _value = value; }
}

When you click on the control in the designer and view its properties, MyValue will show in the list. MyValue will have a list of values that you can then set. The TypeConvertor class has many more goodies that can be used, such as GetPropertiesSupported, etc. Be sure to check out their functionality. The UITypeEditor: Want to show a custom way of setting a property.... You can do that using a UITypeEditor customized class. In this example I use a trackbar to set a property that takes an integer value. 
public class TrackBarEditor : UITypeEditor {
  public TrackBarEditor() {}
  public override UITypeEditorEditStyle
  GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) {
    return UITypeEditorEditStyle.DropDown;
  }
  public override object EditValue(
      System.ComponentModel.ITypeDescriptorContext context,
      IServiceProvider provider,
      object value) {  // use IWindowsFormsEditorService object to display a
                       // control in the dropdown area
                       // IWindowsFormsEditorService frmsvr =
                       // (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                       // if(frmsvr == null) return null; TrackBar tbr = new
                       // TrackBar(); tbr.Orientation = Orientation.Vertical;
                       // tbr.Size = new Size(60,120); tbr.TickFrequency = 50;
                       // tbr.SetRange(10,300); frmsvr.DropDownControl(tbr);
                       // return tbr.Value; } }
 Now to use it add the following attribute above the property: 
[Editor(typeof(TrackBarEditor),typeof(UITypeEditor))] 

 Now when you go to the designer, it will drop down a trackbar, which you can slide around to set the value for the property. Cool, huh?! Other useful attributes: RefreshProperties: How should a refresh be performed Description: description shown for the property in the designer Browsable: Should the property show up in the designer DefaultValue: the default value for the property 

Other useful sites for this kind of stuff: Building Windows Forms Controls and Components with Rich Design-Time Features http://msdn.microsoft.com/msdnmag/issues/03/04/Design-TimeControls/default.aspx Building Windows Forms Controls and Components with Rich Design-Time Features, Part 2 http://msdn.microsoft.com/msdnmag/issues/03/05/Design-TimeControls/default.aspx http://www.awprofessional.com/isapi/product_id~%7BB757E215-0FB4-4EA1-A051-65EE537AE9C7%7D/selectDescTypeId~%7B5DBF6018-1234-4204-B35E-903EC67C9A3B%7D/st~%7B5130B593-BAEC-49C6-B6A8-0035DFD1EA3B%7D/content/images/0321116208/samplechapter/sellsch09.pdf

.Net Design Time Properties for Controls

Ever wanted to add custom properties for your control in .NET. I needed to and found these two classes that can be used to do many different things. UITypeEditor and TypeConverter. The TypeConverter: I had a property that took a string value. By default this property showed up in the designer. But what I wanted to do was constrain the user into selecting from a list of values. To do this I created a typeConvertor similar to the one shown below: public class PatternConvertor : TypeConverter { static System.ComponentModel.TypeConverter.StandardValuesCollection _svc; public PatternConvertor() { if (_svc == null) { ArrayList values; values = new ArrayList(); values.Add("Value 1"); values.Add("Value 2"); _svc = new StandardValuesCollection(values); } } public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return _svc; } } Next, I had to tell the property to use this Convertor. To do this I added the following attribute: [TypeConverter(typeof(PatternConvertor)) ], to the property [TypeConverter(typeof(PatternConvertor)) ] public string MyValue { get { return _value;} set {_value = value;} } When you click on the control in the designer and view its properties, MyValue will show in the list. MyValue will have a list of values that you can then set. The TypeConvertor class has many more goodies that can be used, such as GetPropertiesSupported, etc. Be sure to check out their functionality. The UITypeEditor: Want to show a custom way of setting a property.... You can do that using a UITypeEditor customized class. In this example I use a trackbar to set a property that takes an integer value. public class TrackBarEditor : UITypeEditor { public TrackBarEditor() {} public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) { return UITypeEditorEditStyle.DropDown; } public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value) { //use IWindowsFormsEditorService object to display a control in the dropdown area IWindowsFormsEditorService frmsvr = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if(frmsvr == null) return null; TrackBar tbr = new TrackBar(); tbr.Orientation = Orientation.Vertical; tbr.Size = new Size(60,120); tbr.TickFrequency = 50; tbr.SetRange(10,300); frmsvr.DropDownControl(tbr); return tbr.Value; } } Now to use it add the following attribute above the property: [Editor(typeof(TrackBarEditor),typeof(UITypeEditor))] Now when you go to the designer, it will drop down a trackbar, which you can slide around to set the value for the property. Cool, huh?! Other useful attributes: RefreshProperties: How should a refresh be performed Description: description shown for the property in the designer Browsable: Should the property show up in the designer DefaultValue: the default value for the property Other useful sites for this kind of stuff: Building Windows Forms Controls and Components with Rich Design-Time Features http://msdn.microsoft.com/msdnmag/issues/03/04/Design-TimeControls/default.aspx Building Windows Forms Controls and Components with Rich Design-Time Features, Part 2 http://msdn.microsoft.com/msdnmag/issues/03/05/Design-TimeControls/default.aspx http://www.awprofessional.com/isapi/product_id~%7BB757E215-0FB4-4EA1-A051-65EE537AE9C7%7D/selectDescTypeId~%7B5DBF6018-1234-4204-B35E-903EC67C9A3B%7D/st~%7B5130B593-BAEC-49C6-B6A8-0035DFD1EA3B%7D/content/images/0321116208/samplechapter/sellsch09.pdf