2009-06-24 22 views
2

私は、ファイルの表記法で多く動作するJava Webアプリケーションを持っています。
サーブレットコンテナとしてTomcat 6を使用しています。多くのリクエストが送信されると、Tomcatは非常にメモリが不足します。私はtomcatを微調整してメモリ消費を減らす方法を知りました。 サーブレットコンテナを変更することも検討しています。
あなたは何をお勧めしますか?TomcatのメモリとCPU使用量を調整する

+0

あなたは、「ファイルの規則に多くの作業」とはどういう意味ですか? –

答えて

4

conf/server.xmlの設定では、受け入れられた/動作可能な接続番号を制限できます。

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" 
    maxThreads="16" minSpareThreads="1"/> 

<Connector executor="tomcatThreadPool" 
      port="8080" protocol="HTTP/1.1" 
      connectionTimeout="20000" 
      redirectPort="8443" 
      /> 

や設定ファイルの

<Connector port="8080" protocol="HTTP/1.1" 
      connectionTimeout="20000" 
      redirectPort="8443" 
      maxThreads='16'/> 

を持って、これはあなたブレーキ必要があります。

編集:あなたのコメントに基づいてあなたのCPU数(Runtime.getRuntime().availableProcessors())に応じてサイズの専用スレッドプールに処理を移動することができます(ExecutorServiceExecutorsを参照してください。)次に、保留中の数を絞る有界LinkedBlockingQueueを適用することができタスク(キューがいっぱいになると、ブロッキングの追加を行うためにRejectedExecutionHandlerを指定することを忘れないでください)。

編集2:クラスへのリンクが追加されました。そこにいくつかのサンプルがあります。

編集3:プロジェクトで使用したサンプルメソッド。

/** 
* Creates a new thread pool based on some attributes 
* @param poolSize the number of worker threads in the thread pool 
* @param poolName the name of the thread pool (for debugging purposes) 
* @param priority the base priority of the worker threads 
* @param capacity the size of the task queue used 
* @return the ExecutorService object 
*/ 
private ExecutorService newPool(int poolSize, 
String poolName, final int priority, int capacity) { 
    int cpu = Runtime.getRuntime().availableProcessors(); 
    ExecutorService result = null; 
    if (poolSize != 0) { 
     if (poolSize == -1) { 
      poolSize = cpu; 
     } 
     if (capacity <= 0) { 
      capacity = Integer.MAX_VALUE; 
     } 
     result = new ThreadPoolExecutor(poolSize, poolSize, 
       120, TimeUnit.MINUTES, 
       new LinkedBlockingQueue<Runnable>(capacity), 
     new ThreadFactory() { 
      @Override 
      public Thread newThread(Runnable runnable) { 
       Thread t = new Thread(runnable); 
       t.setPriority(priority); 
       return t; 
      } 
     }, new RejectedExecutionHandler() { 
      @Override 
      public void rejectedExecution(Runnable r, 
        ThreadPoolExecutor executor) { 
       if (!executor.isShutdown()) { 
        try { 
         executor.getQueue().put(r); 
        } catch (InterruptedException ex) { 
         // give up 
        } 
       } 
      } 
     }); 
    } 
    return result; 
} 

そして、あなたはそれをこのように使用することができます

ExecutorService exec = newPool(-1, "converter pool", Thread.NORM_PRIORITY, 500); 
servletContext.setAttribute("converter pool", exec); 

そして、あなたのサーブレットで

ExecutorService exec = (ExecutorService)servletContext 
.getAttribute("converter pool"); 

exec.submit(new Runnable() { 
    public void run() { 
     // your code for transformation goes here 
    } 
} 
関連する問題