Why is my website taking so much time to load?
One of the common reason could be due to growth in site's traffic… Now the question is what should be done to make the site faster. Is it to add additional hardware components or is there something to deal with software? Lets check it out...
First lets start examining the processes which are running on the server. In my case I found that CPU was showing 100% usage with one process called "W3WP.EXE" consuming the highest memory. W3WP.EXE is an IIS web server’s worker process
Now I started digging why W3WP.exe process is consuming so much memory. The solution is actually very simple, but first a little background information.
What is the W3WP.exe process?
The W3WP.exe is the IIS worker process, that handles requests, maintain sessions, viewstate, and the cache. Its been around Since IIS 6 on Windows Server 2003, and is still found as part of IIS 7.5 with Server 2008 R2. On a properly configured, and programmed website the W3WP.exe can usually stay under 100 Megs. If you are running a 64bit OS it could be a little larger. The actual size of the W3WP.exe will be determined by how much traffic a website gets, and how many pages it serves. The 100 Meg figure is just an estimate from my own experiences. Application Pools with multiple websites can easily go over this figure, so don't use it some magic number.
Most popular causes of W3WP.exe using too much memory
Page Caching Dynamic Websites
Some Programmers will use Page Caching on their ASP.NET pages to improve request speeds. Even though this does work. It will quickly eat up all your memory. For Page Caching to work you would need to cache each page and every combination based on its querystring variables. For Members sites this could result in millions of different pages. A more effective way of using caching is to Cache objects that do not change, and are used throughout the website. Things like static comtent, your menu bar, header and footer etc. Or maybe the details for an event. This will reduce the number of calls to the Database Server, and speed up your site. Some may argue that the function of a Database server is to cache popular queries, and that there is no difference between retrieving the data from the database server and caching the objects in your web server's memory. This is absolutely false for many reasons. First anytime your website has to retrieve data from the database, it has to open a connection to another process, even if this process is running on the same box as the web server there is a very small performance penalty to establish this connection. What if your DB server is in a datacenter which is somewhere else. So now your web server has to reach across the datacenter to a separate server. At this point the latency of their network becomes an issue. This is the reason I highly recommend doing everything you can to reduce the calls to the database. Object Caching can make a huge difference on page load times, especially when you are able to cache objects that use expensive queries.
ViewState Enabled on Labels
The ViewState is how ASP.NET keeps track of changes that have been made to Web Controls. ViewState is needed anytime you are going to do a postback, like for a submit form. But a common mistake is programmers don't disable viewstate on Label controls that are often used as place holders for long concatenated HTML strings. If you suspect your may be guilty of this all it takes is a look at the HTML source code from your web browser to confirm it. If your HTML source looks like this

with a really big view state then you need to disable the viewstate on your label controls. The Viewstate is stored in the W3WP.exe process. Its easy to see how this process can consume so much memory when you have a view state this large and on an exponential number of web pages. The solution is to change your ASP:Label Controls from this
<asp:Label ID="mainmenu" runat="server" />to this
<asp:Label ID="mainmenu" EnableViewState="false" runat="server" />
You can also disable the viewstate of complete page under its header as below
<%@ Page EnableViewState=”false” EnableSessionState=”ReadOnly” ...%>
Lets face it there is no reason you need to preserve the ViewState of a Label Control. So go through all your pages and add the EnableViewState="false" attribute to all your pages.
Here you can also see that we have additionally disabled session state.This is with assumption that since Session is accessed for read only then we can set it as read only saving on locking and preventing race conditions.
Unclosed Database Connections
If you use helper functions to manage your Database connections, be sure that you close the connections, when finished. Else this will waste memory on both the W3WP.exe side and the Database Server side.
Conclusion
Thus by doing a little cleanup work as above, we can get decent improvement in our server responses without adding any servers.

No comments:
Post a Comment