Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
Tano_Ortiga
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I created a servlet w/c I am running at my own PC. I configured my webserver such that when I startup my webserver (tomcat), the servlet is automatically loaded. I noticed however, that after several transactions with this servlet the memory usage of JVM in my PC keeps growing. I am worried that eventually this will cause my PC problems (out of memory). This servlet has everything that a servlet should have. I have the "destroy" and also the system.gc at the end. I even assigned every Class wide variables to NULL (not sure if this helped).
can you give me an advice on this ? BTW, i also tried this using apache's jserv. Is there something wrong with the servlet, webserver ?
thanks in advance.
Jonathan
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much memory use growth are we talking about here?
What does your servlet do?
Note that the destroy() method should only be called by Tomcat, and only when the server is shutting down.
Bill

------------------
author of:
 
Tano_Ortiga
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
How much memory use growth are we talking about here?
What does your servlet do?
Note that the destroy() method should only be called by Tomcat, and only when the server is shutting down.
Bill

The "memory leak" plays at the values 4K-8K. There are also times when the memory allocation does not change, but this cases are 1 out of 7 chances. There was never a time when the JVM's memory allocation went down.

I am explictly calling the destroy() method after the servlet has completed a transaction. I suppose this is OK, since the JVM does the same thing implicitly. I also tried removing the call to the destroy method, i got the same results.
The servlet connects to a database via jdbc dbc bridge. All it does is it allows users to update/delete/add records to the database. But I guess it's not what the servlet does that's causing the problem, because I get the same results, when i try to access the initial screen of the servlet. The initial screen does not connect to the db yet. It just prints out a menu.
I read a note in one of the forums here saying that java's gc does not free up memory used by literal strings. Only all memory allocated to objects created with the "new" command are reclaimable by gc. I did the necessary adjustments to comply with this requirement. Still i get the same problem.
Is it possible that the jvm only reclaims memory when memory allocated to it had reached a certain peak already?
pls help. thanks.
Jonathan

 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan,
I also had this memory problem simillar to yours. Finally I figured out that I overlooked to close the database resources like a 'Statement' object / 'Connection' object / 'Resultset' Object etc.
It is always good idea to put all closing of resources at the 'finally' block to make sure they are always closed.
Another tip is I always try to use StringBuffers instead of String objects when I had to combine more than 1 String objects to make a final one. What I do is create a StringBuffer object , then call .append("..") on that and finally use stringBuffRef.toString() to get the final String. I used this especially to create the SQL statements.
regds
maha anna
 
Tano_Ortiga
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My servlet's initial screen does not connect to the DB yet. And yet I am getting the same error. I guess, this leaves our first suggestion not feasible.
StringBuffer? So what your saying is that the JVM has a special handling for StringBuffers. I'll try this one out. and will get back to you.
thank you very much. and let me just say "this site is really helpful."

Jonathan
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"I am explictly calling the destroy() method after the servlet has completed a transaction. I suppose this is OK, since the JVM does the same thing implicitly. I also tried removing the call to the destroy method, i got the same results."
NO NO NO NO NO - Don't do this. The destroy method should not be called by your code. The servlet container (engine) calls destroy when it is about to remove the servlet object from memory. You should override destroy() if your servlet needs to do some clean-up - possibly write to a log or close DB connection pool objects.
Once the servlet engine creates an instance of your servlet, that instance stays in memory to service any number of requests - writing this kind of program requires a shift in viewpoint.
Memory use may grow until your servlet has serviced a few dozen or more requests due to various objects that the servlet engine creates, so don't worry about it unless you see a LONG term memory growth.
As Maha Anna said, it is essential to close() all database related objects - forgetting to do this is a well known cause of memory leaks and strange behaviour. Also, the try {} finally {} construct is your friend in this.
Bill

------------------
author of:
 
Tano Ortiga
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Basically, what you're saying is this 4k increases in memory allocation of the JVM each time the servlet services a client is an ordinary event. Whew! thought it was a memory leak. OK then, I guess my question would be just how long is "LONG" in your statement? thanks!
Jonathan
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Well there certainly is a mem leak if your are opening too many db connections and arent closing them.. thereby keeping them alive.. well I dunno if the web servers / App servers have a facility to timeout the connections but wen the connections are growing the system goes down since the Servlet Engine and the Db Engine do not have or rather fight for memory.
Please check that.
Regds
Gautham Kasinath
 
Tano Ortiga
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I am using connectionpool method in this servlet. And everything seems well in place as far as the DB connection is concerned. As i have mentioned earlier, the problem occurs even if the servlet has not accessed the DB connection yet. I am positive that its not the DB connection that's causing this.
BTW, have tried using the StringBuffer instead of String, it did not work. I am still getting the same results.
Are there any reports about Vectors having problems during MEM clean up? Because I think that's where my problem is. This servlet makes use of the HTML package. This package enables me to code the HTML forms, documents much like in a manner as if I were coding an applet (make use of add(), settext(), etc). All HTML tags are placed in containers (Vectors).Then, once the HTML codes are completed (printed in the container), it prints it out the output stream (CGI).
Because i tried out the same servlet without making use of the HTML package. And it does not have this problem.
Pls help. thanks
Jonathan
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Are there any reports about Vectors having problems during MEM clean up? Because I think that's where my problem is."
The Vector class is very reliable, however, improper use of Vectors can cause memory leaks. Does this HTML package you are using re-use objects or does it start fresh with each HTML page?
Bill
 
Tano Ortiga
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It starts fresh with every HTML page. I noticed one thing though, the objects created by this HTML package do not have methods for destroying itself. I am not sure if the gc() function can clean-up mem space allocated by this package using the "no reference logic". could this be the culprit in my code?
thanks!
Jonathan
 
Tano Ortiga
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took this from a book. This is the hello world snippet. Pls try this out and verify the mem usage of the JVM in your machine. In my machine, it grows by 4K each time I try accessing the servlet (try accessing it several times with 1/100 second interval to see mem growth at larger paces).
It's very disappointing to know that all along the modifications i've been doing have nothing to do with the MEM leak issue. :-( I should have tried this long time ago.
Pls let me know if this problem ("MEM leak") is not what it seems. I need to have some assurance that this servlet would not bog down the server if the webserver is left online for sometime and there are many users accessing the site.
thanks for all your help, guys. appreciate it.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic