File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes java.lang.OutOfMemory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "java.lang.OutOfMemory" Watch "java.lang.OutOfMemory" New topic
Author

java.lang.OutOfMemory

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

Sometimes i get an error caused by the OutOfMemory-error and when i do another request to Tomcat (with smaller amount of data and thus also memory) i get no error anymore (so without restart of Tomcat).
Sometimes i get an error caused by the OutOfMemory-error and can't do a thing because i get always error caused by The OutOfMemory-error.

What's the logic between these cases? Is there a solution to prevent Tomcat for going out of memory (e.g. when you do a tomcat-startup with -Xmx512m and then tell Tomcat he isn't allowed to take new requests - a new request is definied as request of a new browser - if he's using 500m, or something like that)?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
I suspect that what happens is:
In case 1 the error in the requst Thread was caught and Tomcat managed to recover memory used by the request and was thus able to continue.
In case 2 Tomcat was not able to recover the memory.

The solution is generally proper programming technique that avoids "memory leaks" and algorithms that try to do too much in one request. Database operations that return a gazillion results for example.

In server.xml you can configure the number of connections that Tomcat will accept, etc.

Bill
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

Originally posted by William Brogden:
I suspect that what happens is:
In case 1 the error in the requst Thread was caught and Tomcat managed to recover memory used by the request and was thus able to continue.
In case 2 Tomcat was not able to recover the memory.

The solution is generally proper programming technique that avoids "memory leaks" and algorithms that try to do too much in one request. Database operations that return a gazillion results for example.

In server.xml you can configure the number of connections that Tomcat will accept, etc.

Bill


Thanks for the reply.

I have been profiling this web-app and no memory leaks where found. And problem is: initial request must gain a lot of info from a database table so an image could be generated, there is no other solution for (as i see it of course)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Wow - so your image generation program needs hundreds of megabytes?
Here are the thoughts that occur to me just off the wall...
JDBC query related objects are bulky - can you extract the data to a more compact form (maybe even a temp file) and make sure all the JDBC objects are closed and gc-ed before you start generating the image.
You can control the number of instances of the rendering class (possibly even make it a singleton) to prevent a large number of requests causing the memory error.
Bill
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

we have spoken about my app in this thread. So we keep a lot of info in memory about our objects, to have acceptable response-times and which each new browser there are only 2 requests to database and everything that's needed is put in memory, because else it's highly inperformant (approx 1 minute to generate new map if for each request app must access database - approx 5-6 secs tot generate new map if objects are in memory)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Oh yeah, I remember your problem now.
Apparently throwing the out of memory error can leave your application in a state it can't recover from.
It sounds like you need a way to recognize a low memory situation and prevent new requests from being processed if there is too little free memory. What is a typical pattern of memory use - does it build up gradually or does the application have to grab a huge chunk all at once?
You could refuse a request with a "try again later" message if more than N requests are being processed or the free memory is low.

Bill
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

how can i check the amount of free memory ?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Ah - you need to get familiar with the methods in java.lang.Runtime.
We have totalMemory() maxMemory() and freeMemory() at our disposal to watch the JVM's memory use. Calls look like:

long free = Runtime.getRuntime().freeMemory();

You should probably try to get the gc to run before running the above.
Runtime.getRuntime().gc();

Bill
[ November 26, 2004: Message edited by: William Brogden ]
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

i gave JVM -Xmx512m

but JVM get's the memory in small parts, so that's huge problem, because when i say that there must be at least 150 MB free to take an init-request, this condition is never fulfilled. If you know what i mean
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Well, why not just start it with the maximum - I think that is the
-Xms512m
command line parameter. That way you should not get the incremental free memory change, it should all be there to start with.
Bill
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

that's new to me i'll try that one

Thanks. now i am gonna
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
All of those command line parameters are documented in your SDK distribution - look under the ../docs/tooldocs/ directory
There are many settings there that may help with your memory problem - for example you can get a log of garbage collection events.
Bill
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

I added the -Xms512m option, but if i try shutting down the Tomcat server i got this error:

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Ah - the thing is, executing the shutdown runs another JVM instance in order to communicate with Tomcat. If that JVM sees your environment variable with the high memory setting you run out of memory.
You need some way to execute the shutdown with a different JAVA_OPTS setting - maybe a separate set of batch files?
Bill
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

soooooooooo logical, but didn't think of that one.
Thanks for the advice
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.OutOfMemory
 
Similar Threads
OutOfMemory error
OutOfMemory
OutOfMemory Error
OutOfMemory
outOfMemory