aspose file tools*
The moose likes Servlets and the fly likes Possible memory problems from using sessions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Possible memory problems from using sessions?" Watch "Possible memory problems from using sessions?" New topic
Author

Possible memory problems from using sessions?

Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
Hi all,
I am new to Java and am trying to develop my first web site with it. Some things I am doing have me concerned about memory problems with the server. I'll try to explain this well and hopefully someone will follow what I am doing (and have suggestions too)!
1) I created a class that queries a database and loads the rs into an ArrayList. It immediately closes the connection afterwards. The arrayList is then loaded into the session. As I like to have only 5 results per page displayed this makes it really fast to change pages and go through all the results. The db should never return more than about 30-40 results at most, and each result consists of a file name (for an image), 2 small strings and 1 fairly large string. Out of curiosity, I took a test result set that had 40 results and stripped out all the HTML that gets added, so what was left over is what would be in the ArrayList. I copied it to notepad and then checked the properties...it said it was 13kb's in size. Is this too large an object to store in a session?
2) I'm wondering what exactly happens with regards to object instances that are being created. The way I have things set up now, a user accesses my jsp page which in turn calls the class that gets the results from the db. The first 5 results are shown and the rs is loaded into the ArrayList. Then if the user clicks a page link at the bottom it calls the same jsp page to process this again. Each time the jsp page is called it makes a new instance of the class that it calls. Is the previous instance destroyed, or is it still hanging around? If it's still hanging around is there a way to destroy it? Next, the class checks to see if an ArrayList is loaded into the session and if it finds one it skips the db connection and just figures out what the next results are it should display and sends them to the jsp page, so I think this part is all ok.
Sorry if this sounds confusing. I am just worried that I may be using to much server resources and could run into trouble if the site gets a lot of hits at the same time. At most, I don't think this site would ever get anymore than about 50k hits per month, and I am planning on hosting it on a shared server.
Any advice is much appreciated!
Pat
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
1. As far as the API is concerned, there is no limit to the size of objects stored in a session, but it is a good thing to worry about from a practical standpoint.
2. You can free up the memory by calling removeAttribute on the session if you can tell when the user is finished.
3. You can set a more aggressive timeout than the usual 30 min and let the servlet engine clean up.
4. If it was me, I would just monitor memory usage first to see if there was a problem. I like to add an administrator's servlet
that can report current memory usage.
Bill
------------------
author of:
Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
Thanks for the quick reply. I will look into making an adminstrative servlet so I can see if any memory problems are beginning. I like the idea of being able to monitor that and didn't know I could.
As for the session timeout, that is one thing I was thinking about. I may set it for 10-15 minutes instead since even if it does time out then the program will just make another db connection and recreate it.
I'm glad to hear that this doesn't seem to be a bad way to go then, thanks again!
Pat
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
It also depends on the application server you are using, and the number of users you expect to support.
To give an example of the former, if you are using the distributed server features of IBM WebSphere, it will persist the sessions in a database for failover purposes. According to Enterprise Java Programming with IBM Websphere (recommended) this doesn't work well if the sessions are over 2 to 4KB in size.
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Possible memory problems from using sessions?