Java Program Performance Optimization tips

Source: Internet
Author: User

1, optimize the circulation body
If you have a lot of loops, the problem with bad code handling in the loop will be magnified.

 for(intI=0;I<list.size();I++){}Optimize to intsize=list.size(); for(intI=0;I<size;I++){} for(intI=0;I<100000;I++){if (i%= =9) {//Every loop  is executed one time}} is optimized to for(intI=0;J=Ten;I<100000;I++;J--){if (j==0) {//Each loop  is executed one time j=;}}

2, less use new to initialize an instance
The time-consuming of new objects is often time-consuming for local variable assignments, and the system spends a few hours of garbage collection and processing when the object is built.
For example:
NewObject object = new NewObject ();
int value;
if (value>10) {
Value = Object.getvalue ();
}
Optimized for
int value;
if (value>10) {
NewObject object = new NewObject ();
Value = Object.getvalue ();
}
3. Exception Handling
Exception handling is bad for performance, and throwing exceptions is the first thing to create a new object, and to do the related processing, resulting in system overhead. So exceptions should be used in the wrong handling situation and should not be used to control the process.
4. Use local variables as much as possible
Local variables, the parameters passed when the method is called, and the creation of temporary variables are saved in the stack (stack), faster, other variables into static variables, global variables are created in the heap (heap), slower.
Extension: The stack is allocated by the system, faster, the stack uses a first-level cache, they are usually transferred to the storage space, the call is immediately released, the heap is new allocated memory, prone to memory fragmentation, but easy to use, heap storage in the level two cache, the life cycle is determined by the virtual machine garbage collection algorithm.
5. Minimize I/O operations
6. Use cache as much as possible
BufferedReader replaces the reader,bufferedwriter instead of the writer,bufferedinputstream instead of the inputstream for I/O processing can achieve performance improvements.
7, try not to apply synchronization, servlet is multithreaded if you use too much synchronization will lose the advantages of multithreading
8, can not save too much information in httpsession, but when the session storage is very large, the need for memory is high.
9. Eliminate session and manually release resources when the user exits the system

Java Program Performance Optimization tips

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.