22

Is there some kind of Session size limitation or advisable value to not to surpass ?

In my web application I create a few DataTables to store user selections which are stored in session until user approves selections so I add those values to database.

The problem is that I don't know whether session is reliable enough to keep few objects in or not ?

Thanks!

more info

Session size is about 10-20KB max.

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
eugeneK
  • 10,750
  • 19
  • 66
  • 101

4 Answers4

14

Here's a few notes about Session state:

  • InProc (mode="InProc") session state is limited to the amount of memory available to the worker process. Only object references are stored, not the objects themselves.

Out of process state management serialises objects before persisting them:

  • Out of Process state management using the session state server (mode="StateServer") is limited to the amount of memory available to the state service.

  • Out of Process state management using SQL Server (mode="SQLServer") is bound only by the maximum size of the SQL image data type or the maximum permitted size of the database.

Obviously there still has to be enough memory available to the worker process to be able to pull an out of session object back into memory and re-hydrate for the duration of the http request.

As I mentioned previously, out of process state management serialises objects before persisting them.

This means that objects must be serialisable which excludes, for example, XmlDocument or anything that inherits from MarshalByRef.

Attempting to serialise objects of this sort will result in the following exception:

Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.

Kev
  • 118,037
  • 53
  • 300
  • 385
  • 2
    How does this answer the question? – Liam Aug 29 '14 at 08:26
  • 1
    What is *limited to the amount of memory available to the state service* supposed to mean? – Liam Aug 29 '14 at 08:34
  • 1
    @Liam - OP clearly hasn't read up on session state management. I'm explaining the options available, is that such a bad thing, expanding OPs knowledge with relevant same topic information? Per second question. The state service is an out of process service, it persists session state in memory (not on disk or in a SQL database) so therefore will be limited by the amount of memory available to it as a process, this is important if you're running a 32bit state service on a machine with potentially other memory pressures. – Kev Aug 29 '14 at 13:35
  • @Liam That's the answer I was looking for as well. Currently, my state service is using 850mb of memory. – Tomas Beblar Aug 05 '20 at 23:53
13

Yes, it is reliable enough. It just isn't very scalable, so plan ahead. This will totally grind to a halt when you run it on more than 1 server.
And there is sort of a limit: Number-of-concurrent-Users * SizeOf-Session < Available-Mem

It depends of course on the size of the tables, storing a few kB is usually acceptable (although high traffic sites will try to keep it smaller).

If your users can share tables, then you can place that data in the Application object, a great saving.

And a session object is limited to the TimeOut setting, default is 20 min. One way to optimize memory consumption is reducing that, but that is a trade off with user convenience.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    thanks, so no special size limits ? I need to store there few kbs per user. Few small tables and so... Session is Out of Proc and timeout is pretty large. Data is stored until user finish the form... So no need to long timeout. – eugeneK May 16 '10 at 10:09
  • I thought of using ViewState instead but view state is already large enough so at least some data will come from server and some from in page data. – eugeneK May 16 '10 at 10:25
  • 8
    An obvious limit on 32 bit application pools is 1.3GB total virtual memory usage, and you should make sure your sessions do not consume too much of it. – Lex Li May 16 '10 at 22:47
  • @LexLi Does such limits apply to Application level state and web cache as well in case of 32-bit applications? I believe Application level state and web cache also take space in the main memory of the process itself with only difference that they aren't user scoped in terms of access. Isn't it? – RBT Jan 19 '18 at 23:00
2

I am assuming that you are having the session stored in "inProc" mode. In this mode, ASP.NET applications' session, cache etc are stored in the web server's RAM (via aspnet_wp.exe process). And .NET does not get to use all of it. There is a setting in machine.config which tells the threshold limit (by default 60%). Once this threshold is reached, IIS will recycle the worker process, and all the session information is lost.

Note that, if your server hosts more than one asp.net application, the 60% of memory is to be shared by all apps. So if the cumulative memory usage reaches the threshold the worker process still gets recycled.

Alternative to this, besides optimizing your application to use session sparingly,is to set the application to use session in out of process mode (using a stateserver or sqlserver to store session information).

Out of process mode can bring down your system performance.

Refer to this article for more information on Session State Management.

Srikanth Venugopalan
  • 9,011
  • 3
  • 36
  • 76
1

You should always assume session is a very valuable storage and very limited. The consumption should be as little as possible, because you can never know how many users the application is going to support.

DataTable can be too large to store in sessions, unless it can be kept small enough.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • 2
    thanks but i know that I need size, at least some basic rules. Not just as small as possible. – eugeneK May 16 '10 at 09:56
  • @eugeneK Mine is currently sitting at 850Mb. So at least that much. – Tomas Beblar Aug 05 '20 at 23:54
  • @TomasBeblar Your response will be appreciated, did you manage to store that much size of data in session i.e. approx. 850MB, how did you manage to store that size of temp data in session or any alternate solution? – ramya Oct 08 '20 at 18:09
  • @yadavr Running multiple sites in web farm mode with about 15 process all sharing the state server. Currently at 450mb memory usage on the state service. No issues so far. – Tomas Beblar Oct 09 '20 at 20:37