This article discusses a solution for session utilization and management to avoid filling the memory by unused session data. The article introduces a diagram called "Session Diagram" and a couple of simple classes that can be used with in the Session object to efficiently manage sessions.
When developing an ASP.NET application, it is very common for many developers to use Session for passing data between different pages in the web application. Usually developers tend to leave Session data in memory until the whole session is closed when the user closes the window browser or sign-out; the overall result of this behavior is that Session size accumulates with garbage or orphaned objects as the user navigates in the web application.
Let us assume that we develop a simple e-learning solution, where administrator makes tasks such as "Register Students", "Define Subject", "Define Subject Contents", "Define Exams" , "Publish News". As we can see the tasks varies and some of them can be grouped such as "Define Subject", "Define Subject Contents", "Define Exams" while others such as "Register Students" and "Publish News" can not. In the previous example -and based on what I find developers do- they tend to create session data of objects such as Student, Subject, Exam, News and store them in session. Now imagine the administrator goes to Search Employee screen and selected on to modify his/her data. Then moved to Search Subject screen and selected a Subject and started define its contents then in the exam section of the subject the administrator added a new Exam......etc. more session data are created as we go into the web application. Now the administrator goes to the News section. All subject related session data are garbage now and waste valuable memory space with no need.
- I started thinking that I can utilize
Session.Clear, but the issue is that some session data should be permanent through session life. A good example is the logged user session data that can be a user object with properties that define user roles and permitted activities - this cannot be deleted!
- I started to think of creating a shared class with function like
ClearNewsSession. Then I need to track what page I am in now and what page I came from and start to call these functions one by one. Another disadvantage is that in a team, if a developer created a session data and forget to clear it in the related clear function then it will be kept in memory for the session lifetime.
Session Diagram and Session Item Solution
Let us deepen our vision of the Session object and add more dimensions. The Session can be seen as a collection that holds session data "Session Items". If we add another dimension we can see the Session Item it self as another collection holds children "related" session data "Session Items". The session now has a structure of a tree instead of queue.
Each session data appears in the diagram as a node, related session data can be grouped by a parent. Permanent session data is marked permanent so that they are not removed when clearing session data.
This is translated into actual code by defining
Using the code
A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.
First Create a
SessionItem object in the start of every Session, the following code is added to Global.asax
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Session("MainSessionName") = New HProg.SessionManagement.SessionItem
From ASP.NET viewpoint there is a single session data stored each session under the name "MainSessionName". The first rule is all session data should be created as children of this session data. for example:
Private mSessionItem As HPROG.SessionManagement.SessionItem
SessionItem = Session("MainSessionName")
Some functions have been added to ease use of the object, for example instead of a two line function to add a session data you can use
HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "SessionItemName", _
This is implemented as a shared function in the Sessions object. Further more, you can create a session child session item given a string path as in the next example:
In the attached web demo you can see the following:
t1 is created and its children created under it.
- It is not allowed to create a sessiondata as a child of a nonexisted session such as the sessiondata item named "exception"
HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "t1", _<BR> "t1 value")
HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "t1.t2", <BR> "t2 value")
HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, "t1.t2.t3", _
"t3 value") <BR>Try
HProg.SessionManagement.Sessions.SetSessionItem(Me.Page, _<BR> "t1.ierror.t3", "exception")
Catch e As Exception
- You can directly access a leaf child.
- You can delete Session of a Node or All non-Permanent or All
- You can control your session data
- If multiple developers work in the same application in different modules there is no concern of similar session item names. Every module has its own Tree Node.
- Session Items of a particular module can be all cleared with out the need to clear them explicitly one by one.
Well there is nothing for free. The sessionItem class comes with an additional memory space and more processing. So it is recommended to use it only when application stored complex session data. Also you can use your own ordinary session items together with
SessionItem class in a hyprid model. Remember that
SessionItem is basically just another session data a stored in the ASP.NET Session Object.