Scott on Writing

Musings on technical writing...

How Big is Too Big a ViewState?

When creating ASP.NET pages one thing that usually doesn't get looked at too intensely by developers is the page's ViewState weight (I've been guilty of this myself).  While there are various mechanisms to reduce the ViewState bloat in a page, the ultimate (uneloquently worded) question is, “How big is too big a ViewState?”  Dino Esposito chimes in with some metrics in his blog entry ViewState Numbers [emphasis Dino's]:

You should endeavour to keep a page size around 30 KB, to the extent that is possible of course. For example, the Google’s home page is less than 4 KB. The home page of ASP.NET counts about 50 KB. The Google’s site is not written with ASP.NET so nothing can be said about the viewstate; but what about the viewstate size of the home of the ASP.NET site? Interestingly enough, that page has only 1 KB of viewstate. On the other hand, this page on the same site (ASP.NET) is longer than 500 KB of which 120 KB is viewstate.

The ideal size for a viewstate is around 7 KB; it is optimal if you can keep it down to 3 KB or so. In any case, the viewstate, no matter its absolute size, should never exceed 30% of the page size.

I think these are good metrics to live by when building apps targetted for the Internet.  ViewState enacts a “double hit” regarding page load time.  First, the user must download the ViewState bytes.  Then, when posting back, that same ViewState must be reuploaded to the web server (sent back in the POST headers).  And then, when receiving back the resulting markup from the postback, the ViewState (possibly modified) is sent back down again.  So during a postback there's a seemingly double hit.  That is, if it takes x seconds to download the ViewState of the page, when the user posts back it will take at least 2x - x to upload the ViewState and another x to download it back again.  Cripes!  (This is part of the reason AJAX is so appealing in Internet situations, although AJAX carries with it it's own slew of issues.)

When you're building intranet apps, where you know your user's will be connecting over a LAN, the page sizes and ViewState sizes are not as important as they impact the user experience much less.  Most of my “real-world” projects have been created for the intranet setting, so I've not had to fret over ViewState size as much as others may have. 

For those projects where a trim ViewState size is paramount, one common question is how to quickly determine the ViewState size (and, perhaps, what junk is actually being stored in there).  For the ViewState size, I usually just do a View/Source and then highlight the ViewState content.  (In UltraEdit - my text editor of choice - the number of bytes selected is shown in the toolbar.)  To determine the contents of ViewState there are tools like Fritz Onion's ViewState decoder (for ASP.NET 1.x and 2.0) and Nikhil Kothari's Web Development Helper (for 2.0).  I also provide code for a web-based ViewState decoder (for ASP.NET 1.x) in my article, Understanding ASP.NET ViewState.

posted on Wednesday, September 07, 2005 9:14 AM

Feedback

# re: How Big is Too Big a ViewState? 9/7/2005 2:03 PM Kevin Blakeley

Some people may have more resources available to them at the server level and could keep the viewstate on the server. I am not sure how well known it is, but you can caputre the rendering of the viewstate so nothing gets sent to the client and keep it all on the web servers. Read http://devauthority.com/blogs/whoiskb/archive/2005/06/27/30.aspx for more info.

# re: How Big is Too Big a ViewState? 9/8/2005 9:40 AM Girish Gangadharan

Good point. How do you find out how much each control's viewstate takes up, anyways? I can imagine Viewstate.ToString kind of operation. Any suggestions?

# re: How Big is Too Big a ViewState? 9/13/2005 7:08 AM Craig Suthers

You could either view the page source and copy the view state to a seperate file and look at the files size.

Or you could add a bit of javascript that shows you the viewstate. Something like

#if DEBUG
RegisterStartupScript( "ViewStateSize", "<script language='javascript'>window.status = 'ViewState size: ' + document.forms[0].__VIEWSTATE.value.length;</script>" );
#endif

added to your page class will output it for you.

# re: How Big is Too Big a ViewState? 9/14/2005 6:03 PM Jacob

I am using Cute Edit instead of UltraEdit.

CuteEdit: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx

I am interested in the UltraEdit. Can you tell me why you choose UltraEdit as your rich text editor?

# re: How Big is Too Big a ViewState? 9/15/2005 5:51 AM Kevin

Craig: with that javascript, is the value returned in B or KB?
And is there a similar way to return the page size?

# re: How Big is Too Big a ViewState? 9/15/2005 7:50 AM Kevin

Since I found a script and altered it a bit to answer my question above...I'll post it here...this uses an Alert instead of the status bar....

"<script language='javascript'>var size = (document.fileSize)*1; var y = document.images; var imglength = 0; for (i=0;i<y.length;i++) { imglength += (y[i].fileSize)*1; } var total = size + imglength; var writestring = 'File size HTML: ' + size; writestring += '\nFile size images: ' + imglength; writestring += '\nTotal file size: ' + total; if (total > 40000) writestring += '\nFile too large!'; writestring += '\nViewState size: ' + document.forms[0].__VIEWSTATE.value.length; alert(writestring); </script>"
Page.RegisterStartupScript("PopupScript", popupScript)

# FeedBurner Summary 9/15/2005 12:34 PM Scott on Writing

# re: How Big is Too Big a ViewState? 3/20/2006 8:23 AM Frank

To see the breakdown of how much each control adds to viewstate, modify the trace tag in web.config. Set enabled="true" pageoutput="true". You'll see your entire control tree with the viewstate size for each control.

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55618.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3020100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1193.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3020100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.00160
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403020

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.67322
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403020

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles