aspose file tools*
The moose likes Servlets and the fly likes Proper way to exit a servlet, and release the resources it is using? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Proper way to exit a servlet, and release the resources it is using?" Watch "Proper way to exit a servlet, and release the resources it is using?" New topic
Author

Proper way to exit a servlet, and release the resources it is using?

eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51

If I never really exit my servlet (unless the user closes the browser window), am I failing to do some needed clean up?

I am noticing that my java session, that is handling my tomcat servlet, is gradually increasing its use of memory, throughout the day.
Shutting tomcat down does release the memory.

I believe the problem is my design, but at this point, there is no time for a complete re-design.

I build an html page, using java, and each action is a submit. I did not want to do any work on the client side.
Each submit refreshes the browser session, and reads the submitted data, to determine the next action.
All of the screens, are accessed from exactly the same web address, and each time it knows where it is by reading the data in the hidden fields, and the value of the submit button.

I never actually exit the servlet.

When the submit is executed, and it calls the page again, should I be doing something to release some resources?
What should I be doing?
What about when the user closes the browser window?


Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
You do not exactly exit your servlet. Your container will handle all that. However, if you feel you are using too much memory beecause of the size of your session attributes or so, you just have to cut down on the amount of things you are adding to the session. Also, at certain points in your application you can remove atttributes that are no longer needed. Or if you want to do things when the application is being removed/shut down, you can override the destroy() method.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

eileen keeney wrote:I never actually exit the servlet.

I have no idea what you mean by this -- unless you are putting the code into an infinite loop, or putting the thread to sleep.

When you return from the goGet or doPost method, you are "exiting" the servlet. (And never ever call System.exit()! if that's what you mean.)

Aside from a really bizarre design, it sounds like you have a good old-fashioned memory leak that you need to track down.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

Bosun Bello wrote:if you feel you are using too much memory beecause of the size of your session attributes or so, you just have to cut down on the amount of things you are adding to the session.

I'll disagree and say that you should never make a change because you "feel" something might help. Prove that the session data is a problem first, before acting. (That said, the session is a likely culprit. Carefully analyze how you are using it to prevent unused data from piling up in long-term sessions.)
eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
If I had an old fashioned memory leak, would the memory recover when I did a shutdown and restart of tomcat?
I didn't think it would.

Java is not like C, I don't actually allocate memory, that I have to remember to release.

My concern, (related to my bizarre design), is that each call to the page, has within it a POST, that calls the same exact page.
My concern is that this is behaving more like recursion then like a loop, possibly grabbing more memory each time the form does a POST.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

eileen keeney wrote:If I had an old fashioned memory leak, would the memory recover when I did a shutdown and restart of tomcat?I didn't think it would.

Of course. When the VM shuts down, any memory is released. How could it be otherwise?

Java is not like C, I don't actually allocate memory, that I have to remember to release.

Wow, like, I didn't know that! I'm so glad that you told me!

Seriously, do you really think that that completely protects you from memory leaks?

My concern, (related to my bizarre design), is that each call to the page, has within it a POST, that calls the same exact page.
My concern is that this is behaving more like recursion then like a loop, possibly grabbing more memory each time the form does a POST.

Regardless of whether you "call" the same page, or another page, there is no "recursion". Each request/response cycle is independent.

That's why the session is usually suspect -- it's one of the few areas where stuff can accumulate that's independent of the request life-cycle. Another is the application context. Another is any static data you are accumulating.
eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
Thank you
I was not trying to insult your intelligence, But since I do not do any of my own allocation/de-allocation, and java does its own garbage collection, I am not sure I understand exactly how I can create a memory leak.

So I guess I will do some reading and figure it out.








Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60049
    
  65

eileen keeney wrote:But since I do not do any of my own allocation/de-allocation, and java does its own garbage collection, I am not sure I understand exactly how I can create a memory leak.

Java cannot release memory that is still in use, so memory leaks can occur whenever unused data continues to be referenced.

For example, sticking stuff in the session or application context, and never getting rid of it.

Memory leaks in Java take on a slightly different flavor than in C or C++, but they can still exist.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
1. Analyze what you are putting in session? If something is not needed, remove those session.setAttribute() calls, carefully.
2. Invalidate your session if user is idle for some time - depends on your application.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Proper way to exit a servlet, and release the resources it is using?
 
Similar Threads
Do stuffs when browser closes
screwy problem with sessions/windows
How to make a request from jsp or action class to the new jsp to open in new browser
Tomcat Inactive Session Management
session timeout in login page