Friday, May 27, 2011

3.5 performance related enhancements

Garbage Collection:

 
Change1:
  • New overloaded method added in System.GC class
  • public static void Collect(int generation, GCCollectionMode mode);
  • Method can be used to adjust the behavior for a forced garbage collection.
  • There are 3 GCCollectionMode available: Default, Forced, Optimized
  • Forced is the default mode
  • Forced – it forces garbage collection to occur immediately.
  • Optimized – it allow garbage collection to determine whether current time is optimal to reclaim objects

using System;
class Program
{
static void Main(string[] args)
{
 GC.Collect(2, GCCollectionMode.Optimized);
}

 
//GCCollectionMode.Forced is the default mode
GCCollectionMode.Default 

 
//allows garbage collection to determine whether current time is optimal to reclaim the objects
GCCollectionMode.Optimized 

 
//forces garbage collection to occur immediately
GCCollectionMode.Forced

 

 
Change2:
  • System.Runtime.GCSettings
  • The GCSettings class has a new LatencyMode property
  • It allows to adjust time that garbage collection intrudes in application.
  • GCLatencyMode enumeration has 3 values: (Batch,Interactive,LowLatency)
 
//disable garbage collection concurrency and reclaims objects in a batch call
//this is most intrusive mode
GCSettings.LatencyMode = GCLatencyMode.Batch;

 
//this is default mode of gc
//enable garbage collection concurrency and reclaims objects while application is running
//less intrusive(disturbing)
GCSettings.LatencyMode = GCLatencyMode.Interactive;

 
//enable garbage collection that is more conservative in reclaims objects
//full collection occurs only if the system is under memory pressure
//least intrusive
GCSettings.LatencyMode = GCLatencyMode.LowLatency;

 

 

 High performance hashset

 
  • HashSet is the new generic collection added in 3.5 framework
  • System.Collections.Generic
  • HashSet provides high performance set operations to the .net framework.
  • You can do UnionWith/IntersectWith on the HashSet objects
  •  Set collection does not contain duplicate elements.
  •  Elements of the set are in no particular order
  •  HashSet object’s capacity automatically increases as elements are added to the object
 
Parallel Programming
  •  
  • ·         Parallel Programming is breaking a large problem into smaller ones
  • ·         Carrying the execution of each chunk simultaneously. 
  • ·         There can be different forms of parallel computing:
  • o    bit-level
  • o     instruction-level
  • o    Data
  • o    task-parallelism.
 
Of these two forms, the ones that are important in the context of Task Parallel Library are Data- Parallelism and Task-Parallelism.

 
Data Parallelism
In data parallelism, the focus is on distributing data across different parallel computing nodes. For a multiprocessor system  executing a single set of instructions, data parallelism can be achieved by making each processor perform the same task on  different pieces of distributed data. There can be a single thread controlling this, or can be multiple threads. However, the  processing of data would be the same for each thread.

 
Task Parallelism
Task parallelism is the execution of a different thread by each processor on the same or different data. The threads may  execute the same or different code. For a simple example for a two processor machine, if we want to do two tasks, it will be  CPU ‘a’ doing task ‘A’ and CPU ‘b’ doing task ‘B’.

 
Task parallelism emphasizes the distributed (parallelized) nature of the processing (i.e., threads), as opposed to the data  (data parallelism).

No comments: