Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java performance Optimization Notes (1) Overview

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Performance reference metrics:

Execution time: the execution time of the program from start to finish.

CPU time: the cpu time occupied by a function or thread.

Memory allocation: the amount of memory occupied while the program is running.

Disk throughput: hard disk Istroke O usage.

Network throughput: network usage.

Response time: the response time made by user behavior, the shorter the performance, the better.

Short board principle:

How much water a barrel can hold depends on the board with the shortest wall.

The resources that create the deficiency are:

Disk Ipicuro: in many applications, most of the bottleneck lies in the hard disk, which is much slower than memory. Waiting for disk read and write completion will waste a lot of CPU time and drag down the entire system.

Network request: because network instability or congestion control will reduce the efficiency of network request, if you wait for the end of the network request or the timeout is set too long, it will waste a lot of CPU time and drag down the whole system.

CPU: some operations take up a lot of CPU directly or continuously, and CPU-intensive applications will create bottlenecks on CPU.

Program exceptions: for java applications, it is very resource-consuming for exceptions to generate and process exception stacks, and high-frequency exceptions will drag down the whole system.

Database: relational database will cause bottlenecks in disk read operation (that is, disk Imax O operation), updating index, lock waiting and competition.

Lock contention: the interlocking competition between highly concurrent application threads and thread context switching are all very expensive.

Memory: memory uses nand flash to read and write very fast, but if the application and operating system fill up the memory, it will lead to the use of swap partitions, and the exchange of memory data with hard disk partitions will cause a bottleneck.

The basic solution to the above bottleneck:

Disk Icano: using NIO, zero copy, mmap mapping and other technologies, try to read and write to the hard disk asynchronously, using cache and buffer technology. Use SSD hard drives and RAID technology to improve write performance.

Network requests: CDN or PCDN acceleration, caching, merging requests, compressing messages, asynchronous network requests (message queuing service, master-worker, etc.) as far as possible, using NIO. Increase the bandwidth and upgrade the network card.

CPU: continuously optimize the code, use many threads or processes, design a reasonable architecture, split the business, reduce complex operations on a single machine, and avoid mixing complex operations with business requests.

Exception: handle boundaries, buffers, etc., especially to avoid the occurrence of NullPointException. Testing time is longer than development time to reduce the probability of bug occurrence.

Database: optimize sql statements, pay attention to the execution plan, use caching instead of some database functions, use a reasonable storage engine according to the situation, pay attention to locks, use connection pooling, and use PrepareStatment.

Lock competition: minimize lock competition, you can use lock-free copy-on-write, reentrant lock and other technologies.

Memory: optimize the code, analyze the memory usage, and configure gc reasonably. Increase physical memory.

Amdahl's law:

Speedup formula: speedup = time before optimization / time after optimization

Speedup ≤ 1 / (F+ (1murf) / N)

-Speedup: acceleration ratio

-F: serialization specific gravity

-number of N:CPU

According to Amdahl law, the effect of optimization depends on the number of CPU and the proportion of serialization programs in the system. The more the number of CPU is, the lower the serial ratio is, the better the optimization effect is.

Performance tuning level

Design tuning: sort out the overall structure, find out the shortcomings for optimization, and make appropriate use of design patterns and past experience. The system is designed in detail.

Code tuning: familiar with the principles of basic API and third-party class libraries, select the most appropriate and best algorithm, simplify the implementation, interface-oriented.

JVM tuning: familiar with jvm memory model, bytecode and other deep technology, using common monitoring tools such as visualVM, Jconsole, etc. Perform pressure test, adjust gc and stack size.

Database tuning: tuning sql statements, analyzing execution plan, tuning database system configuration (buffer, shared area, connection pool, etc.), tuning database design (redundancy, index, database and table, read-write separation, etc.)

Operating system tuning: adjust the number of handles, turn off useless services and ports, adjust shared memory, adjust swap partition size, and so on.

Finally, you need to pay attention to: do not optimize because of optimization!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report