Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help, java memory leak somewhere!!!  RSS feed

 
Zayka R
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are running Linux RedHat7.1 and resin 2.0.3 on our production servers with jdk 1.3
We constantly get java.lang.outofMemory errors. The number of java threads is set to 30 right now. I upped the memory allocated for java threads to 512 from 256 and it helped a little two and a half weeks we haven't recieved on out of memory alert. However, today I got it again.
Doe anyone know what to do and how to find the memory leak?
Thanks!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take a moment to reread our display name policy and change your display name to include a first and last name. Thanks.
To find a memory leak - well, your best course of action in the long run is probably to get a good memory analysis tool like JProbe. This will cost money though. As a cheaper alternative, you can often get useful data just by running java -verbose:gc, which will print out reports on all garbage collection activities as your program runs. It doesn't tell you exactly which objects are collected (and which aren't), but it can be useful in helping you figure out when the memory rises - and when it falls.
You can also get good info from a class like this:

This allows you to display memory usage from anywhere in your code by calling Mamory.report(). You can modify the System.out.println() to use some other logging mechanism instead, since it's particularly useful to correlate memory increases with other events as they are logged. And System.out can be a messy way to do this.
Also, it's going to be pretty hard to figure out what's going on with hundread of threads running. But if you have a memory leak for hundread of threads, you will most likely have the same sort of leak with just one thread. Try testing your system this way first, and see if you can detect a leak. If not, then try two threads - that's enough to introduce the additional complexites of multithreading, yet still have a chance of keeping the log file understandable.
Good luck...
 
Charlie Sturman
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just learning java so take this with a big grain of salt.While at the local bookstore I skimmed over something about jdk1.3 not being able to gc dead thread objects even when there are no references to them.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Zayka:
We are running Linux RedHat7.1 and resin 2.0.3 on our production servers with jdk 1.3
We constantly get java.lang.outofMemory errors. The number of java threads is set to 30 right now. I upped the memory allocated for java threads to 512 from 256 and it helped a little two and a half weeks we haven't recieved on out of memory alert. However, today I got it again.
Doe anyone know what to do and how to find the memory leak?
Thanks!

In the Tomcat documentation it is mentioned that Suns javac compiler, used by Tomcat to compile JSPs, has memory leaks. Possibly Resin is having the same problem. One workaround is to only deploy precompiled JSPs.
 
Sean Sullivan
Ranch Hand
Posts: 427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source:
http://www.ddj.com/documents/s=888/ddj0002l/0002l.htm
{{
"While Java's garbage collection mechanism removes much of the difficulty of
managing dynamic memory, problems can still occur. Most nontrivial Java
programs will have some loitering objects present in them. Loitering objects
are generally fewer than memory leaks in C++, but when they do occur they
generally cause much larger problems. Removing loitering objects can be
difficult because Java handles memory in a fundamentally different way than
C++, which is what most developers are familiar with. You have to think
about managing the edges, not the nodes on the heap. A thorough
understanding of object lifecycles, including lifetimes and state values, is
key and must be used to build good memory-management practices into
development practices."
}

See also:
http://industry.java.sun.com/javaone/99/pdfs/e618.pdf
http://www-106.ibm.com/developerworks/library/j-leaks/index.html
 
Amanda Song
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programmers are out of Luck! I have programed in Microsoft Territory and had enough headaches with their memory leaks. Now I found out Java, if not worse, also have tons of problems : memory leaks, numerous bugs in SUN's Java packages...
Maybe we should go back and code in ASSEMBLY Language!
 
Sam Kebab
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Zayka
I don't think the problem is with Java or the webserver -- but the design and coding of the program. So even if you increase your memory to 4GB you will eventually reach memory out of bounds etc.
In order to find the "leak" you have to:
1) find the references to the objects you created
2) whenever possible, and you know you no longer need the object -- set it to null.
3) If you are using jsp/servlets watch out for creating objects in ServletContext -- these objects persist until eternity. If in Session -- you can control the Max period or directly call invalidate() when you know the session must really be over. Whenever possible go local: use Request, PageContext etc. -- they do not persist.
4) If you are doing a lot of database work: watch out for the objects Connection, Statement, and SQL String. As much as possible learn to contol the creation and use of Connection. In one program I created I used only One Connection for the entire 4mg worth of source code. If a Statement can be reused reuse it, when done set it to null(This one i had to learn the hard way --my program also went memory out of bounds -- i was creating 300 Statement objects per minute without setting it to null -- so when i went to work the following day -- lo and behold my computer crashed.

Good luck. Happy object hunting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!