2009-06-24 4 views
2

파일 규칙을 많이 사용하는 Java 웹 응용 프로그램이 있습니다.
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 
    } 
} 
관련 문제