Java Performance Optimization Principles: Code Performance, Memory recycling, application configuration ( The main reason for Java programs is garbage collection)
Code-Layer Optimization: Avoid excessive loop nesting, invocation, and complex logic
Tuning content:
1 , increase the maximum number of connections
2 , adjust the mode of Operation
3 , enable gzip compression
4 , adjusting the JVM memory size
5 , as a Web server, integrates with Apache or Nginx
6 and reasonable selection of garbage collection algorithm
7 , try to use a newer JDK version
production configuration example:
<connectorport= "8080" protocol= "Org.apache.coyote.http11.Http11NioProtocol" maxthreads= " " minsparethreads= " " maxsparethreads= " " acceptcount= " " disableuploadtimeout= "true" connectiontimeout= "20000" uriencoding= "UTF-8" enablelookups= "false" redirectport= "8443" compression= "on" compressionminsize= "1024x768" compressablemimetype= "Text/html,text/xml,text/css,text/javascript"/ >
Parameter description:
Org.apache.coyote.http11.Http11NioProtocol: Adjust the working mode to NIO
There are three modes of operation: Bio, NiO and Apr
Bio (Blocking I/O): Default operating mode, blocking I/O operation, no optimization technology processing, performance is relatively low.
Nio (New I/O or non-blocking): non-blocking I/O operations, with bio having better concurrency processing performance.
APR (Apache portable Runtime,apache Portable Runtime): Preferred mode of operation, primarily for upper-level applications to provide an underlying support interface library that can be used across multiple operating system platforms.
Tomcat leverages the APR library tomcat-native to achieve operating system level control, providing an optimized technology and non-blocking I/O operations that greatly improve concurrency processing power. However, the APR and tomcat-native libraries need to be installed.
Knowledge of network I/O models involved:
blocking I/O Model: when the application process calls the RECV function system call, if the data waiting to be manipulated is not sent to the kernel buffer, the application process is blocked and cannot receive other requests. Conversely, the kernel recv side buffer has data, the kernel will copy the data to the user space unblocked, continue to process the next request. (Kernel space (buffer)-User space (System call))
non-blocking I/O Model: The application process is set to non-blocking mode, and if the data to be manipulated is not sent to the kernel buffer, the RECV system call returns an error, and the application process continuously checks to see if the operation is ready by polling, and if there is data in the buffer, the I/O operation does not block the application process. The new request will continue to be processed.
I/O multiplexing model: blocking occurs on select/poll system calls, rather than blocking on actual I/O system calls. Can handle multiple operations at the same time and check if the operation is ready, the Select/epoll function discovers that the data is ready and copies the data into the buffer of the application process through the actual I/O operation.
asynchronous I/O Model: The application process notifies the kernel to start an asynchronous I/O operation and allows the kernel to notify the application process after the entire operation (including the data copy buffer) is complete, while the new request continues to be processed.
I/O operations are divided into two phases: The first phase waits for data to be available, and the second phase copies the data from the kernel to the user space.
the difference between the first three models: The first-stage blocking I/O blocking is on I/O operations, non-blocking I/O polling, and I/O multiplexing is blocked on Select/poll or Epoll. The second stage is the same. Both phases of asynchronous I/O do not block the process.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7F/02/wKiom1cPZiexhqQVAADEfIyyX4s521.png "title=" 1.png " alt= "Wkiom1cpziexhqqvaadefiyyx4s521.png"/>
MaxThreads: Maximum number of threads, default 150. Increasing the value avoids too many queue requests, resulting in a slow response.
Minsparethreads: Minimum number of idle threads.
Maxsparethreads: The maximum number of idle threads, and if this value is exceeded, the useless thread is closed.
Acceptcount: When the processing request exceeds this value, the subsequent request is placed in the queue waiting.
Disableuploadtimeout: Disable upload time-out
ConnectionTimeout: Connection timeout, per millisecond, 0 for unlimited
Uriencoding:uri address encoding using UTF-8
Enablelookups: Turn off DNS resolution to increase response time
Compression: Enable the compression feature
Compressionminsize: Minimum compression size, unit byte
Compressablemimetype: Compressed file types
Tuning the JVM Memory Size:
added java_opts= '-xms512m-xmx1024m-xx:permsize=128m-xx:maxpermsize=256m ' in catalina.sh
-XMS JVM Initial minimum heap memory, default to physical memory 1/64, do not set too large, otherwise increase the recovery time (suspended application), relatively low frequency, on the contrary, high frequency.
-xmx JVM Maximum allowable heap memory size, default to physical memory 1/4
-xx:permsize JVM initially allocates non-heap memory size
-xx:maxpermsize JVM Maximum allowable allocated non-heap memory
involving Heaps (HEAP) and non-heap (non-heap) Memory Knowledge:
heap memory is a storage object, such as a class instance, an array, and so on. memory outside the heap in the JVM is called non-heap memory. Can be understood as non-heap memory left to the JVM for its own use.
An object is stored in the heap, and the stack is a reference to the base data type and the objects in the heap. In the stack, an object corresponds to a 4byte reference.
The stack is the unit of the runtime, and the heap is the unit of storage.
gzip Compression Effect: Save server traffic and increase website access speed. After the client requests the server resource, the server compresses the resource file and returns it to the client, which is extracted and browsed by the client's browser.
Garbage collection involves knowledge:
Garbage Collection ( GC,garbage Collection) algorithm:
1. Mark - Purge algorithm (mark-sweep)
Divided into two stages, marked and cleared. The collector marks all referenced objects from the root node, marks the available objects, and then performs a purge on unmarked objects. The reclaimed space is discontinuous. The disadvantage is that the entire application is paused, and fragmentation is generated.
2. replication Algorithm (copying)
Divide the memory into two pieces, use only one piece at a time, and when garbage is collected, copy the tagged object to another block, and then completely erase the memory that was used. The space after the copy is contiguous. The disadvantage is that it takes two blocks of memory space.
3. Mark - Collation Algorithm (mark-compact)
combined tagging - clears and replicates two algorithm advantages. Also divided into two stages, the first phase marks all referenced objects starting from the root node, the second stage traverses the entire heap, clears unmarked objects, and compresses the surviving objects into one of the heaps and discharges them sequentially. This method avoids the fragmentation problem of Mark-and- sweep, and also avoids the space problem of the replication algorithm.
Garbage collector Technology:
1. Serial Collection
It is easy and efficient to use single-threaded processing for all garbage collection work, but it is not possible to use multi-processor advantages, so this is suitable for a single processor server, the amount of data ( around 100M) is relatively small and the response time is not required for the scene.
2. Parallel Collection
Use multithreading to handle garbage collection work, fast and efficient. In theory, the more processors, the better the performance. Suitable for large data volumes and no requirement for response time scenarios.
3. Concurrent Collection (CMS )
Front two when garbage collection is working, the entire app needs to be paused, and the pause time is determined by the heap size. Use multithreading to scan the heap memory, mark the objects that need to be recycled, clear the flagged, and in some cases pause the app. Ideal for scenarios where data volumes are large, multiprocessor, and response times are demanding.
4. G1 GC
split multiple memory into separate areas, and then garbage collection. When memory is freed,G1 can also compress the free heap memory.
which garbage collector to choose depends on the scenario, hardware resources, and throughput. Generally use CMS.
Specify garbage collection technology (catalina.sh java_ops specified):
-XX:+USESERIALGC serial Garbage collector
-XX:+USEPARALLELGC parallel Garbage collector
-XX:+USECONCMARKSWEEPGC concurrent Tag scan garbage collector
-xx:parallelcmsthreads= concurrent Tag scan garbage collector = number of threads used
Print garbage Collection information:
-xx:+printgc
-xx:+printgcdetails
-xx:+printgctimestamps
-xloggc:filename
Use Apache integrates with Tomcat because tomcat handles static files far less than Apache, so that Apache handles static files,Tomcat processing dynamic jsp files can effectively improve the processing speed. It also involves a problem, how to save the Session?
Tomcatsessionid three ways to persist:
Session stickiness: bind SessionID via browser Cookie, assign the same Session request to the same through sticky mode Tomcat on.
Session copy:Tomcat synchronizes the session to other tomcat nodes via broadcast , and The open broadcast address is manually opened under Linux. Not easy back end node too much
Session Save Database (memcache,redis): save SessionID in a shared database.
There is often a memory overflow error message, why?
These two conditions are met to throw a memory overflow: JVM 98% time is spent in memory reclaim and memory per recycle less than 2%
1. Build too many objects, resulting in insufficient heap memory (heap memory size is limited by physical memory, virtual memory).
2, the code design is unreasonable, occupy the memory can not be collected in time.
This article is from the "Li Zhenliang Technology Blog" blog, make sure to keep this source http://lizhenliang.blog.51cto.com/7876557/1763866
Tomcat Performance Optimizations