[英]Tuning Tomcat memory and cpu consumption
I have a Java web application that works a lot with file conventions. 我有一个Java Web应用程序,可以很好地处理文件约定。
I am using Tomcat 6 as my servlet container. 我正在使用Tomcat 6作为我的servlet容器。 When many requests are submitted, Tomcat becomes very memory hungry. 当提交许多请求时,Tomcat变得非常消耗内存。 I wonder how I can fine-tune tomcat to reduce the memory consumption. 我想知道如何微调tomcat以减少内存消耗。 I am also considering changing my servlet container. 我也在考虑更改我的servlet容器。
What do you suggest? 你有什么建议?
You can limit the accepted/operational connection numbers in the conf/server.xml
configuration. 您可以在conf/server.xml
配置中限制可接受/可操作的连接数。
Have 有
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="16" minSpareThreads="1"/>
and 和
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
/>
or 要么
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxThreads='16'/>
in the config file and this should brake you. 在配置文件中,这应该会刹车。
Edit: Based on your comment you could move the processing into dedicated thread pool sized according to your CPU count ( Runtime.getRuntime().availableProcessors()
) (see ExecutorService and Executors .) Then you could apply a bounded LinkedBlockingQueue to throttle the number of pending tasks (don't forget to specify a RejectedExecutionHandler to do the blocking add when the queue gets full). 编辑:根据您的评论,您可以将处理移到根据您的CPU数量确定大小的专用线程池中( Runtime.getRuntime().availableProcessors()
)(请参阅ExecutorService和Executors 。)然后您可以应用有界LinkedBlockingQueue来限制数量待处理任务(不要忘记指定RejectedExecutionHandler来在队列已满时进行阻塞添加)。
Edit 2: Added links to the classes. 编辑2:添加到类的链接。 There you find some samples. 在那里您可以找到一些样本。
Edit 3: A sample method I used in a project. 编辑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;
}
And you could use it this way: 您可以通过以下方式使用它:
ExecutorService exec = newPool(-1, "converter pool", Thread.NORM_PRIORITY, 500);
servletContext.setAttribute("converter pool", exec);
And in your servlet 并在您的servlet中
ExecutorService exec = (ExecutorService)servletContext
.getAttribute("converter pool");
exec.submit(new Runnable() {
public void run() {
// your code for transformation goes here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.