Sometime, int web application we need to store data in session excessively. This data probably not related to the current user and not being used through out the application. It may be process specific or page specific. The question arise here that if data is page specific than why to store in session, why can't it be stored in viewstate. Ofcourse, we can store data in Viewstate but storing large data in ViewState is not good idea because of following reason:
- Data travels alongwith page HTML between client and server. If same data is bound to any grid or control, data travels in twice of the size.
- If data is in user defined object or datatable/dataset, it requires to serialize and deserialize before stroring and after retrieving from the ViewState.
Above both cases are expensive with respect to performance and should always avoid. Always getting data from database or any service may also be expensive with respect to performance, so we are compelled to stored.
In most of the scenarios, SQL Server is used to manage the sessionstate. Anydata being stored in session must be serializable. For any object being stored/retrieved from session, serialization and deserialization happens respectively. All session data retrieved and stored back in sessionstate server for each request. It happens in following events.
- Application_AcquireRequestState: This event occurs when session state being retrieved and stored in collection to use during the process of the page.
- Application_ReleaseRequestState: Occurs when session state being stored back in the starage.
Therefore, if there are lots of data in session, each request process will impect the performance. Generally, I have seen that data stored in session to process a page not cleared after completion.
I am explaining a way to handle this situation and clear the unwanted session data pertaining to this scenario. We can create a class which keep the track of session variables.
Join the conversation! Your thoughts help the community grow.