Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, July 8, 2017

Performance And Memory Tips for An Application



Performance and memory utilization play very important role for success of any application.we should be very careful for the application’s footprint and performance during development of application.
I have shared some common and important rules that will be very useful for developer.

  • Don't use ToUpper () to do a comparison of a case sensitive string.When you "convert a string to upper case" you create a second temporary string object. So, use:                           String.Equals(stringA, stringB, StringComparison.CurrentCultureIgnoreCase)
  • MyClass myObject = (MyClass) obj;                                           MyClass myObject = obj as MyClass;                                              The second will return null if obj isn't a MyClass, rather than throw a class InvalidCastException exception. 
  • Use "throw;" instead of "throw ex;" to preserve stack trace  - If re-throw an exception without adding additional information, use "throw" instead of "throw ex". An empty "throw" statement in a catch block will emit a specific IL that re-throws the exception while preserving the original stack trace. "throw ex" loses the stack trace to the original source of the exception.
  • Always implement the "Finally" block.
  • Create object only when it is really required.
  • The more objects, the more poor the performance.
  • Grab resources, use them, and release at the earliest time.
  • Avoid unnecessary boxing and unboxing.
  • Prefer lazy loading.
  • Don't hard-code values
  • Use generics where possible since it's typesafe & avoids boxing & unboxing
  • Use an error handler where it's absolutely needed
  • Dispose, dispose, dispose. CLR wound't know how to close your database connections, so close them after use and dispose of unmanaged resources
  • Use Static variables cautiously as they will stay live throughout the application's life.
  • Avoid using IDisposable everywhere.
  • Throw fewer exceptions. Avoid using exceptions to control the program flow. Never catch exceptions that you cannot handle.
  • Use Performance Monitor to check the exception count and other relevant information.
  • Prefer value types; i.e., If structure can work, then why take class?
  • Prefer AddRange() over Add() for adding multiple items to collection.
  • Trim your working set. Use and load the minimal and only the required number of assemblies. Prefer single huge assembly rather than using multiple small assemblies.
  • Prefer thread pool rather than creating a new thread for each request.
  • Use "For" loop for string iterations rather than "ForEach" iterator.
  • Use StringBuilder for string manipulation.
  • Prefer early binding.
  • Be careful while choosing .NET collections as each collection is designed for specific purpose.
  • Use StringCollection class to store strings.
  • Use Hashtable when frequent query is required on large number of records.
  • Prefer ListDictionary as it is faster than HashTable for <10 records.
  • For small data, go for SortedList. For large data, go for ArrayList and then call Sort method on it.
  • Prefer arrays over collections unless you need some special functionality as they use contiguous memory arrangement and are faster.
  • Avoid calling GC.Collect method because it traverses all the generations. If you have to call GC.Collect in your particular niche case, then make sure to clean finalized object also using GC.WaitForPendingFinalizers() and again call GC.Collect. This will collect all the dead objects.
  • Be cautious while using Thread.Suspend, as it may lead to deadlock due to incorrect synchronization.
  • Prefer Using statement to ensure that Dispose is called.
  • Trailing Comments Trialing comments are generally written on the same line as code with some tabs to separate them. These are short comments generally used to add information about variables, object declarations and so on.                                                                        Example long controlcounts; // Count of the controls in current context
  •  object initializers: below 3 line of code can be written in 1 line                                                                                                                          Employee emp = new Employee();                                       emp.Name = "John Smith";                                                           emp.StartDate = DateTime.Now(); becomes                               Employee emp = new Employee {Name="John Smith", StartDate=DateTime.Now()}

No comments :

Post a Comment