Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

Archive for January, 2010

Best Practices for Speeding Up Your Web Site

Posted by Ramani Sandeep on January 30, 2010

The Exceptional Performance team has identified a number of best practices for making web pages fast. The list includes 34 best practices divided into 7 categories.

1) Content

  • Minimize HTTP Requests
  • Reduce DNS Lookups
  • Avoid Redirects
  • Make Ajax Cacheable
  • Post-load Components
  • Preload Components
  • Reduce the Number of DOM Elements
  • Split Components Across Domains
  • Minimize the Number of iframes

2) Server

  • Use a Content Delivery Network
  • Add an Expires or a Cache-Control Header
  • Gzip Components
  • Configure ETags
  • Flush the Buffer Early
  • Use GET for AJAX Requests

3) CSS

  • Put Stylesheets at the Top
  • Avoid CSS Expressions
  • Choose <link> over @import
  • Avoid Filters

4) Javascript

  • Put Scripts at the Bottom
  • Make JavaScript and CSS External
  • Minify JavaScript and CSS
  • Remove Duplicate Scripts
  • Minimize DOM Access
  • Develop Smart Event Handlers

5) Cookie

  • Reduce Cookie Size
  • Use Cookie-free Domains for Components

6) Images

  • Optimize Images
  • Optimize CSS Sprites
  • Don’t Scale Images in HTML
  • Make favicon.ico Small and Cacheable

7) Mobile

  • Keep Components under 25K
  • Pack Components into a Multipart Document

           Read more

About these ads

Posted in ASP.NET, ASP.NET 3.5, ASP.NET 4.0, ASP.NET Ajax, Css, JavaScript, Performance | Tagged: , | Leave a Comment »

MultiSelect Dropdown in ASP.NET

Posted by Ramani Sandeep on January 29, 2010

In this article, I will show you a different way of doing a multi-select in an ASP.NET page. I will keep this article short and sweet so you can just use the code in your applications.

We are going to put our CheckBoxList ASP.NET control inside an HTML div object. I have also added code to support changing the background color of the selected row.

Read more

Shout it

Posted in ASP.NET, JavaScript | Tagged: , , | Leave a Comment »

Patterns for Great Architecture

Posted by Ramani Sandeep on January 28, 2010

These concepts can be applied to any Application which is a good candidate for a SaaS application.

Contents:

  1. SaaS- Introduction
  2. SaaS -Challenges
  3. SaaS -Solution to challenges

SaaS- Introduction

A SaaS application can be defines as any “Software deployed as a service and accessed using internet technologies”. In order to realize a SaaS solution (in fact any solution) we need to do two things:

1) Build our application which can be used as a service over internet by different clients. This could range from a programmatic service (accessed programmatically by other softwares) to a stand-alone application used by different clients. This step is involves us to follow certain steps that are different than developing a normal “On-Premise” or simply called a non-SaaS application.

Typically when we develop an application (a non-SaaS application) then we do assume two things:

  • Application will be installed at client side (e.g., a desktop application) be it through a CD or downloading via internet. This also implies that the client will have to do the maintenance (like upgrading to new version, apply patches, maintain databases etc.) of application even after he has bought the application (not true for Application Service Provider model). This fact is not true in terms of a web application. So we can say that any web application forms a different category of application since it is not installed at client side. But still we cannot call ANY web application a SaaS application.
  • Application will be served to requirement of a particular client. If another client needs some changes in the application, we will make changes in the application source code and then run another instance of that application for the new client. So in nutshell, we assume that one application is meant for one client only. This assumption is true even for a web application.

Here comes the difference! The core of a SaaS application is based on the opposites of these two (above mentioned) assumptions. The following are assumptions that are true for a SaaS application.

  • Application will be typically deployed and maintained by a hosting provider. So, clients don’t have to invest in terms of buying hardware resources and employing the IT staff to manage the application. This will be done by the hosting provider.
  • Application will be accessed by clients using internet. In special cases where a SaaS application is hosted inside the enterprise itself this is not true but still the clients accessing the hosted application will still use internet technologies to access the application.
  • A single SaaS application (in fact a single database too) serves more than one client having different needs. So the application will be designed in such a way that only a single application instance will be able to provide different functionalities to different clients. This model is more popularly known as Multi-tenant model.

Among these assumptions, the last assumption related to multi-tenancy requires a SaaS application to be architected in a special way keeping in mind about certain aspects of a multi-tenant application. These aspects form the challenges of building a SaaS application and also our next topic of discussion.

2) SaaS world does not end just by building the application that satisfies SaaS characteristics. Deploying a SaaS application forms another set of challenges. Typically a SaaS application is deployed by a SaaS hosting provider and the hosting provider is responsible of maintaining the application. So, not only clients get rid of maintaining the application but also ISVs get rid of that aspect.

Microsoft provides end to end resources in developing and deploying a SaaS application. It provides resources ranging from development frameworks and technical resources used to build and design the application to hosting options that assist in deploying the application.

Read more

Shout it

Posted in Enterprice Applications | Tagged: , | 2 Comments »

Cascading DropDownLists With ASP.NET and jQuery

Posted by Ramani Sandeep on January 25, 2010

Cascading drop down lists is a really nice feature for web developers. I thought it was time to write an article on how to do this using ASP.NET and jQuery. Well here’s one way of doing it. Before we get started, this example uses the latest version of jQuery which is 1.3.2. That can be downloaded from here.

Read full article here :

http://www.dotnetcurry.com/ShowArticle.aspx?ID=417&AspxAutoDetectCookieSupport=1

Hope this will help

Shout it

Posted in ASP.NET 3.5, JQuery | Tagged: , | 3 Comments »

Indexer Vs Property

Posted by Ramani Sandeep on January 23, 2010

Recently I gone threw the Technical Interview in Accenture. In My Tech Round they have asked me

“What is Indexer?”

I was not able to answer the question because i never used it in any of my project as well not read anywhere about it.

Luckily I am selected also. Sooner will get appointment letter if i get what i want. (HR Round)

So I goggled on it & find out what is indexer. now Here I am looking to share it with you so that at least you can answer.

Indexer Property
Identified by its signature. Identified by its name.
Accessed through an element access. Accessed through a simple name or a member access.
Must be an instance member. Can be a static or an instance member.
A get accessor of an indexer has the same formal parameter list as the indexer. A get accessor of a property has no parameters.
A set accessor of an indexer has the same formal parameter list as the indexer, in addition to the value parameter. A set accessor of a property contains the implicit value parameter.

Hope this will help

Reference  : http://msdn.microsoft.com/en-us/library/6x16t2tx%28VS.71%29.aspx

Posted in C# 2.0 | Tagged: , , , | 1 Comment »

.Net Memory Management & Garbage Collection

Posted by Ramani Sandeep on January 20, 2010

I have seen one video from red-gate about the memory management in .net when I was surfing about ANTS memory profiler. So I thought why not I create article based on this facts & help others to know how Garbage collector actually works.

Introduction

The Microsoft .NET common language runtime requires that all resources be allocated from the managed heap. Objects are automatically freed when they are no longer needed by the application.

When a process is initialized, the runtime reserves a contiguous region of address space that initially has no storage allocated for it. This address space region is the managed heap. The heap also maintains a pointer. This pointer indicates where the next object is to be allocated within the heap. Initially, the pointer is set to the base address of the reserved address space region.

Garbage collection in the Microsoft .NET common language runtime environment completely absolves the developer from tracking memory usage and knowing when to free memory. However, you’ll want to understand how it works. So let’s do it.

Topics Covered

We will cover the following topics in the article

  1. Why memory matters
  2. .Net Memory and garbage collection
  3. Generational garbage collection
  4. Temporary Objects
  5. Large object heap & Fragmentation
  6. Finalization
  7. Memory problems

1) Why Memory Matters

Insufficient use of memory can impact

  • Performance
  • Stability
  • Scalability
  • Other applications

Hidden problem in code can cause

  • Memory leaks
  • Excessive memory usage
  • Unnecessary performance overhead

2) .Net Memory and garbage collection

.Net manages memory automatically

  • Creates objects into memory blocks(heaps)
  • Destroy objects no longer in use

Allocates objects onto one of two heaps

  • Small object heap(SOH) – objects < 85k
  • Large object heap(LOH) – objects >= 85k

You allocate onto the heap whenever you use the “new” keyword in code

Small object heap (SOH)

  • Allocation of objects < 85k – Contiguous heap – Objects allocated consecutively
  • Next object pointer is maintained – Objects references held on Stack, Globals, Statics and CPU register
  • Objects not in use are garbage collected

Figure-1

SOH_1

Next, How GC works in SOH?

GC Collect the objects based on the following rules:

  • Reclaims memory from “rootless” objects
  • Runs whenever memory usage reaches certain thresholds
  • Identifies all objects still “in use”
    • Has root reference
    • Has an ancestor with a root reference
  • Compacts the heap
    • Copies “rooted” objects over rootless ones
    • Resets the next object pointer
  • Freezes all execution threads during GC
    • Every GC runs it   hit the performance of your app

Figure-2

SOH2

3) Generational garbage collection

Optimizing Garbage collection

  • Newest object usually dies quickly
  • Oldest object tend to stay alive
  • GC groups objects into Generations
    • Short lived – Gen 0
    • Medium – Gen 1
    • Long Lived – Gen 2
  • When an object survives a GC it is promoted to the next generation
  • GC compacts Gen 0 objects most often
  • The more the GC runs the bigger the impact on performance

Figure-3

Generations_3

Figure-4

Generations_4

Here object C is no longer referenced by any one so when GC runs it get destroyed & Object D will be moved to the Gen 1 (see figure-5). Now Gen 0 has no object, so when next time when GC runs it will collect object from Gen 1.

Figure-5

Generations_5

Figure-6

Generations_6

Here when GC runs it will move the object D & B to Gen 2 because it has been referenced by Global objects & Static objects.

Figure-7

Generations_7

Here when GC runs for Gen 2 it will find out that object A is no longer referenced by anyone so it will destroy it & frees his memory. Now Gen 2 has only object D & B.

Garbage collector runs when

  • Gen 0 objects reach ~256k
  • Gen 1 objects reach ~2Meg
  • Gen 2 objects reach ~10Meg
  • System memory is low

Most objects should die in Gen 0.

Impact on performance is very high when Gen 2 run because

  • Entire small object heap is compacted
  • Large object heap is collected

4) Temporary objects

  • Once allocated objects can’t resize on a contiguous heap
  • Objects such as strings are Immutable
    • Can’t be changed, new versions created instead
    • Heap fills with temporary objects

Let us take example to understand this scenario.

Figure – 8

TempObj_8

After the GC runs all the temporary objects are destroyed.

Figure–9

TempObj_9

5) Large object heap & Fragmentation

Large object heap (LOH)

  • Allocation of object >=85k
  • Non contiguous heap
    • Objects allocated using free space table
  • Garbage collected when LOH Threshold Is reached
  • Uses free space table to find where to allocate
  • Memory can become fragmented

Figure-10

LOH_10

After object B is destroyed free space table will be filled with a memory address which has been available now.

Figure-11

LOH_11

Now when you create new object, GC will check out which memory area is free or available for our new object in LOH. It will check out the Free space table & allocate object where it fit.

Figure-12

LOH_12

6) Object Finalization

  • Disk, Network, UI resources need safe cleanup after use by .NET classes
  • Object finalization guarantees cleanup code will be called before collection
  • Finalizable object survive for at least 1 extra GC & often make it to Gen 2
  • Finalizable classes have a
    • Finalize method(c# or vb.net)
    • C++ style destructor (c#)

Here are the guidelines that help you to decide when to use Finalize method:

  • Only implement Finalize on objects that require finalization. There are performance costs associated with Finalize methods.
  • If you require a Finalize method, you should consider implementing IDisposable to allow users of your class to avoid the cost of invoking the Finalize method.
  • Do not make the Finalize method more visible. It should be protected, not public.
  • An object’s Finalize method should free any external resources that the object owns. Moreover, a Finalize method should release only resources that are held onto by the object. The Finalize method should not reference any other objects.
  • Do not directly call a Finalize method on an object other than the object’s base class. This is not a valid operation in the C# programming language.
  • Call the base.Finalize method from an object’s Finalize method.

Note: The base class’s Finalize method is called automatically with the C# and the Managed Extensions for C++ destructor syntax.

Let see one example to understand how the finalization works.

Each figure itself explain what is going on & you can clearly see how the finalization works when GC run.

Figure-13

Final_13

Figure-14

Final_14

Figure-15

Final_15

Figure-16

Final_16

Figure-17

Final_17

For more information on Finalization refer the following links:

http://www.object-arts.com/docs/index.html?howdofinalizationandmourningactuallywork_.htm
http://blogs.msdn.com/cbrumme/archive/2004/02/20/77460.aspx

How to minimize overheads

Object size, number of objects, and object lifetime are all factors that impact your application’s allocation profile. While allocations are quick, the efficiency of garbage collection depends (among other things) on the generation being collected. Collecting small objects from Gen 0 is the most efficient form of garbage collection because Gen 0 is the smallest and typically fits in the CPU cache. In contrast, frequent collection of objects from Gen 2 is expensive. To identify when allocations occur, and which generations they occur in, observe your application’s allocation patterns by using an allocation profiler such as the CLR Profiler.

You can minimize overheads by:

  • Avoid Calling GC.Collect
  • Consider Using Weak References with Cached Data
  • Prevent the Promotion of Short-Lived Objects
  • Set Unneeded Member Variables to Null Before Making Long-Running Calls
  • Minimize Hidden Allocations
  • Avoid or Minimize Complex Object Graphs
  • Avoid Preallocating and Chunking Memory

Read more: http://www.guidanceshare.com/wiki/.NET_2.0_Performance_Guidelines_-_Garbage_Collection

7) Common Memory Problems

  • Excessive RAM footprint
    • App allocates objects too early or for too long using more memory than needed
    • Can affect other app on system
  • Excessive temporary object allocation
    • Garbage collection runs more frequently
    • Executing threads freeze during garbage collection
  • Memory leaks
    • Overlooked root references keep objects alive (collections, arrays, session state, delegates/events etc)
    • Incorrect or absent finalization can cause resource leaks

References & other useful articles related to this topic:

Hope this help

Jay Ganesh

Shout it kick it on DotNetKicks.com

Posted in .Net Framework | Tagged: , , , , | 7 Comments »

How to deploy ASP.NET Web Application on server

Posted by Ramani Sandeep on January 20, 2010

Hello, in this article i have tried to explain, how a web application is deployed on to web server.

Deployment refers to the process of copying an asp.net web application from the development system to the server on which the application will be run. There are several way we can deploy our web application

We can deploy ASP.NET Application in  3 different ways

  • xCopy Deployment
  • Precompiled Deployment
  • Web Setup Project  

The choice of best deployment alternative depends upon particular need of each application. Xcopy deployment is the most easiest, and it is often used during development to create copies of an application n different servers for testing purpose. For small application xcopy deployment may be the best choice.

Precompiled deployment has several advantages over XCopy deployment. Eg. Precompiled deployment is always gives better performance for the first users of the site at the same time it is more secure as we don’t need to copy our source code files on to server. If our application deployed on one or few servers then precompiled deployment is usually best choice.

When we are going to deploy our application on number of servers then creating a setup program is a very handy tool. Although creating this setup program is much tedious and involves considerable working, the deployment from this setup program becomes very easier.

read more : http://www.codeproject.com/KB/aspnet/Deploy_web_application.aspx

Hope this will help

Jay Ganesh

Posted in ASP.NET, ASP.NET 3.5, ASP.NET 4.0 | Tagged: , , , , , | 4 Comments »

Duwamish 7.0

Posted by Ramani Sandeep on January 19, 2010

Duwamish 7.0 is a multi-tier, distributed enterprise application built specifically for the Microsoft .NET Platform. Its design, development, and deployment provide insight into how developers can leverage various features of the .NET Platform to build reliable, scalable, and well-performing applications.

Overview

Duwamish Books Inc. is a fictitious company that sells books online. Since its model is an e-commerce business-to-consumer pattern that is most prevalent in the typical online shopping experience, it includes basic features such as membership, account management, shopping cart, search, and checkout processes.

Duwamish 7.0 is a functional port (using 100 percent .NET technologies) of the popular Duwamish series of applications developed by MSDN. Although the sample itself is built around a fictitious online bookstore, the primary focus areas of this sample are performance, issues related to porting technology from Windows DNA to the .NET developer platform, design patterns, and real-life deployment scenarios in a distributed computing environment.

Functionally, it is a complete implementation of the pattern without the fulfillment processes fully implemented (that is, credit card account decrements, checking inventory, and shipping). However, the features are sufficiently complex and cover a broad range of .NET technological areas to illustrate the primary goals.

read more : http://msdn.microsoft.com/en-us/library/aa288561%28VS.71%29.aspx

Hope this will help

Jay Ganesh

Posted in Enterprice Applications | Tagged: , , , | Leave a Comment »

Budhia Singh – world’s youngest marathon runner

Posted by Ramani Sandeep on January 18, 2010

Recently When I visited my home for Utrayan on 14-Jan-2010, I was watching National Geographic channel on TV channel. They were talking about the Rising heroes of India – Budhia singh. A Boy who is the youngest marathon runner in the world. You might be knowing about this news but if you don’t than here is some of information how Budhia became a Marathon runner from slum dog to national athletes.

Budhia Singh

Budhia Singh (born 2002) is an Indian boy and the world’s youngest marathon runner. Generally considered an athletic phenomenon, Budhia has participated in (and finished) races of up to 60 kilometers (37.3 miles) in roughly six hours and thirty minutes.

Limca Book of Records

On 2 May 2006, Budhia Singh completed a 65 kilometres (40 mi) run from Chapandie temple to Bhubaneshwar in seven hours, two minutes. This feat was registered in the Limca Book of Records. The race was originally designed to be a 70 kilometres (43 mi) race, but his body collapsed 5km shy of the target distance. Budhia was not allowed by his coach to drink during his run.

more information

http://www.indeaparis.com/index.php?option=com_content&task=view&id=673
http://en.wikipedia.org/wiki/Budhia_Singh#Murder_of_Biranchi_Das

 

Keep the Spirit high if you want to achieve something – don’t give up

Posted in Mics | Tagged: , , | Leave a Comment »

Access Viewstate across pages in ASP.NET

Posted by Ramani Sandeep on January 9, 2010

Introduction

There was one question during my recent work in one of my ASP.NET Project.

“Is it possible to access the ViewState variable of one page on another page?”

My answer was “No” . Because i have read in books that Viewstate is page specific, it is available only on the same page on which it was created.Once you redirect to another page, the previous page’s viewstate is no longer accessible.

But that is not true. Yes, we can access the viewstate variables across pages. This is only possible if Cross Page Posting or Server.transfer is used to redirect the user to other page. If Response.redirect is used, then ViewState cannot be accessed across pages.

Let us create one sample application to understand this.

Step 1 : Create two page one.aspx & two.aspx

Step 2 : Code for one.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        ViewState["Page1"] = "Page1 ViewState";
        Server.Transfer("two.aspx");
    }

    public StateBag ReturnViewState()
    {
        return ViewState;
    }

As you can see, I have set a ViewState variable in Page Load and transfer the user to two.aspx page using the Server.transfer() method. This page also contains a method ReturnViewState() which actually returns the ViewState of this page to the calling function. The return type of the method is StateBag class.

StateBag class: This class is the primary storage mechanism for all HTML and Web server controls. It stores attribute/value pairs as strings associated with the control. It tracks changes to these attributes only after the OnInit method is executed for a page request, and saves the changes to the page’s or control’s viewstate.

Step 3 : Code for two.aspx.cs

    private StateBag PreviousPageViewState
    {
        get
        {
            StateBag returnValue = null;
            if (PreviousPage != null)
            {
                Object objPreviousPage = (Object)PreviousPage;
                MethodInfo objMethod = objPreviousPage.GetType().GetMethod
						("ReturnViewState");
                return (StateBag)objMethod.Invoke(objPreviousPage, null);
            }
            return returnValue;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            if (PreviousPageViewState != null)
            {
                Label1.Text = PreviousPageViewState["Page1"].ToString();
            }
        }
    }

Whenever we use Server.transfer or Cross Page Posting, We can get the previous page object via PreviousPage property. Using Previous Page, we can find the controls of the previous page. For example, one can access Label control placed in ViewStateContainer Page in current Page.

Looking at the code, I have created a PreviousPageViewState property in this page, which returns the previous page’s ViewState. It first checks whether PreviousPage is null or not, if it’s not null, then it creates an object of the previous page.

Now using Reflection, we can invoke the method of the previous class. Using MethodInfo class, I have invoked the ReturnViewState() method of ViewStateContainer Page.

In Page_Load event, I am able to access the ViewState variable of ViewStateContainer Page. You can access all the viewstate variables set in ViewStateContainer Page.

Step 4: Run the application & see the effect

Recently I got comment from Bijayani & he has highlighted one of the important point regarding  “Viewstate”. so here I am sharing with you.

A Tip While Using ViewState

View state maintains data in a page across postbacks. And this data passes in form of hidden field data. There is a certain limitation of this hidden field. If your hidden field will be greater than that specified value, then sometimes firewalls and proxy servers refuse to let your data passes through. And in that case you have to disable your view state .This might cause many problem if you want your data to be passed.

So to overcome this problem you can do viewstate chunking. This is a process of splitting the data into multiple chunks and putting them into multiple hidden fields, So that there will not be any problem to your data to pass through. This chunking is done by  adding “MaxPageStateFieldLength” property in the page tag in web.config file. The default value is -1 which indicates there is no maximum limit of data. You have to give the integer value which indicates the maximum bytes size for a viewstate field.

For more detail visit this link:
http://www.mindfiresolutions.com/a-tip-while-using-viewstate-548.php

Hope this will helps

Jay Ganesh

Shout it kick it on DotNetKicks.com

Posted in ASP.NET | Tagged: , , | 6 Comments »

 
Follow

Get every new post delivered to your Inbox.

Join 124 other followers