[Silverlight getting started series] Silverlight performance best practices

Source: Internet
Author: User
Tags visual studio 2010

How to make your Silverlight ApplicationProgramHigh performance, fast and smooth operation? From the very beginning, we should know what practices will lead to poor performance of your Silverlight application? Which of the following best practices can improve the performance of our Silverlight application?

Silverlight performance-related Article Recommendation

These articles on Silverlight performance and best practices are worth reading:

    • Msdn: Silverlight performance tips (recommended)
    • Msdn magazine: Silverlight Best Practices
    • Msdn blog: Silverlight performance best practices (recommended)
    • Silverlight best practices for Quick Start Performance
    • Silverlight performance issue experience
Silverlight performance best practices

I will also summarize some practical performance experience here. I will continue to update this article. Please note. The following Silverlight performance programming tips are from msdn blog: Silverlight performance best practices (recommended):

Improve startup time:
  • simplify your XAML

    • reduce element count. Inspect your tree and know your tree element count using tool such as Silverlight spy.
    • reduce the # of properties in your XAML. if your XAML is generated
      by blend for example, remove all the properties that are the default.
      This shoshould improve XAML parsing time.
    • remove "Dead XAML" that may left beyond and is no longer needed.
    • Use templates over usercontrols. usercontrol re-parsed per
      instantiation, templates parsed only once (we have not discussed this
      but it is in our blog)
  • minimize download size
    • breakup large xaps to smaller ones

      • dynamically load modules not needed at app startup
    • re-zip your xap using a more efficient compression (e.g. 7-zip which is open source)
    • postpone initializations, web access, etc.
      • defer objects initialization until it is really needed (system. Lazy makes this easy)
    • do not wait on network I/O during startup
    • avoid loading data not needed at startup
    • use a splash screen to improves perceived performance
  • Minimize disk I/O
    • Majority of coldstart time is disk Io.

      • Make network download, disk Io & CPU/JIT as concurrent as possible
    • Load fewer assemblies at startup
    • Beware of JIT in-lining. Move code that you know is not needed at startup to its own method and decorate it with "noinlining ".
      Example: Since
      DataGrid is not created during startup it shoshould be moved to its own
      Method. This will prevent system. Windows. Controls. Data. dll from getting
      Loaded at startup!

  Improve run-time perf
  • reduce what is updated to screen

    • Silverlight will draw only what is "dirty", you can help by:
    • avoid or reduce animations of large events als
    • turn off hidden animations
    • beware of controls that animate by default such as progress bar
  • visualize what is updated to screen
    • set flags during debug

      • enable redrawregions to see what's updating

        • enableredrawregions " value = "true"/>
        • in code: application. Current. Host. settings. enableredrawregions = true
      • enable frameratecounter to verify your animations frame-rate is not slowing
        • enableframeratecounter " value = "true"/>
        • on lower/left status bar And on top-left if enablegpuacceleration enabled
  • use effects carefully
    • designers love to use them, but be cautious
    • effects have CPU & memory impact: Causes allocation of additional intermediate surfaces
    • effects are rendered in Software
      • any object that applies an effect will also be rendered in software
    • avoid triggering re-Rasterization
      • do not animate properties of effects
      • do not apply effects to elements that frequently change (e. g. Animating color)
    • apply to smaller areas
      • be especially watchful when effects are applied on a large container
      • verify container has no child elements that are updating.
        Any small animation (or even blinking caret in a textbox) will cause constant redraw!
      • Read more in this blog: understanding the impact of effects on performance
  • Understands when to use the GPU.
    • Elements or trees of elements can be bitmap cached (akaCachedcomposition) And will get accelerated via GPU

      • Cached bitmap lives on GPU memory
    • Significant perf gain for blending and transforms scenarios (e.g. animate opacity)
    • Properties that can be GPU accelerated:
      • Render transforms
      • Opacity
      • Rectangular clip
      • Visibility
      • Projections *
    • Falls into software rendering when using:
      • Effects (e.g. dropshadoweffect), opacity mask, non-rectangular clips
    • You can control the size (quality) of the cached Bitmap UsingRenderatscale.Understand TheAreMemory/perf vs. Quality tradeoff
      • Maybe useful when you drag/scroll your diagrams
  • Simplify your tree
    • Keep your trees smaller and flatter trees.

      • Less memory consumption (less allocations, GC, paging, etc .)
      • Events that traverse smaller/flatter trees are much faster
      • Less blending by the graphics pipeline
    • Know your element tree count
    • Use templates over usercontrols. usercontrol re-parsed per
      Instantiation, templates parsed only once (we have not discussed this
      But it is in our blog)
    • Use Silverlight spy to inspect your tree
    • Remove sub-trees no longer used (LOGIN & welcome dialogs often left unused ).
      If you must, use visibility = "Collapsed" over opacity = "0"
      Collapsed elements not blended, don't participate in input events, etc bt they do consume memory.
    • Consider removing large sub-trees in chunks not to block the UI thread
    • Export Alize your canvas and lists. Remove or skimpily elements that are not legible when you zoom out.
      • Check Visual Studio 2010 ultimate dgml viewer perf (open these demos in vs10 ultimate)
        Vs2010 uses limits of the concepts that are discussed in codecanvas and zoomablecanvas series of blogs. Although the code samples are for WPF it shocould relative easy to port to Silverlight.
  • Avoid blocking the UI tread.
    • Use backgroundworker or dispatcher. begininvoke () for long running/blocking operations. (Sample in our Silverlight firestarter resource below)

Reduce memory usage
    • As mentioned earlier sampling the visual tree, keep element count low, Virtualize where possible.
    • Be aware of high resolution images
      Always
      Decode the image to desired size and not to the default size. You will
      Reduce not only your application's working set, but execution speed
      Well.
      WPF has decodepixelwidth however, Silverlight does not have
      This API, however you can work around und this by using writeablebitmap, see
      More in this blog: Silverlight's big image problem (and what you can do about it)
    • Use tools to understand your application memory
    • Avoid memory leaks

Useful Tools & Resources
  • resources most useful for diagnosing Startup Performance issues:

    • xperf-essential tool to inspect system activity like disk Io, CPU, network, CLR, etc.
    • how to enable ETW events in CLR, see: ETW events in the Common Language Runtime
    • fiddler-to inspect hhtp (s) traffic between your computer and the Internet
    • visual round trip analyzer-another really useful network analyzer.
    • Read our blog: Silverlight startup best practices
  • resources most useful for diagnosing memory issues:
    • vmmap-useful to get and ideas where your memory is being allocated in a high level
    • xperf-useful for native memory analysis
    • memory leaks & diagnostics:
      • Read about memory leaks here: Finding memory leaks in WPF-based applications. Although it discusses WPF the concepts are similar.
      • consider professional memory profiles like Scitech and ants
    • Read our blog: Analyzing Silverlight memory usage: Part 1-obtaining measurements
    • understand and follow CLR garbage collection best practices.
      • see more in Fundamentals of garbage collection and maoni's blogs discussing GC in details:

        • using GC efficiently-Part 1
        • using GC efficiently-Part 2
        • using GC efficiently-Part 3
        • using GC efficiently-Part 4
  • General tools and resources:
    • Visual Studio CPU profiler-essential tool to inspect where time is spent in your code.

      • How to profile SL in vs profiler: vs2010: Silverlight 4 profiling
    • Read our blog: Performance on Silverlight TV
    • watch our Silverlight firestarter presentation and download the sample
    • Read our general Silverlight performance tips on msdn
    • Silverlight spy to inspect your visual tree, monitor events, elements properties, etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.