where developers meet development
Monday,November 18,2019


Writing High Performance .NET Code

g) Don’t lock “this” and “type” of an object: Locking this pointer is a bad idea as this can have correctness issue as it is visible. Similarly, locking type of an object is a bad idea as these objects are the same across application domains and so thus we lock all instances of objects across app domains in a process.

lock (this) {
    do something;

public class foo {
    Object sync_obj = new Object();
    lock(sync_obj) {
        Do something

    Do something;

public class foo {
private static Object sync_obj = new Object();
lock (sync_obj) {
    Do something;

h) Consider using [ThreadStatic] to eliminate or reduce lock contention: If you can have a data as part of thread local storage (per thread) rather than sharing and after the threads have completed the jobs, you can process the combined effect. Consider using this to reduce lock contention.

i) Acquire and release lock in the same order: Otherwise you can cause deadlock condition

lock(obj_A) {
lock(obj_B) {
  Do something;

lock(obj_B) {
  lock(obj_A) {
    Do something;

j) All collections in .NET are not thread safe. Some collection classes (ex: ArrayList) allow multiple readers concurrently. Need to call “Synchronized” method for making it thread safe for updates

ArrayList myAr = new ArrayList();
ArrayList mySyncAr = ArrayList.Synchronized (myAr); //use mySyncAr

k) Enumerating through collections is also “not” thread safe even though it is synchronized. If another thread modifies the underlying collection then an exception will be thrown.

Automatic Memory Management (Garbage Collection)

Automatic memory management, aka GC is one of the most important features provided by .NET Framework. GC manages the allocation and reclaiming of memory in your application. When ever you call “new” to create a new object, GC will allocate memory from managed heap as long as space is available and once it runs out of memory it triggers collection, reclaim memory so that it can start allocating again. We will go into some detail about GC algorithms, how they work, different GC flavors, and how you can write a GC friendly code.

.NET GC is a generational and mark and compact algorithm. We have 3 generations (Gen0, 1 and 2). .NET GC assumes that most of the objects you create die young, so only a part of your entire manage help can be collected (which is much faster) than collecting the entire manage heap. GC first marks the root objects (to find out those who are alive) and then compacts the heap (moving all live objects to a part of the heap which forms older generation(s). Always, allocations happen in Gen0 heap. The initial gen0 heap is some fraction of the last level cache. The idea is to have gen0 fit in the cache to avoid cache misses.

.NET GC Flavors:

  • Workstation GC (WKS)
  • Server GC (SVR)
Note: Selecting appropriate GC flavor is essential for optimal performance of your application.

Workstation (WKS) GC: WKS GC has 2 variants. Concurrent GC (on) which is the default and can be turned off. Concurrent GC (on) will have less pause time, increasing the UI responsiveness. GC stops the application threads for a shorter duration when absolutely necessary. If you have a throughput kind of application (console app non UI) then turning off concurrent GC might get you better performance. In your application configuration file (ex: foo.exe.config), you can add following [2]:

  <gcConcurrent enabled="false"/>

Register to DeveloperMarch

Be part of DeveloperMarch and get updates on upcoming events. Youcan register with the form below:

Job Title*
Phone Number*
Confirm Password*
E-mail address:*
Your Details:
Your name: *
E-mail address: *
Software Supportby Advanced Millennium Technologies

Advanced Millennium Technologies. Expertise in software development, offering consultancy services, Open source programming, CRM - Customer Relationship Management, CMS - Content Management System , ERP - Enterprise Resource Planning and Ecommerce development, AJAX, PHP, .NET, J2EE, SOA, XSLT, DOJO toolkit development and software testing. A robust onsite-offshore model. A well-defined global delivery model. AMT Outsourcing center. www.amt.inTAROBY - The E-Mail Dashboard for EntrepreneursTaroby is a SaaS based messaging and collaboration suite inbox that enables sharing of email accounts among team members. The unique concept of 'Team Inbox' makes Taroby an excellent enterprise collaboration suite for enterprises. Taroby is an effective tool for CEO's and entrepreneurs to manage multiple departments or manage multiple projects under them. The team inbox gives the entrepreneurs an overview of what is happening their business and give a quick snap shot of the employees who is responcible for handling the tasks/emails. For team members taroby brings in transparency and efficiency in their teams. Taroby improves the internal and external communication in an organization. Using the Taroby's Team Inbox also helps in reducing the usage of disc space and there by helping the enterprises to reduce carbon footprints.