调整Tomcat内存和CPU消耗

我有一个Java Web应用程序,它适用于文件约定。
我使用Tomcat 6作为我的servlet容器。 当提交了许多请求时,Tomcat变得非常渴望内存。 我想知道如何微调tomcat以减少内存消耗。 我也在考虑更改我的servlet容器。
你有什么建议?

您可以在conf/server.xml配置中限制接受/操作的连接数。

  

  

要么

  

在配置文件中,这应该制动你。

编辑:根据您的注释,您可以将处理移动到根据您的CPU数量( Runtime.getRuntime().availableProcessors() )resize的专用线程池(请参阅ExecutorService和Executors 。)然后您可以应用有界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(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); 

在您的servlet中

 ExecutorService exec = (ExecutorService)servletContext .getAttribute("converter pool"); exec.submit(new Runnable() { public void run() { // your code for transformation goes here } }