Performance Tuning Tips

What is Performances Tuning?
Performance tuning is the improvement of system performance, performances tuning need when application is working fine for x number of user load but whenever user load in increase to x+y it is observed performances of application degraded, the degradation of performances in web application is measured in response time taken by the web server to respond any HTTP request. Other degradation parameters are CPU, Memory utilization ect.

Profiling:profiler is used to find root cause in database or in application code, profiler is the investigation of a program's behavior using information gathered as the program executes. Its goal is to determine which sections of a program to optimize.

Code Optimization: Avoid Excess Garbage Collection
  • The canonicalization techniques are one way to avoid garbage collection fewer objects means less to garbage-collect.
  • The pooling technique tends to reduce garbage-collection requirements, partly because you are creating fewer objects by reusing them, and partly because you deallocate memory less often by holding on to the objects you have allocated
  • Reducing garbage collection by using primitive data type . Hold an object in a primitive data-type format rather than another format as primitive data type use less memory.

Lazy Initialization
  • of delaying object creation until the last possible moment. This technique is useful for voiding unnecessary object creation when only a few objects are used although many possible objects can be created.
  • objects that need to be created and initialized, and most of these objects will be used, but not immediately. In this case, it can be useful to spread out the load of object initialization so you don't get one large hit on the application. It may be better to let a background thread initialize all the objects slowly or to use lazy initialization to take many small or negligible hits, thus spreading the load over time.

Excess Object Creation
  • need to be created before they can be used, and garbage-collected when they are finished with.
  • more objects you use, the more garbage-cycling happens, the CPU cycle wasted.
  • object creation is roughly as expensive as a malloc in C, or a new in C++, and there is no easy way of creating many objects together, so you cannot take advantage of efficiencies you get using bulk allocation.

Reuse Object
  • Objects are expensive to create so it is reasonable to reuse the same object.
  • This requires awareness of when not to create new object.
  • Look at the object and consider whether it is possible to reset the fields and then reuse the object, rather than throw it away and create another
  • Important for objects that are constantly used and discarded: for example, in graphics processing, objects such as Rectangles, Points, Colors, and Fonts are used and discarded all the time. Recycling these types of objects can certainly improve performance.
Key Point
  • Reduce the number of temporary objects being used, especially in loops.
  • Avoid creating temporary objects within frequently called methods.
  • Presize collection objects.
  • Reuse objects where possible.
  • Empty collection objects before reusing them. (Do not shrink them unless they are very large.)
  • Use custom conversion methods for converting between data types (especially strings and streams) to reduce the number of temporary objects.
  • Define methods that accept reusable objects to be filled in with data, rather than methods that return objects holding that data. (Or you can return immutable objects.) Canonicalize objects wherever possible. Compare canonicalized objects by identity.
  • Use primitive data types instead of objects as instance variables.
  • Preallocate storage for large collections of objects by mapping the instance variables into multiple arrays.
  • Eliminate object-creation bottlenecks by moving object creation to an alternative time.
  • Create objects early, when there is spare time in the application, and hold those objects until required.
  • Use lazy initialization when there are objects or variables that may never be used, or when you need to distribute the load of creating objects.