• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Memory leak - Buffer size problem?

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?


 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic