views:

121

answers:

2

I have a web app that is being served through Tomcat. My app lets users submit their own workflow which will then be executed on the server. My problem is how to control how much memory each user is taking up on the server. I think Tomcat itself runs apps in a sandbox of its own. I say this because when my app runs out of memory and crashes, Tomcat still keeps running. How does Tomcat sandbox the app (or does it)? Does it run the app on a separate JVM?

On a related note, a similar question about controlling thread CPU and memory usage has been asked before. The solutions suggested were not acceptable for me and I'd like to believe that Tomcat has a different mechanism. But for those who are interested, http://stackoverflow.com/questions/1202184/throttling-cpu-memory-usage-of-a-thread-in-java

Thanks!

A: 

Tomcat doesn't really Sandbox web apps. The apps run in the same JVM. The only thing that might isolate an app is the classloader. Each webapp has its own classloader so it doesn't share app specific classes with other apps.

When you run out of memory, the container is out of memory also. The only reason it's running is probably because it doesn't need to allocate more memory.

ZZ Coder
A: 

Ok, I used to think in a multithreaded application if one thread runs out of memory and crashes, all the other threads will also crash. I wrote a class to test this and it seems I was wrong - the other threads will continue to run. Come to think of it, I don't know why I used to assume other threads will crash. Why on earth would they? Stupid of me!

package test;

import java.util.ArrayList;

public class ThreadTest
{
 public void startMemoryHungryThread()
 {
  Runnable runnable = new Runnable(){
   public void run()
   {
    ArrayList<String> array = new ArrayList<String>();
    while(true)
    {
     array.add(new String());
    }
   }
  };
  Thread thread = new Thread(runnable);
  thread.start();
 }

 public void startSleepyThread()
 {
  Runnable runnable = new Runnable(){
   public void run()
   {
    while(true)
    {
     System.out.println("Sleepy thread still sleeping");
     try
     {
      Thread.sleep(1000);
     }catch(Exception e)
     {
      e.printStackTrace();
     }
    }
   }
  };
  Thread thread = new Thread(runnable);
  thread.start();  
 }

 public static void main(String[] args)
 {
  ThreadTest thread = new ThreadTest();
  thread.startSleepyThread();
  thread.startMemoryHungryThread();
 }
}
tilish