Este conteúdo não está disponível em seu idioma, mas aqui está a versão em inglês.

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

Ff647717.checkbox(en-us,PandP.10).gifDesign for efficient resource management.
Ff647717.checkbox(en-us,PandP.10).gifReduce boundary crossings.
Ff647717.checkbox(en-us,PandP.10).gifPrefer single large assemblies rather than multiple smaller assemblies.
Ff647717.checkbox(en-us,PandP.10).gifFactor code by logical layers.
Ff647717.checkbox(en-us,PandP.10).gifTreat threads as a shared resource.
Ff647717.checkbox(en-us,PandP.10).gifDesign for efficient exception management.

Class Design Considerations

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

Garbage Collection Guidelines

Ff647717.checkbox(en-us,PandP.10).gifIdentify and analyze your application's allocation profile.
Ff647717.checkbox(en-us,PandP.10).gifAvoid calling GC.Collect.
Ff647717.checkbox(en-us,PandP.10).gifConsider weak references with cached data.
Ff647717.checkbox(en-us,PandP.10).gifPrevent the promotion of short-lived objects.
Ff647717.checkbox(en-us,PandP.10).gifSet unneeded member variables to Null before making long-running calls.
Ff647717.checkbox(en-us,PandP.10).gifMinimize hidden allocations.
Ff647717.checkbox(en-us,PandP.10).gifAvoid or minimize complex object graphs.
Ff647717.checkbox(en-us,PandP.10).gifAvoid preallocating and chunking memory.

Finalize and Dispose

Ff647717.checkbox(en-us,PandP.10).gifCall Close or Dispose on objects that support it.
Ff647717.checkbox(en-us,PandP.10).gifUse 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).gifDo not implement Finalize unless required.
Ff647717.checkbox(en-us,PandP.10).gifImplement Finalize only if you hold unmanaged resources across client calls.
Ff647717.checkbox(en-us,PandP.10).gifMove the finalization burden to the leaves of object graphs.
Ff647717.checkbox(en-us,PandP.10).gifIf you implement Finalize, implement IDisposable.
Ff647717.checkbox(en-us,PandP.10).gifIf you implement Finalize and Dispose, use the Dispose pattern.
Ff647717.checkbox(en-us,PandP.10).gifSuppress finalization in your Dispose method.
Ff647717.checkbox(en-us,PandP.10).gifAllow Dispose to be called multiple times.
Ff647717.checkbox(en-us,PandP.10).gifCall Dispose on base classes and on IDisposable members.
Ff647717.checkbox(en-us,PandP.10).gifKeep finalizer code simple to prevent blocking.
Ff647717.checkbox(en-us,PandP.10).gifProvide thread-safe cleanup code only if your type is thread-safe.


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


Ff647717.checkbox(en-us,PandP.10).gifMinimize thread creation.
Ff647717.checkbox(en-us,PandP.10).gifUse the thread pool when you need threads.
Ff647717.checkbox(en-us,PandP.10).gifUse a Timer to schedule periodic tasks.
Ff647717.checkbox(en-us,PandP.10).gifConsider parallel versus synchronous tasks.
Ff647717.checkbox(en-us,PandP.10).gifDo not use Thread.Abort to terminate other threads.
Ff647717.checkbox(en-us,PandP.10).gifDo not use Thread.Suspend and Thread.Resume to pause threads.

Asynchronous Calls

Ff647717.checkbox(en-us,PandP.10).gifConsider client-side asynchronous calls for UI responsiveness.
Ff647717.checkbox(en-us,PandP.10).gifUse asynchronous methods on the server for I/O bound operations.
Ff647717.checkbox(en-us,PandP.10).gifAvoid asynchronous calls that do not add parallelism.

Locking and Synchronization

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

Boxing and Unboxing

Ff647717.checkbox(en-us,PandP.10).gifAvoid frequent boxing and unboxing overhead.
Ff647717.checkbox(en-us,PandP.10).gifMeasure boxing overhead.
Ff647717.checkbox(en-us,PandP.10).gifUse DirectCast in your Visual Basic .NET code.

Exception Management

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

Iterating and Looping

Ff647717.checkbox(en-us,PandP.10).gifAvoid repetitive field or property access.
Ff647717.checkbox(en-us,PandP.10).gifOptimize or avoid expensive operations within loops.
Ff647717.checkbox(en-us,PandP.10).gifCopy frequently called code into the loop.
Ff647717.checkbox(en-us,PandP.10).gifConsider replacing recursion with looping.
Ff647717.checkbox(en-us,PandP.10).gifUse for instead of foreach in performance-critical code paths.

String Operations

Ff647717.checkbox(en-us,PandP.10).gifAvoid inefficient string concatenation.
Ff647717.checkbox(en-us,PandP.10).gifUse + when the number of appends is known.
Ff647717.checkbox(en-us,PandP.10).gifUse StringBuilder when the number of appends is unknown.
Ff647717.checkbox(en-us,PandP.10).gifTreat StringBuilder as an accumulator.
Ff647717.checkbox(en-us,PandP.10).gifUse the overloaded Compare method for case-insensitive string comparisons.


Ff647717.checkbox(en-us,PandP.10).gifPrefer arrays to collections unless you need functionality.
Ff647717.checkbox(en-us,PandP.10).gifUse strongly typed arrays.
Ff647717.checkbox(en-us,PandP.10).gifUse jagged arrays instead of multidimensional arrays.


Ff647717.checkbox(en-us,PandP.10).gifAnalyze your requirements before choosing the collection type.
Ff647717.checkbox(en-us,PandP.10).gifInitialize collections to the right size when you can.
Ff647717.checkbox(en-us,PandP.10).gifConsider enumerating overhead.
Ff647717.checkbox(en-us,PandP.10).gifPrefer to implement IEnumerable with optimistic concurrency.
Ff647717.checkbox(en-us,PandP.10).gifConsider boxing overhead.
Ff647717.checkbox(en-us,PandP.10).gifConsider for instead of foreach.
Ff647717.checkbox(en-us,PandP.10).gifImplement strongly typed collections to prevent casting overhead.

Reflection and Late Binding

Ff647717.checkbox(en-us,PandP.10).gifPrefer early binding and explicit types rather than reflection.
Ff647717.checkbox(en-us,PandP.10).gifAvoid late binding.
Ff647717.checkbox(en-us,PandP.10).gifAvoid using System.Object in performance critical code paths.
Ff647717.checkbox(en-us,PandP.10).gifEnable Option Explicit and Option Strict in Visual Basic.NET.

Code Access Security

Ff647717.checkbox(en-us,PandP.10).gifConsider SuppressUnmanagedCodeSecurity for performance-critical, trusted scenarios.
Ff647717.checkbox(en-us,PandP.10).gifPrefer declarative demands rather than imperative demands.
Ff647717.checkbox(en-us,PandP.10).gifConsider using link demands rather than full demands for performance - critical, trusted scenarios.

Working Set Considerations

Ff647717.checkbox(en-us,PandP.10).gifLoad only the assemblies you need.
Ff647717.checkbox(en-us,PandP.10).gifConsider assemblies that are being loaded as side effects.
Ff647717.checkbox(en-us,PandP.10).gifReduce the number of application domains, and/or make assemblies shared assemblies.
Ff647717.checkbox(en-us,PandP.10).gifReduce the number of threads.

Native Image Generator (Ngen.exe)

Ff647717.checkbox(en-us,PandP.10).gifScenarios where startup time is paramount should consider Ngen.exe for their startup path.
Ff647717.checkbox(en-us,PandP.10).gifScenarios that will benefit from the ability to share assemblies should adopt Ngen.exe.
Ff647717.checkbox(en-us,PandP.10).gifScenarios with limited or no sharing should not use Ngen.exe.
Ff647717.checkbox(en-us,PandP.10).gifDo not use Ngen.exe for ASP.NET version 1.0 and 1.1.
Ff647717.checkbox(en-us,PandP.10).gifConsider Ngen.exe for ASP.NET version 2.0.
Ff647717.checkbox(en-us,PandP.10).gifMeasure performance with and without Ngen.exe.
Ff647717.checkbox(en-us,PandP.10).gifRegenerate your image when you ship new versions.
Ff647717.checkbox(en-us,PandP.10).gifChoose an appropriate base address.
Ff647717.checkbox(en-us,PandP.10).gifInstall assemblies in the GAC if you want to use NGEN to get the best performance.
Ff647717.checkbox(en-us,PandP.10).gifA 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.