Search code examples
asp.netperformancesessionobjectpagespeed

what's better : create new object every time OR store it in session?


Lets take an example of a table adapter (typed data-sets)

I am storing data-set in session. but I don't know what is better for table adapters.

In general, will creating object hit performance badly when compared to storing and getting from session ?


Solution

  • The two alternatives uses different resources.

    Creating a new instance of an object uses time. Storing the instance in the session uses memory.

    If you create a new instance each time you need one, it will only take up memory for the short while that you are using it. If you store it in the session, it will take up memory all the time, and if you don't remove it from the session it will even take up memory for a while after the user has left.

    If you were thinking of keeping the table adapter connected to the database while you keep it in the session, that would also use up a connection to the database, which is a resource that is even more limited. That would seriously limit the number of users that the site could handle.

    Generally you should only store things in the session that actually has state, i.e. something that really has data that you need to keep. Most objects take so little time to create that there is no point in keeping them just to avoid creating them again.