where developers meet development
Monday,November 18,2019

EXCLUSIVES 

Writing High Performance .NET Code

WKS GC has 1 heap per process and it has 1 GC thread per process. WKS GC is the default even on multiprocessor systems for any non ASP.NET application. ASP.NET automatically chooses SVR GC if you are on a multi processor system.

Server (SVR) GC: As the name suggests, SVR GC is optimized for server based applications (better scalability). It has 1 GC heap per Processor and 1 GC thread per 1 GC heap. For example, if you are on a 4 processor system, you will have 4 heaps and 4 GC threads operating on each of those heaps. A process can create objects in multiple heaps (for load balancing the allocation on heaps) and as mentioned above it is not the default. To enable Server GC, add the following in application configuration files.


<!--
<configuration> [2]
  <runtime>
    <gcServer enabled=“true"/>
  </runtime>
 </configuration>
-->


Tips for Selecting Appropriate GC

  • For all server throughput related applications, consider selecting Server GC
  • ASP.NET web applications on >1 proc machine automatically selects SVR GC. However if you want to run web garden scenario, then consider using WKS GC as the memory foot print might be really high as a result of multiple w3wp processes. SVR GC assumes it is the king and so will try to grab as many resources as possible. So if you have multiple processes running SVR GC, there can be degradation in performance and also an increase usage of system resources. In order to enable ASP.NET using WKS GC, add the following configuration to the Aspnet.config file. This is in the same directory as Aspnet_isapi.dll.
        <!--
     <configuration>
         <runtime>
            <gcServer enabled="false"/>
            <gcConcurrent enabled="false"/>
         </runtime>
     </configuration>
     -->
  • If you have a client – UI application which requires UI responsiveness, consider selecting WKS GC with Concurrent enabled
  • If you have Console application (no UI but throughput app) then consider turning off concurrent GC for better performance.
  • If you want lesser resource utilization in a system (memory etc) then consider using WKS GC.
Note: When you ask for Server GC on a UP machine, you get WKS GC with concurrent off. CLR assumes that since you are asking SVR GC, you are more interested in throughput than UI responsiveness and so automatically turn off concurrent GC.

Tips for Writing GC-Friendly Code

a. Never Call GC.Collect from your code: .NET GC is a dynamically tuning GC. At every collection it collects information such as survivor rate, and tunes its internal GC tuning parameters so the next GC is more effective than its previous GC. Unlike Java, .NET GC doesn’t expose many tuning parameters for the developer. So when you call GC.Collect in your code, it collects those parameters. Since you induced GC the next GC will not be as productive. Also, if GC.Collect is executing not just once but many times (lets say before you start expensive time consuming work and so you need more memory) then GC will not be productive at all. But there is an exception. If you know that you opened a custom form and made some configuration changes and you know that you are not going to need that form any time sooner, you can go ahead and call GC.Collect() so all the long live objects in Gen2 are now dead. It is recommended to use the following code (starting from Orcas build). Here even though GC.Collect is called on gen2, GC will decide if it is helpful if it collects (2nd parameter – Optimized). This is not available in VS2005 and older versions.

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

b. Create objects that die young: .NET GC is optimized on the premises that most of objects allocated are temporary and die young so they can be collected in gen0 which is cheap. (2)
c. Don’t allocate too many objects: One little line of code could trigger a lot of allocations. Most of the time, it is an allocation that triggers a collection. Keep an eye on what you allocate particularly in loops (2)
d. Don’t allocate too many almost long-life objects: Objects that are neither temporary nor long lived end up in Gen2 and die. This puts pressure on gen2 heap and you may end up doing full collections which is expensive.(2)
e. Don’t allocate too many temporary large objects: Large objects (>85K size) are allocated on a separate large objects heap which is never compacted (it is expensive to move many large objects during compaction). This could put pressure on large object heap, resulting in your doing full collections, also expensive.(2)
f. Dispose and Finalize: Implement these only when needed. Make sure you call these when an exception occurs (to avoid a memory leak). Also make sure you implement finalize only when you have an unmanaged resource and keep the code very simple. (2)


Register to DeveloperMarch

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

FirstName*
LastName*
Company*
Country*
Job Title*
Phone Number*
Email*
UserName*
Password*
Confirm Password*
To:
Name:*
E-mail address:*
Your Details:
Your name: *
E-mail address: *
Message:
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.