views:

27

answers:

2

I'm trying to understand when I can put certain objects into a users session and am wondering how the session is stored and values retrieved from it. If I make a request to pull Key A from the session state will it also read Key B?

I know that viewstate is stored as one big object andn I am going ot assume that it is then accessed from my code once it's been entireely loaded. Is this similar for session state data or does it only load the keys that are requested form the server.

So if my state is 20KB and I want to get a value that's 5KB from it will it read all 20KB or just the 5KB that I need?

+2  A: 

By default, session state is stored in memory until the session expires (a period of inactivity from a given user). The view state is not stored at all between requests, but is actually sent to the page as a hidden form field. This data is sent back to the server on subsequent requests.

To answer your question, the default behavior is that the entire session is ALREADY loaded so whether or not you actually access it, it is there and in memory.

There are several options for managing this however, and you can find an excellent reference here:

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

free-dom
ViewState is placed in a hidden field, not Session State. Session State remains in memory on the server, that's why when using a server farm, session might be lost if the user is bounced to another server on a subsequent request, or when the servers app pool is refreshed.
NerdFury
Sorry, I read incorrectly. You did say view state.
NerdFury
A: 

By default, a users session is stored in memory. You could configure it to be stored in the database, but it is serialized, and read out completely when re-instantiated.

So yes, if you have 20KB worth of data in your users session, it will always use 20KB of memory.

NerdFury