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.
catalina.bat
under Windowscatalina.sh under Linux such as:
JAVA_OPTS='-Xms256m -Xmx512m'
-Xms
-Xmx
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.
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.
APR是从操作系统级别来解决异步的IO问题,大幅度的提高性能. (http://apr.apache.org/).
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的代码".优化的前提条件是良好的代码质量和设计.
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!

Windowsispreferredfordesktopcomputingandgamingdueto:1)itsvastsoftwareandgamelibrary,2)user-friendlyandcustomizableinterface,3)extensivehardwarecompatibility,and4)performanceoptimizationcapabilities,despitesomeresource-heavyandupdate-relatedissues.


For years, Linux software distribution relied on native formats like DEB and RPM, deeply ingrained in each distribution's ecosystem. However, Flatpak and Snap have emerged, promising a universal approach to application packaging. This article exami

The differences between Linux and Windows in handling device drivers are mainly reflected in the flexibility of driver management and the development environment. 1. Linux adopts a modular design, and the driver can be loaded and uninstalled dynamically. Developers need to have an in-depth understanding of the kernel mechanism. 2. Windows relies on the Microsoft ecosystem, and the driver needs to be developed through WDK and signed and certified. The development is relatively complex but ensures the stability and security of the system.

The security models of Linux and Windows each have their own advantages. Linux provides flexibility and customizability, enabling security through user permissions, file system permissions, and SELinux/AppArmor. Windows focuses on user-friendliness and relies on WindowsDefender, UAC, firewall and BitLocker to ensure security.

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
