views:

157

answers:

5

multiple webapp running on same tomcat using same jvm. sometime, one webapp that have memory leak will cause entire jvm to crash and affect other webapps. any recommendation how to isolated that without need to use multiple jvm and tomcat

+1  A: 

Are the applications running as separate processes? Or the same one?

First off you should look at profiling to find the memory leak http://stackoverflow.com/questions/1716597/java-memory-leak-detection-tools.

However, as a quick solution from inside you could use Runtime.getRuntime().totalMemory() to see how much memory is in use, and if it grows above a certain limit, and you know which app is causing the problem, you could restart that app.

You could also try running System.gc() which is a terrible way to do it, and really shouldn't be used as it can be ignored by the JVM.

jex
@jex, thank you for answering but my question is not to address the memory leak issue. i just wanna see whether possible to isolate different webapps on same tomcat/jvm
cometta
+3  A: 

Within the same JVM everything shares the the same memory. There is no system to allocate separate pools or quota.

If one of your applications behaves really badly in this regard, the only thing you can do is run it isolated in a separate JVM (separate Tomcat).

Thilo
+1 If you know which webapp is causing the memory leak, it is better to run it in a separate JVM so it can be monitored separately
JoseK
i agreed on this. but just thinking of might have alternative way..
cometta
+1  A: 

To the best of my knowledge, the short answer is: No, it can't be done. Tomcat uses a single memory space for all running apps.

My knee-jerk response is that you should fix the memory leak rather than trying to isolate the misbehaving app. Cure is better than quarantine. As I don't know the details of your problem, maybe this isn't practical for some reason.

Jay
+1  A: 

You can't isolate apps in the same JVM (though you can do things like instrument a particular apps ClassLoader for diagnostics)

If your concern is administration/configuration though (and not total memory consumption) you can run multiple instances of Tomcat off the same install by using catalina.home and catalina.base

jayshao
+1  A: 

JSR 121 was designed to solve this, but it hasn't been implemented yet.

instantsetsuna