You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by "Doshi, Gunjan" <gu...@merck.com> on 2000/07/17 20:32:42 UTC

Sessions - Memory Constraints

Friends:
When you put a object in the session, does that mean the whole object is
loaded in the memory. 

Because if the whole object is stored in the memory, then the chances of
reaching the peak and getting out of memory errors are high.

Example: Say I have a image and I need to put that in the session. Does that
mean the whole Image object gets stored in the memory for each session
created. 

Or is it that only the reference to the object is loaded in the memory.

Best Regards
Gunjan

Re: Sessions - Memory Constraints

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
"Doshi, Gunjan" wrote:

> Friends:
> When you put a object in the session, does that mean the whole object is
> loaded in the memory.
>

For the purposes of this question, think of a session as a Hashtable
(which is
in fact the way many servlet containers actually implement session
attributes).
The Hashtable itself contains only references to the objects it refers
to -- but
you had to have the object in memory in the first place to "put it" into
the
session.

>
> Because if the whole object is stored in the memory, then the chances of
> reaching the peak and getting out of memory errors are high.
>

Only if you don't have an adequate amount of memory for your app :-). 
In a
previous life I wrote an app that caches monstrous amounts of database
stuff (up
to half a gigabyte) to improve performance -- even on a high performance
server,
buying the extra memory was much cheaper than paying for the development
time to
make the software fast enough without the caching.

You should also design your app to remove things from the session when
you no
longer need them.  Again, this is just like a Hashtable -- if removing
it from
the session removes the last live reference to that object, then it is
available
for garbage collection, just like any other Java object.  This also
happens (in
general) when sessions are invalidated or timed out, so you should also
give
your user a "log out" button that lets your servlet do this immediately,
and
encourage users to leave that way.

>
> Example: Say I have a image and I need to put that in the session. Does that
> mean the whole Image object gets stored in the memory for each session
> created.
>

Depends on how your image object works.  That object itself does indeed
stay in
memory while you have it in the session, but nothing stops you from
creating a
"proxy" object that looks like an Image, but goes and gets the data
(say, from a
database) only when you need it for display purposes.

>
> Or is it that only the reference to the object is loaded in the memory.
>
> Best Regards
> Gunjan
>

Craig McClanahan