Showing posts with label performance tip. Show all posts
Showing posts with label performance tip. Show all posts

March 14, 2011

ASP.NET Performance Tip3 - Use using key word

The using keyword is basically a try - finally block, without a catch , where IDisposable.Dispose() is called in the finally .

Using block defines a scope, outside of which an object or objects will be disposed.

Use using keyword for the objects that  hold any resources other than managed memory.  For Example like files, sockets, database connections, or even GDI drawing handles.

We should  be using "using" whenever the class implements IDisposable. So use of using keyword will always help to avoid Garbage Collection call or explicitly calling garbage collector to

destroy objects.

For example

using(Dataset ds = new Dataset())
{
//code
}

We do not need to worry about destroying objects once the job is done. Garbage Collector will take care of it.


using(ClassName x = new ClassName())
{

     ...use x here...

}

This will automatically remove the resources occupied by the obejct x.

Another Example while working with database objects :

 

using ( SqlConnection connection = new SqlConnection(connectionString) )


{

      SqlCommand cmd = new SqlCommand(commandString, connection);


      connection.Open();


      using (SqlDataReader reader = cmd.ExecuteReader())


      {


         while (reader.Read())


         {

    listBox1.Items.Add(reader[0].ToString() + ", " + reader[1].ToString());
}
}
}
But normally we use try ,catch and dispose objects explicitly in Finally. Why to do this when we have the job that will be done by CLR through GC with the help of using keyword. A "using" statement can be exited either when the end of the "using" statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement. Multiple Objects can be used with using statement as
using (Furniture chair = new Furniture(300, 400),
table = new Furniture(20, 100))
{
    // Use chair and table.
}

It is very important to know when to use what and implement best practices to achieve for performance , reliability

March 06, 2011

ASP.NET Performance Tip2

Read Tip1 Here

2.  Disable Session State

In ASP.NET Session State  is on by default for every page . So we need to disable Session State if we are not going to use it.  Sessions are created to  pass information from one page to other page per user. We do not require Session State when web pages are static or when there is no need to store information captured in the page.  Do not use sessions to store Global data that is specific to all users. Session is specific to single user.

We can this off for specific pages, as well as entire page depending on the requirement. Usage of sessions will significantly decrease the performance of the application .  So we need to try to minimize the use of  sessions in Web Applications.  If we are not using Sessions in the application it can be set off in the web.config that applies to entire application.

Otherwise it can be disabled in page level as

<%@ Page language="c#" Codebehind="Form.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.Form" EnableSessionState="false" %>


In web.config  we can turn off sessionstatemode under System.Web Section as follows.

<sessionState mode="off"></sessionState>

As all of us know,ASP.NET Manages session state automatically. But if we do not require Sessions, disabling it will help in boosting the performance of the application.

March 05, 2011

ASP.NET Performance Optimization Tips

I would like to write series of tips for my blog readers on ASP.NET Application performance and what kind of tips we need to follow to improve it .We design and develop web applications. Perfect. Works Fine in Development Server. We will not have huge data as we have in production environment in the development environment.

So everything goes well in development , QA Testing , Staging. But when it moves to production common problem is Performance . Each & Every Web Developer struggle to make their applications scalable.  When multiple users accessing the application simultaneously, or if we have larger data from years that has grown in the database what are the precautions we need to take?  General Question that sucks is What are the best practices to improve the performance of the application.? So Read on.. and stay tuned for daily updates

1. Disable View State if not used

2. Disable Session State if not required

3. Use using keyword to define scope for objects dispose

4. Set Updatepanel  updatemode to conditional

March 04, 2011

ASP.NET Performance Tip1

1. Disable View State

The view state is important for an ASP.NET page because it is mainly used to persist the state of the Web server controls. As ViewState has performance and security Hit it is important to use to properly and how to ?

ViewState is passed to the client during every postback as an hidden element.  Viewstate is stored in an encrypted format. You can view the view state in Page Source . As its added for every page  view state will definitely effect loading of page at the client side.

Disable ViewState when it is not required.or used.  

For ex: For a Grid that is just used to display records that acts as HTML table , not firing any GridView events or Sorting or paging it is best practice to always disable ViewState.

To add more to optimize webpage performance we need to disable ViewState in the following cases.

  • When a page do not postback to itself
  • When there are no dynamically set control properties
  • When the dynamic properties are set for each request of the page

ASP.NET4 comes with very interesting property called ViewStateMode.  ViewStateMode property is used to control the Viewstate at Control level or Page Level or application level in config File.

Why is this Property ? What is the significance of this?

You can Disable viewstate at page level and enable viewstate at control level using this viewstatemode property. This viewstatemode comes with 3 properties namely

1. Enabled

2. Disabled

3. Inherited  -  ViewStateMode gets inherits from parents control viewstatemode property. For ex. If we have a label inside a panel and label’s viewstatemode property set to inherit, then its viewstatemode property gets inherited from panel. As panel is parent control for label.

If we disable Viewstate at page level and enable viewstate at control level then there is no use as the page level settings override the control settings.

So with the help of this new property we can set viewstatemode to false at page level  and enable viewstatemode for individual controls at control level so that unnecessary viewstate can be avoided.

Note: Viewstatemode property can be used when viewstate is enabled which is set at page level by default.