Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 4th, 2010, 06:15 AM
Authorized User
 
Join Date: Aug 2010
Posts: 16
Thanks: 10
Thanked 0 Times in 0 Posts
Default Application Optimisation

After discovering my application consumes over 100MB of server resources I've spent some time researching how to minmise memory usage. I still haven't fixed the problem, so I going to list what I've been through in hopes somebody may see if I'm missing something.

*I downloaded ANTS Memory Profiler but after spending many hours with the program I still can't seem to understand how to fix the memory leaks. I wasn't able to find much support for this program, every time I run it I'm shown that the majority of memory is used in strings. This seems to be quite common.

*I found what I feel to be an important article from Microsoft I've been try to work off:

http://msdn.microsoft.com/en-us/library/ms998596.aspx

*I've disabled viewstate on all my pages and have sessionstate on sqlserver mode

*I've enabled caching on my AccessDataSources. I tried to use OutputCaching but couldn't get things like the login status to show correctly for pages I'd already visited so I only cached the DataSources

*I changed my coding method about how I was accessing data. Instead of each page_load containing a large number of calculations of a single table I created a few extra tables to contain the calculations and update the extra tables with the calculations as I update the data. This way the calculations were not being performed with every page_load

*I've spent a lot of time researching disposing objects but haven't seemed to incorperate it into my code. Across a spread of eight pages I only use two functions:
-one function creates a dictionary which collects data from a gridview then is databound to a repeater
-the other assign two AccessDataSource's SelectCommands in response to a QueryString
This is the only code in my whole project. I tried to dispose the Dictionary but couldn't and other than that have only created strings.

*I've used Server.Transfer instread of Response.Redirect.

*I removed some code in two pages where I was called custom functions with the Eval("X") function around thirty times to save typing. I still have around five databind functions which reference custom functions. The MSDN article mentions Minimize calls to DataBinder.Eval. Should I aim to not call any functions in databinding?

*I stopped using any Session or Application objects.

*I don't use any server side validation or SSL

This seems to be a broad subject so any input would be much appreciated.

Also in response to Imar's previous post, Do you store lots of objects in session or application state? Do you have a long session timeout? Are you, for example, generating images on the fly and do not dispose of all your objects? Could you have other memory leaks by not using a using statement (or calling Dispose in a Finally block) on disposable objects such as ObjectContexts or SqlConnections?

*session timeout is 30
*I may be doing this but I'm unclear on how to generate images on the fly
*I don't use any objects which have a dispose function or any of my own custom objects

Last edited by R4VEN; December 4th, 2010 at 06:31 AM..
 
Old December 4th, 2010, 08:19 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Depending in the data used in your application and the number of users to your site, 100MB is not a whole lot, and isn't something I would worry about if you're not having performance issues.

Remember that caching *consumes* memory. Caching is great for improving performance as pages render much quicker, but since the data is stored in memory, the application's footprint on your server increases.

Quote:
I may be doing this but I'm unclear on how to generate images on the fly
If you don't know how to generate images on the fly, then you're probably not doing that ;-) What I was referring to was, for example, streaming images directly from a database.

Quote:
I don't use any objects which have a dispose function or any of my own custom objects
I wonder if that's true. Many built-in objects such as a SqlConnection, a SqlCommand and object contexts for LINQ to SQL and Entity Framework implement IDisposable. If you don't wrap them in a using block or manually dispose them, they could increase the memory used by the application.
Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
R4VEN (December 6th, 2010)
 
Old December 6th, 2010, 08:38 AM
Authorized User
 
Join Date: Aug 2010
Posts: 16
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Caching consumes memory? I'll disable that and see if it helps.

I don't use the word SqlConnection or SqlCommand even once in my code and I don't use an Entity Framework or even LINQ. Are there controls such as a AccessDataSource or a ListView etc that I need to close/dispose of?
I have hardly any C# on my site.

I feel this is the area I need to understand more because I don't dispose anything. My site is as minimal as possible, I'm trying to isolate the problem and avoid complex coding.

Thank you for your patience with this.

Also 100MB is the threshhold that get my application recycled. I think.

Last edited by R4VEN; December 6th, 2010 at 09:00 AM..
 
Old December 6th, 2010, 09:03 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Caching consumes memory? I'll disable that and see if it helps.
Yes, it does. When you retrieve some database from, say, a database and then store it in the cache, you can access it from the cache (which stores the objects in memory) on subsequent requests. Since accessing memory is way faster than accessing a database, caching is often a very good solution to increase performance. So, I am not sure if "it helps" is appropriate here; Disabling caching may lower the memory footprint of your application in some areas, but it may result in more CPU utilization and memory usage to get the data from the data source on each request.

Quote:
Are there controls such as a AccessDataSource or a ListView etc that I need to close/dispose of?
Nope.

Quote:
I don't use the word SqlConnection or SqlCommand even once in my code and I don't use an Entity Framework or even LINQ.
...
because I don't dispose anything.
If your code does not create objects that need be disposed, you don't have to do anything. ASP.NET should take care of objects it creates itself such as the AccessDataSource.

Quote:
I'm trying to isolate the problem and avoid complex coding.
But what is the problem exactly? 100 MB for a web site isn't really something I would worry about. Maybe you're too concerned and are over-optimizing things? You need to be worried if your memory consumption grows over time when the application is used. E.g. a process that grows by 10MB every hour the application is used. But if the application remains steady at 100MB over time, I would focus on other things.... ;-)

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
R4VEN (December 6th, 2010)
 
Old December 6th, 2010, 10:17 PM
Authorized User
 
Join Date: Aug 2010
Posts: 16
Thanks: 10
Thanked 0 Times in 0 Posts
Default

When I test my site on my home server it works perfectly but when I access it from my hosting server I get logged out randomly.

After spending time on the hosting help forum I was told the following:

Your application is definitely being recycled due to memory usage. I suggest you optimize your application so that it doesn't consume a lot of the servers memory. Normal websites should only be using less then 100 MB of the servers memory.

My solution around this problem is that I removed all unneeded code from my site (I had a lot of bells & whistles stuff relating to the username). I then condensed all the needed username functionality into a single page. To access this page is a login page which redirects to this page when logged in.

Profile.aspx pretty much.

I've found with this setup a user only ever navigates a maximum of two pages to perform user related tasks.

Ideally I'd like to not be logged out randomly but this solution is working at the moment
 
Old December 7th, 2010, 04:03 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Ideally I'd like to not be logged out randomly
This seems to suggest that you keep track of whether a user is logged in or not in session state, is that correct?

Normally, when you use the built-in membership provider, your client gets an authentication cookie which remains valid, even if the server restarts / recyclcles.

What authentication system do you use? Do you make use of session state?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
R4VEN (December 8th, 2010)
 
Old December 8th, 2010, 12:30 AM
Authorized User
 
Join Date: Aug 2010
Posts: 16
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Cookies? I'm not too sure whether I track users being logged through session state.

Looking into my web.config file:

Code:
<sessionState timeout="30" mode="SQLServer" allowCustomSqlDatabase="true" cookieless="false" sqlConnectionString="data Source=tcp:s01.winhost.com;database=DB_8868_users; user id=DB_8868_users_user;password=password1"/>

<authentication mode="Forms"/>
I hope this is the information you're talking about regarding Sessionstate & authentication
 
Old December 8th, 2010, 04:21 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
I'm not too sure whether I track users being logged through session state
I am, unless you changed your coding: ASP.NET 4 Login timeout

You have some code that stores user data in session state. If, for some reason, that session state is lost (for example when the web server process is restarted) your user loses that data.

By relying on standard Membership and / or Forms Authentication features, the user is authenticated based on a cookie. Since that cookie is stored at the client, it can be examined on each request to determine if the user is still logged in. You then have User.Identity.IsAuthenticated and User.Identity.Name at your disposal to determine the user's name: http://msdn.microsoft.com/en-us/libr...text.user.aspx

By not relying on session state, you make your application more robust as it's able to survive restarts without annoying the user.

In your case, however, I am not sure what's going on. You have configured SQL Server as the store for session state which should guard your session data even if the web server process recycles. So, unless the code you posted is not in effect, or you haven't carried out the steps to configure SQL Server for session state as explained here: http://msdn.microsoft.com/en-us/library/ms178586.aspx I don't know what's going on. It's hard to say something about this without seeing the complete code for the application and the server's configuration.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
R4VEN (December 9th, 2010)
 
Old December 9th, 2010, 07:33 AM
Authorized User
 
Join Date: Aug 2010
Posts: 16
Thanks: 10
Thanked 0 Times in 0 Posts
Default

I've stopped storing usernames in Session because of this.

I spent a lot of time setting up the SQLServer mode with my hosting forum so I feel it should be alright. There's most likely some deeper problems I'm unaware of causing this so I feel I should just keep studying ASP.NET so I can get to the bottom of this as it doesn't seem obvious.

Thank you so much for your help so far, I plan to purchase the Professional ASP.NET 4.0 book after Christmas because the other book has been so helpful. I'm interested in learning about the code involved in forums, I think ages ago I saw a book you wrote touched on that. Do you have a title?



I'll next year create another ASP.NET 4 website at some point, I'll try with a different provider on the chance that it's something out of my control causing this.
 
Old December 9th, 2010, 07:51 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
I'm interested in learning about the code involved in forums,
Not sure I understand what you mean with this so I can't answer the question.... ;-)

Quote:
I've stopped storing usernames in Session because of this.
Then I don't understand why you're being logged out. Maybe you just haven't set a long timeout in the timeout attribute of the <forms /> element in web.config?

Do you want to share the code of your site (privatly if you wish) so I can take a look and see what's wrong?

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
R4VEN (December 10th, 2010)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Search Engine Optimisation SouthendSupporter ASP.NET 3.5 Basics 1 August 6th, 2009 02:59 PM
Optimisation TIP: Viewstate jimibt BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 6 May 15th, 2007 09:57 AM
Query Optimisation artidatanova SQL Server 2000 1 April 4th, 2005 08:42 PM
Search Engine Optimisation mekh HTML Code Clinic 1 February 25th, 2005 04:54 AM
CPU Usage Optimisation avbabu VS.NET 2002/2003 1 January 21st, 2005 07:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.