【问题标题】:Tuning Tomcat memory and cpu consumption调整 Tomcat 内存和 CPU 消耗
【发布时间】:2010-11-05 10:06:54
【问题描述】:

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

【问题讨论】:

  • “大量使用文件约定”是什么意思?

标签: java memory tomcat servlets performance


【解决方案1】:

您可以在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);

在你的 servlet 中

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

exec.submit(new Runnable() {
    public void run() {
        // your code for transformation goes here
    }
}

【讨论】:

    猜你喜欢
    • 2014-09-19
    • 1970-01-01
    • 2011-10-03
    • 2011-02-20
    • 2017-10-01
    • 1970-01-01
    • 2015-11-03
    • 2017-09-10
    • 1970-01-01
    相关资源
    最近更新 更多