Home> System Tutorial> LINUX> body text

Analyze N ways to optimize Tomcat performance

PHPz
Release: 2024-06-14 21:10:00
Original
707 people have browsed it

Analyze N ways to optimize Tomcat performance

Tomcat is often the default development environment for JavaWeb practitioners, but the default configuration of Tomcat as a production environment, especially the configuration of memory and threads, is very low by default and can easily become a performance problem. Bottleneck. Fortunately, Tomcat still has a lot of room for improvement. The following introduces Tomcat optimization, which can be divided into memory, threads, and IO.
Tomcat is often the default development environment for JavaWeb practitioners, but the default configuration of Tomcat as a production environment, especially the configuration of memory and threads, is very low by default and can easily become a performance bottleneck.

Fortunately, Tomcat still has a lot of room for improvement. The following will introduce Tomcat optimization, which can be divided into memory, threads, and IO.

1: Tomcat memory optimization, tell the JVM that I want a large memory when starting (tuning the memory is the most direct way)

catalina.bat

under Windows

catalina.sh under Linux such as:

JAVA_OPTS='-Xms256m -Xmx512m'
Copy after login

-Xms JVM initialization heap size

-Xmx The maximum value of the JVM heap. The actual parameter size depends on the server configuration or project specific settings.

2: Tomcat thread optimization in server.xml such as:

maxThreads="X" means that up to X connections can be processed simultaneously

minSpareThreads="X" Initialize X connections

maxSpareThreads="X" means that if there can be at most X threads, once more than X, the threads that are no longer needed will be closed

acceptCount="X" When the number of people connected at the same time reaches maxThreads, you can still queue, and the queue size is X. If it exceeds X, it will not be processed.

Three: Tomcat IO optimization

1: Synchronous blocking IO (JAVA BIO) Synchronization and blocking, the server implementation mode is one connection one thread (it’s scary to think about it, threads are very precious resources), of course it can be improved through the thread pool mechanism .

2: JAVA NIO: It is divided into synchronous non-blocking IO, asynchronous blocking IO and BIO. The biggest difference is one request one thread. The same thread can be reused to process multiple connections (multiplexing).

3: Asynchronous non-blocking IO (Java NIO2 is also called AIO). The main difference with NIO is the underlying difference of the operating system. It can be compared to express delivery. NIO means that after online shopping, you have to go to the official website to check the express delivery. Check whether it has arrived (maybe multiple times), and then pick up the express yourself; AIO means the courier delivers the goods to your door (you don’t need to pay attention to the progress of the express).

The BIO method is suitable for architectures with a relatively small and fixed number of connections. This method has relatively high requirements for server resources and concurrency is limited to applications. It was the only choice before JDK1.4, but the program is intuitive, simple and easy to understand.

The NIO method is suitable for architectures with a large number of connections and relatively short connections (light operations), such as chat servers. Concurrency is limited to applications and programming is complex. JDK1.4 begins to support it.

The AIO method is used in architectures with a large number of connections and relatively long connections (heavy operations), such as photo album servers. It fully calls the OS to participate in concurrent operations. The programming is relatively complicated, and JDK7 begins to support it.

In server.xml

Realize IO switching for Tomcat.

Four: Big Killer APR
APR是从操作系统级别来解决异步的IO问题,大幅度的提高性能. (http://apr.apache.org/).
Copy after login

APR (Apache Portable Runtime) is a highly portable library, which is the core of Apache HTTP Server 2.x. It can be better integrated with other local web technologies and generally makes Java more efficient as a high-performance web Server platform rather than simply as a backend container.

In a production environment, especially when using Tomcat directly as a WEB server, Tomcat Native should be used to improve its performance. Without APR, basically 300 threads will be full soon, and future requests will have to wait. .But after adding APR, the number of concurrent threads has dropped significantly. It may drop from the original 300 to only a few dozen, and new requests will come in without blocking.

When tested in a LAN environment, even 400 concurrencies are processed/transmitted in an instant. However, in a real Internet environment, the page processing time only accounts for less than 0.1%, and most of the time is spent on the page. Transmission. If APR is not used, one thread can only process one user at the same time, which will inevitably cause blocking. Therefore, it is very necessary to use apr in a production environment.

Install the Apache Tomcat Native Library and support apr (http://tomcat.apache.org/native-doc/) when started directly. It is based on APR. For specific installation methods, please refer to other blogs and articles. Troubleshoot code problems Tomcat is optimized to this level and can meet most performance requirements.

最后一句话"再牛B的服务器,也顶不住一个傻B的代码".优化的前提条件是良好的代码质量和设计.
Copy after login

The above is the detailed content of Analyze N ways to optimize Tomcat performance. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!