In what scenarios does freezing WPF objects benefit performance greatly? In what scenarios does freezing WPF objects benefit performance greatly? wpf wpf

In what scenarios does freezing WPF objects benefit performance greatly?


You might be interested in my experiences with Freezable:

I once wrote a PDF viewer using muPdf which renders bitmaps, that I render with WPF. What helps performance greatly is that I can render the page bitmaps on a background thread, freeze them, and then pass them to the UI thread. It is nice that WPF does not copy the image to freeze it, but the ability to do all this preparation on a background thread was the key benefit for me.

From what I understand, all visuals need to be frozen so they can be safely rendered by the WPF render thread. If you render large unfrozen visuals, they will get cloned to frozen ones when WPF renders them. If you freeze your static bitmaps beforehand, WPF can just share the pointer with the render thread without cloning. Unfrozen objects may even get copied repeatedly if WPF is not aware wether the object is changed from the last time it was rendered. Frozen objects eliminate the need for all this copying.


These potential memory leaks could happen if you use the Image control (and not use Freeze method):

a) You use BitmapImage as the Image source and do not release the BitmapImage:

static BitmapImage bi1 = new BitmapImage(new Uri("Bitmap1.bmp",UriKind.RelativeOrAbsolute));m_Image1 = new Image();m_Image1.Source = bi1; //bi1.Freeze() //if you do not Freeze, your app will leak memory.MyStackPanel.Children.Add(m_Image1);

b) You assign multiple BitmapImage as the Image source and do not release all of the BitmapImage you used (similar to (a)). This one introduced in .Net 3.5:

static BitmapImage bi1 = new BitmapImage(new Uri("Bitmap1.bmp",UriKind.RelativeOrAbsolute));static BitmapImage bi2 = new BitmapImage(new Uri("Bitmap2.bmp",UriKind.RelativeOrAbsolute));bi2.Freeze();m_Image1 = new Image();//bi1.Freeze() //even though you are really using bi2 for Image Source, //you also need to Freeze bi1 it to avoid leak m_Image1.Source = bi1;  // use un-frozen bitmap, which causes the leakm_Image1.Source = bi2;  // use frozen bitmapMyStackPanel.Children.Add(m_Image1);

Source: WPF Performance


Though you have already accepted the answer, just wanted to log a different version of the answer that helped me better.

From MSDN (minor edit):

If you were to modify control holding reference to unmanaged low-level resources (eg: Brush), every modification would have to regenerated those low-level objects!

The freezable class is what gives a brush the abilityto find its corresponding generated, low-level objects and to updatethem when it changes. When this ability is enabled, the brush is saidto be "unfrozen."

A freezable's Freeze method enables you to disable this self-updatingability. You can use this method to make the brush become "frozen," orunmodifiable. Thus, improving performance.

And, the code to explain the usage:

            Button myButton = new Button();            SolidColorBrush myBrush = new SolidColorBrush(Colors.Yellow);            if (myBrush.CanFreeze)            {                // Makes the brush unmodifiable.                myBrush.Freeze();            }                            myButton.Background = myBrush;                    if (myBrush.IsFrozen) // Evaluates to true.            {                // If the brush is frozen, create a clone and modify the clone.                SolidColorBrush myBrushClone = myBrush.Clone();                myBrushClone.Color = Colors.Red;                myButton.Background = myBrushClone;            }            else            {                // If the brush is not frozen, it can be modified directly.                myBrush.Color = Colors.Red;            }