Java programming: How to improve performance? (Simple summary of the article)

Source: Internet
Author: User

Developers in programming in addition to the programming specifications, but also pay attention to performance, in Java programming, what is the best way to improve performance?

This article is transferred from the domestic ITOM industry leader OneAPM Cloud Insight (a solution that can gracefully monitor a wide range of operating systems, databases, middleware, cloud hosts) beauty engineer Chen Yongmei Jane Book.

Developers in programming in addition to programming norms, but also pay attention to performance Oh, today on the work of some problems encountered a simple summary, I hope the siege lion to criticize.

As a QA, from the quality management of the knowledge gained:

    • One line of code can solve the problem try not to use multiple lines, the more lines of code the higher the maintenance cost, the more the probability of defects;

    • Writing a single line of code might take only 30s, and finding and optimizing a single line of code produces a performance problem that requires 72h or more.

Do not say, the following is a brief discussion of the actual programming process performance-related issues:

1. Reduce the number of objects created in the loop logic

For example:

for (int i = 0;i < list.size; i + +) {

...

}

Should be replaced by:

for (int i = 0,int len = list.size (); i < Len; i + +) {

...

}

2, pay attention to the loop structure whether there are multiple access network, database and other operations

Principle: To be able to complete a one-time query to avoid multiple queries.

Before optimization:

After optimization:

3. Poor tolerance of objects

Principle: Do not directly convert the use of Jsonobject object when the parameter is passed, if the value does not exist the method will report an exception cause the program to abort, as far as possible to JavaBean or MAP, otherwise you should first determine whether the value exists. It is best to receive parameters in the JavaBean package, and the parameters are initially compared, and the data integrity cannot be given to the front-end processing.

If you use the Jsonobject object, you should first make an existing judgmentif(json.contains(“xxx”))

4. Reasonable design of the information that needs to be cached

Principle: Login function to analyze what is closely related to the user, such as session information, which is related to business system properties, such as System configuration resource information. The latter should be cached to avoid building access information every time the login is made.

5. Order of execution of business logic

Principle: In the process of logical judgment, try to delay the execution of the complex operation, do not advance, such as login operation, only after the successful login to do the relevant resources load, query operations;

Example: static resources should be as far back as possible, it is recommended that the business logic to comb the first negative affirmation, that is, the first column does not meet the business conditions.

Before optimization:

After optimization:

6, Try...catch relative consumption performance is serious, minimize the use of frequency, use to maximize the scope of action, prohibit in the cycle and other operations.

7, create similar arrays, such as the size of the object can be specified, as far as possible to specify the size, minimize the waste of memory space.

8, HashMap read traversal use entry, instead of first take the key set, and then value.

9. The object created in the method is best used to point the reference to NULL after the use is complete, which facilitates GC garbage collection.

10. Array replication uses system.arraycopy () to reduce the use of methods to create new array assignments.

11, the method size should not exceed 80 lines, the method stack is too deep to cause memory overflow, try not to have too few classes, class maintenance also requires performance overhead.

12. Creating complex objects should use the Clone method to reduce the overhead of constructing method calls.

13. To be Continued ...

Cloud Insight integrates monitoring, management, computing, collaboration, and visualisation into a data management platform that helps all IT companies reduce their human and time cost inputs to system monitoring, making operations more efficient and simple.

Java programming: How to improve performance? (Simple summary of the 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.