caching: Session state or Application state...

S

spacehopper_man

I'm considering ditching all use of Session state in favour of
Application state.

This is because - from what I can work out - it will be more memory
efficient for me.

I have three questions:

1) When is memory used for Session State freed (or essentially freed)?
- if ever...

2) When is memory used in Application State (or essentially freed) - eg
- if my cache item expires due to it reaching it's expiration time - is
it freed immediately - or must the process wait for some cleanup job to
run.

3) does anyone know of any docs/articles on when *exactly* memory is
made available when using the various caching techniques?

cheers very much,
Oli.
 
S

sdbillsfan

It seems to me that you have no basic understanding of session or
application state or you wouldn't be asking these questions and/or
considering "ditching all use of Session state in favour of Application
state".

http://msdn.microsoft.com/library/d.../en-us/cpguide/html/cpconapplicationstate.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconsessionstate.asp

You really need at least this fundamental knowledge if you plan on
doing (successful) asp.net development.
 
J

John Timney \( MVP \)

I'm considering ditching all use of Session state in favour of
Application state.

This is because - from what I can work out - it will be more memory
efficient for me.

If your only sharing data at an application scope, and need no session
independence it makes sense! Thats a judgement call on your part. Be aware
that overloading any of the state objects can be a bad thing.
I have three questions:

1) When is memory used for Session State freed (or essentially freed)?
- if ever...

When the session ends and the systems decides that all objects now out of
scope can be freed and garbage collected, I dont recall there being a fixed
duration as to when this happens. When the garbage collector decides that
sufficient garbage has accumulated that it is efficient to do so, it
performs a collection to free some memory.
2) When is memory used in Application State (or essentially freed) - eg
- if my cache item expires due to it reaching it's expiration time - is
it freed immediately - or must the process wait for some cleanup job to
run.

When a cached object expires, the ASP.NET process removes it behind the
scenes immediately unless an active request for that cache item is
different to the retrieved value at request time and it needs to be updated,
then it updates the object if its priority is high enouigh to merit keeping
it cached. However, the type of cahcing and the expiration policy can see
the cache object reacting in different ways, for example if your using
sliding expiration, or SQL cache invalidation. If you keep adding items
to the cache and it gets full (ie. the free cache memory is exhausted),
ASP.NET will automatically invalidate and removes the oldest, least used, or
lowest priority items from the cache, hence you may not see your object
behave as predictably as you think it might. You need to be aware of this
if your going to use it heavily. Its worth having a read of Alex Homes
excellent article on this:
http://www.devx.com/dotnet/Article/27865/1954?pf=true

3) does anyone know of any docs/articles on when *exactly* memory is
made available when using the various caching techniques?

Not exactly, this is an easy read though and you can deduce when the memory
would be freed. Only tip I guess is to watch perfromance monitor to see
exactly whats happening.

http://msdn.microsoft.com/asp.net/archive/?pull=/library/en-us/dnvs05/html/cachingnt2.asp

also read about GC here

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenetchapt05.asp

cheers very much,
Oli.


--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
S

spacehopper_man

I have more than a basic understanding -

I have been doing web development now for over 11 years.

I have worked in first class companies in the City and around the
world.

I have a degree in Physics, and an MSc in Computer Science and
Artificial Intellegence.

I am lead developer at an industry leading company.

I feel Applicate state may be more appropriate in my situation because
from what I can gather it offers more flexibility and control over
memory management than Session state does. I can "roll my own" session
state management on top of Application state if this is true...

I do not have to worry about cross machine scaling issues as I am
working in a Portal environment.

I appreciate your efforts at helping me sdbills - but I have the hump
slightly about your answer - sorry dude ;)
 
S

spacehopper_man

ok thanks john - the more I look at this the more I think that
Application State will be more efficient in my case.

There's still one key piece of info I'm missing though - and that is:
*exactly* when is memory made available in these two scenarios - and
how efficient are the processes that do the freeing - (ok that's two
points..)

you say "immediately" for Application state - but that seems unlikely
unless there is a resource intensive process doing some very close
monitoring. More likely I'd say is that there is a periodic monitoring
process - that runs ...say...every 5 secs or so - but I can find no
info on the exact mechanism. It's possible there is an event based
system that does as you say - it'd be great to learn more in this
case....

ok - thanks again for your help.
O.
 
S

spacehopper_man

....just to clarify - I understand well the garbage collection process,
and that it is difficult to determine exactly when memory will be
freed. What I am not clear on is when the memory is made available for
garbage collection - and I mean in more depth that "when it times
out"...

cheers all,
O.
 
J

John Timney \( MVP \)

I dont think I have the answer you are perhaps looking for!

A session ended, is ended - even if the session object has not been
collected as a managed object its reference at session.end (timeout for
example) is destroyed as an accessible object and the session object is then
subject to garbage collection. When exactly does the freeing occur, thats
pretty much impossible to say as its determined by heuristic rules inside
the garbage collector and uses something called non-deterministic
finalization to action events, which means that you cant exactly be really
sure when the garbage collector will action as it depends on what else is
happening in memory and how the heaps are being allocated and managed.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top