.NET CLR optimization good practices


  • free your object as soon as possible (this will help to prevent the object promotion to into higher GC generation)
  • long lived object create early (this will help to avoid memory defragmentation by setting long lived (promoted to 1 and 2) object at bottom of CLR memory block)
  • avoid implementing finalizes in your class (so the object does not survive collection process)
  • avoid referencing a short lived objects from a long lived object ()
  • assign your reference to null (nothing) as soon as possible, especially before making a call to a long method
  • identify objects with a long lifespan and use CLR Profiler to analyze the reasons
  • avoid complicated object nesting and hierarchy (it make object graphing complex and expensive, as the GC walks all roots and builds a graph of reachable objects.)
  • use .NET Memory Performance Counters to identify possible problems in GC
  • use Performance Counter to monitor .NET counters:
  • if “% Time in GC” >50% it is too much
  • ideal ratio for these counters should be “# Gen 0 Collections” : “# Gen 1 Collections” : “# Gen 2 Collections” = 100: 10 : 1

JIT optimization (skipped if used NGEN)

  • do not set to null(nothing) all local variables (it is set to null by JIT to optimize GC)
  • enregistration – frequently called local variables stored in CPU registry instead of stack
  • pitching – if the system low on memory the native code is removed from memory
  • inlining – for methods smaller than 8-10 lines (<32b compiled) with no loops it is placed in caller body

Exception handling approach

  • use only when need try-catch block (it is expensive)
  • apply error handlers from top to bottom
  • do not use exceptions to control the program flow or for business exceptions only for exceptional situations
  • do not derive custom exception from Application Exception (adds unnecessary depth)
  • add automated exception logging inside the custom exceptions
  • exceptions is a class – use property for new message – do not parse
  • when rethrow use Throw nor Throw ThisException (so original exceptions context is saved) although it is a good practice to let CLR to propagate the exception
  • exceptions should be attributed with for remote calls or WS
  • use validations to avoid exceptions
  • avoid exceptions in loops

Performance settings and coding

  • set Remove Integer Overflow Check (project settings-compile) (checks overflow if for loop) saves 20% in integer-long operations
  • set Option Strict On (project settings-compile) to avoid implicit unboxing
  • avoid SPLIT function
  • use DirectCast not CType for references (for unboxing values, for casting variable of base class to variable of derived class)
  • use StringBuilder for string concatenations

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: