Jaa


Checklist: Managed Code Performance

 

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

patterns & practices Developer Center

Improving .NET Application Performance and Scalability

J.D. Meier, Srinath Vasireddy, Ashish Babbar, and Alex Mackman
Microsoft Corporation

May 2004

Related Links

Home Page for Improving .NET Application Performance and Scalability

Send feedback to Scale@microsoft.com

patterns & practices Library

How to Use This Checklist

This checklist is a companion to Chapter 5, "Improving Managed Code Performance"

Design Considerations

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Design for efficient resource management.
Ff647717.checkbox(en-us,PandP.10).gif Reduce boundary crossings.
Ff647717.checkbox(en-us,PandP.10).gif Prefer single large assemblies rather than multiple smaller assemblies.
Ff647717.checkbox(en-us,PandP.10).gif Factor code by logical layers.
Ff647717.checkbox(en-us,PandP.10).gif Treat threads as a shared resource.
Ff647717.checkbox(en-us,PandP.10).gif Design for efficient exception management.

Class Design Considerations

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Do not make classes thread safe by default.
Ff647717.checkbox(en-us,PandP.10).gif Consider using the sealed keyword.
Ff647717.checkbox(en-us,PandP.10).gif Consider the tradeoffs of using virtual members.
Ff647717.checkbox(en-us,PandP.10).gif Consider using overloaded methods.
Ff647717.checkbox(en-us,PandP.10).gif Consider overriding the Equals method for value types.
Ff647717.checkbox(en-us,PandP.10).gif Know the cost of accessing a property.
Ff647717.checkbox(en-us,PandP.10).gif Consider private versus public member variables.
Ff647717.checkbox(en-us,PandP.10).gif Limit the use of volatile fields.

Garbage Collection Guidelines

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Identify and analyze your application's allocation profile.
Ff647717.checkbox(en-us,PandP.10).gif Avoid calling GC.Collect.
Ff647717.checkbox(en-us,PandP.10).gif Consider weak references with cached data.
Ff647717.checkbox(en-us,PandP.10).gif Prevent the promotion of short-lived objects.
Ff647717.checkbox(en-us,PandP.10).gif Set unneeded member variables to Null before making long-running calls.
Ff647717.checkbox(en-us,PandP.10).gif Minimize hidden allocations.
Ff647717.checkbox(en-us,PandP.10).gif Avoid or minimize complex object graphs.
Ff647717.checkbox(en-us,PandP.10).gif Avoid preallocating and chunking memory.

Finalize and Dispose

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Call Close or Dispose on objects that support it.
Ff647717.checkbox(en-us,PandP.10).gif Use the using statement in Microsoft® C# and Try/Finally blocks in Microsoft Visual Basic®.NET to ensure Dispose is called.
Ff647717.checkbox(en-us,PandP.10).gif Do not implement Finalize unless required.
Ff647717.checkbox(en-us,PandP.10).gif Implement Finalize only if you hold unmanaged resources across client calls.
Ff647717.checkbox(en-us,PandP.10).gif Move the finalization burden to the leaves of object graphs.
Ff647717.checkbox(en-us,PandP.10).gif If you implement Finalize, implement IDisposable.
Ff647717.checkbox(en-us,PandP.10).gif If you implement Finalize and Dispose, use the Dispose pattern.
Ff647717.checkbox(en-us,PandP.10).gif Suppress finalization in your Dispose method.
Ff647717.checkbox(en-us,PandP.10).gif Allow Dispose to be called multiple times.
Ff647717.checkbox(en-us,PandP.10).gif Call Dispose on base classes and on IDisposable members.
Ff647717.checkbox(en-us,PandP.10).gif Keep finalizer code simple to prevent blocking.
Ff647717.checkbox(en-us,PandP.10).gif Provide thread-safe cleanup code only if your type is thread-safe.

Pinning

Check Description
Ff647717.checkbox(en-us,PandP.10).gif If you need to pin buffers, allocate them at startup.

Threading

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Minimize thread creation.
Ff647717.checkbox(en-us,PandP.10).gif Use the thread pool when you need threads.
Ff647717.checkbox(en-us,PandP.10).gif Use a Timer to schedule periodic tasks.
Ff647717.checkbox(en-us,PandP.10).gif Consider parallel versus synchronous tasks.
Ff647717.checkbox(en-us,PandP.10).gif Do not use Thread.Abort to terminate other threads.
Ff647717.checkbox(en-us,PandP.10).gif Do not use Thread.Suspend and Thread.Resume to pause threads.

Asynchronous Calls

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Consider client-side asynchronous calls for UI responsiveness.
Ff647717.checkbox(en-us,PandP.10).gif Use asynchronous methods on the server for I/O bound operations.
Ff647717.checkbox(en-us,PandP.10).gif Avoid asynchronous calls that do not add parallelism.

Locking and Synchronization

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Determine if you need synchronization.
Ff647717.checkbox(en-us,PandP.10).gif Determine the approach.
Ff647717.checkbox(en-us,PandP.10).gif Determine the scope of your approach.
Ff647717.checkbox(en-us,PandP.10).gif Acquire locks late and release them early.
Ff647717.checkbox(en-us,PandP.10).gif Avoid locking and synchronization unless required.
Ff647717.checkbox(en-us,PandP.10).gif Use granular locks to reduce contention.
Ff647717.checkbox(en-us,PandP.10).gif Avoid excessive fine-grained locks.
Ff647717.checkbox(en-us,PandP.10).gif Avoid making thread safety the default for your type.
Ff647717.checkbox(en-us,PandP.10).gif Use the fine grained lock (C#) statement instead of Synchronized.
Ff647717.checkbox(en-us,PandP.10).gif Avoid locking "this".
Ff647717.checkbox(en-us,PandP.10).gif Coordinate multiple readers and single writers by using ReaderWriterLock instead of lock.
Ff647717.checkbox(en-us,PandP.10).gif Do not lock the type of the objects to provide synchronized access.

Boxing and Unboxing

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Avoid frequent boxing and unboxing overhead.
Ff647717.checkbox(en-us,PandP.10).gif Measure boxing overhead.
Ff647717.checkbox(en-us,PandP.10).gif Use DirectCast in your Visual Basic .NET code.

Exception Management

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Do not use exceptions to control application flow.
Ff647717.checkbox(en-us,PandP.10).gif Use validation code to avoid unnecessary exceptions.
Ff647717.checkbox(en-us,PandP.10).gif Use the finally block to ensure resources are released.
Ff647717.checkbox(en-us,PandP.10).gif Replace Visual Basic .NET On Error Goto code with exception handling.
Ff647717.checkbox(en-us,PandP.10).gif Do not catch exceptions that you cannot handle.
Ff647717.checkbox(en-us,PandP.10).gif Be aware that rethrowing is expensive.
Ff647717.checkbox(en-us,PandP.10).gif Preserve as much diagnostic information as possible in your exception handlers.
Ff647717.checkbox(en-us,PandP.10).gif Use performance monitor to monitor common language runtime (CLR) exceptions.

Iterating and Looping

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Avoid repetitive field or property access.
Ff647717.checkbox(en-us,PandP.10).gif Optimize or avoid expensive operations within loops.
Ff647717.checkbox(en-us,PandP.10).gif Copy frequently called code into the loop.
Ff647717.checkbox(en-us,PandP.10).gif Consider replacing recursion with looping.
Ff647717.checkbox(en-us,PandP.10).gif Use for instead of foreach in performance-critical code paths.

String Operations

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Avoid inefficient string concatenation.
Ff647717.checkbox(en-us,PandP.10).gif Use + when the number of appends is known.
Ff647717.checkbox(en-us,PandP.10).gif Use StringBuilder when the number of appends is unknown.
Ff647717.checkbox(en-us,PandP.10).gif Treat StringBuilder as an accumulator.
Ff647717.checkbox(en-us,PandP.10).gif Use the overloaded Compare method for case-insensitive string comparisons.

Arrays

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Prefer arrays to collections unless you need functionality.
Ff647717.checkbox(en-us,PandP.10).gif Use strongly typed arrays.
Ff647717.checkbox(en-us,PandP.10).gif Use jagged arrays instead of multidimensional arrays.

Collections

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Analyze your requirements before choosing the collection type.
Ff647717.checkbox(en-us,PandP.10).gif Initialize collections to the right size when you can.
Ff647717.checkbox(en-us,PandP.10).gif Consider enumerating overhead.
Ff647717.checkbox(en-us,PandP.10).gif Prefer to implement IEnumerable with optimistic concurrency.
Ff647717.checkbox(en-us,PandP.10).gif Consider boxing overhead.
Ff647717.checkbox(en-us,PandP.10).gif Consider for instead of foreach.
Ff647717.checkbox(en-us,PandP.10).gif Implement strongly typed collections to prevent casting overhead.
Ff647717.checkbox(en-us,PandP.10).gif Ff647717.checkbox(en-us,PandP.10).gif

Reflection and Late Binding

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Prefer early binding and explicit types rather than reflection.
Ff647717.checkbox(en-us,PandP.10).gif Avoid late binding.
Ff647717.checkbox(en-us,PandP.10).gif Avoid using System.Object in performance critical code paths.
Ff647717.checkbox(en-us,PandP.10).gif Enable Option Explicit and Option Strict in Visual Basic.NET.

Code Access Security

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Consider SuppressUnmanagedCodeSecurity for performance-critical, trusted scenarios.
Ff647717.checkbox(en-us,PandP.10).gif Prefer declarative demands rather than imperative demands.
Ff647717.checkbox(en-us,PandP.10).gif Consider using link demands rather than full demands for performance - critical, trusted scenarios.

Working Set Considerations

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Load only the assemblies you need.
Ff647717.checkbox(en-us,PandP.10).gif Consider assemblies that are being loaded as side effects.
Ff647717.checkbox(en-us,PandP.10).gif Reduce the number of application domains, and/or make assemblies shared assemblies.
Ff647717.checkbox(en-us,PandP.10).gif Reduce the number of threads.

Native Image Generator (Ngen.exe)

Check Description
Ff647717.checkbox(en-us,PandP.10).gif Scenarios where startup time is paramount should consider Ngen.exe for their startup path.
Ff647717.checkbox(en-us,PandP.10).gif Scenarios that will benefit from the ability to share assemblies should adopt Ngen.exe.
Ff647717.checkbox(en-us,PandP.10).gif Scenarios with limited or no sharing should not use Ngen.exe.
Ff647717.checkbox(en-us,PandP.10).gif Do not use Ngen.exe for ASP.NET version 1.0 and 1.1.
Ff647717.checkbox(en-us,PandP.10).gif Consider Ngen.exe for ASP.NET version 2.0.
Ff647717.checkbox(en-us,PandP.10).gif Measure performance with and without Ngen.exe.
Ff647717.checkbox(en-us,PandP.10).gif Regenerate your image when you ship new versions.
Ff647717.checkbox(en-us,PandP.10).gif Choose an appropriate base address.
Ff647717.checkbox(en-us,PandP.10).gif Install assemblies in the GAC if you want to use NGEN to get the best performance.
Ff647717.checkbox(en-us,PandP.10).gif A setup application for an update of a library assembly should run the 'Ngen.exe update' command.

patterns & practices Developer Center

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

© Microsoft Corporation. All rights reserved.