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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version
