aspose file tools*
The moose likes Servlets and the fly likes Memory leak - Buffer size problem? 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 "Memory leak - Buffer size problem?" Watch "Memory leak - Buffer size problem?" New topic
Author

Memory leak - Buffer size problem?

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Hello.

I think I might have found a memory leak in my code.

I have an autocomplete text field (using Ajax) to search for persons stored in the database. When the user is searching a person he types in a text string, i.e,. the first letter in the persons name. The ajax script is calling a servlet, which returns users from the database. The servlet makes a concatenated string which is returned to the ajax script.

One client has more than 10000 persons stored in the database. After searching a user you submit to another page. Sometimes I have experienced that the next page is showing the long concatenated string of persons that was created in the servlet. It' seems that the buffer is full. Why is the next page showing the concatenated string being created in the servlet from the previous page?

I guess the buffer size may be to large, taken all the memory when few users use the autocomplete field?!

I haven' t found out if I can clear the buffer somehow and if I can set the JSP buffer size to a fixed value. Maybe there is another problem in my code?

I have attached the servlet code which is returning the concatenated string of persons from the database.

I hope you can help me to see, if I have any memory leaks in the way I make my code.

I appreciate your help.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
You seem to be missing a VERY important point.

ALL of those instance variables will be shared by all requests to that servlet. Using private does not do a thing.

Any number of requests could be in progress "at the same time."

You need to study servlet life cycles and memory management, then completely redesign your approach.

Bill

Java Resources at www.wbrogden.com
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Thank for your comment. I will try study it.

I know that Private variables is shared across all requests. But I am actually initiating the variables later inside the try block for each request. I' am always initiating variables inside the "public void service" method. Is that not the way to do it?



How is it possible by another request (thread) to get the same variables?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

There is only one copy of each servlet and therefore one copy of each instance variable. Two threads running at the sae time will be stomping all over the shared data.

As William pointed out, you need to redesign the servlet to use no instance variables.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
So if we initialize all variables (that may not be shared with other requests) in a init method, then we are thread safe?


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Of course not.

Instance variable can never be thread-safe because there is only one copy to be shared amongst all the concurrent threads. Where and how you initialize the vairables makes not one iota of difference.

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
So if we move all instance variables inside the service method, we are still not thread safe? Thread 2 could actually still change a variable in thread 1? So we need to synchronized the variables that has to bed thread safe?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

No, if you move the variables into a method, they are thread-safe. Instance variables will not be.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
Note that if you are extending HttpServlet, the usual style is to implement the various methods associated with HTTP protocol methods rather than override the service method.

The HttpServlet class has a service method which detects the HTTP request type - GET, POST, etc and directs the request accordingly. You can certainly write your own service method but then you are not taking advantage of the HttpServlet base class.

Check out the JavaDocs for javax.servlet.http.HttpServlet

Bill
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
What is the problem by using service instead of i.e. doGet?

After removing the instance variables the memory leak seems to dissapear. Can anyone explain why declaring instance variables could lead to the memory leak?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
What is the problem by using service instead of i.e. doGet?


As I said, the usual approach takes advantage of the built in service method. Here is the code which you are overriding (Tomcat 6.0.18).



Note that this is not trivial stuff you are discarding.
Bill
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Regarding the multi thread issue in the previous discussion I understand that we shouldn't declare istance variables in the servlet.

But what about declaring instance variables in another class, which is called from the servlet? Please see the example below.

If two theads are requesting MyServlet at the same time, are there any risk that thread 2 could overwrite the public instance variables initiated in the class called SessionHandler, when called from the servlet as below?

Please note that a local instance of SessionHandler is created in the servlets doGet method.

Do you see any problems in the code below in an application with many requests?


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Memory leak - Buffer size problem?
 
Similar Threads
ObjectStream client-server object sharing problem.
Instance variables
Ajax autocomplete
Java & Outlook integration - how to go about it
Input redirection in Java