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

What is the method of Java code optimization

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is the method of Java code optimization". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the method of Java code optimization".

First, the objectives of code optimization are:

1. Reduce the size of the code

2. Improve the efficiency of code running

Some of the content of this article comes from the Internet, some from daily work and study, of course, this is not important, the important thing is whether the details of these code optimizations are really useful. That article will be updated for a long time, and will be updated irregularly whenever there are code optimization details worth sharing.

Second, code optimization details:

1. Specify final modifiers for classes and methods as much as possible.

Classes with final modifiers are not derivable. In the core API of Java, there are many examples of using final, such as java.lang.String, where the whole class is final. Assigning a final modifier to a class makes the class uninheritable, and assigning a final modifier to a method makes it impossible for a method to be overridden. If you specify a class as final, all the methods of that class are final. The Java compiler will look for opportunities to inline all final methods, which play an important role in improving the efficiency of Java. For more information, see Java runtime optimization. This can improve performance by an average of 50%.

2. Reuse objects as much as possible

In particular, when using String objects, you should use StringBuilder/StringBuffer instead of string concatenation. Because the Java virtual machine not only takes time to generate objects, it may also take time to garbage collect and process these objects in the future, so generating too many objects will have a great impact on the performance of the program.

3. Use local variables whenever possible

The parameters passed when the method is called and the temporary variables created in the call are saved in the stack, which is faster, while other variables, such as static variables, instance variables, and so on, are created in the heap and are slow. In addition, the variables created in the stack are gone as the method ends, and no additional garbage collection is required.

4. Close the stream in time

In the process of Java programming, you must be careful when you connect to the database and operate the I _ big O stream, and close it in time to release resources after using it. Because the operation of these large objects will cause great overhead of the system, a little carelessness will lead to serious consequences.

5. Minimize the double calculation of variables

Make clear a concept, the call to the method, even if there is only one sentence in the method, there is consumption, including creating stack frames, protecting the scene when the method is called, restoring the site when the method is called, and so on. So for example, do the following:

It is recommended to be replaced by:

In this way, when list.size () is very large, it reduces a lot of consumption.

6. Try to adopt the strategy of lazy loading, that is, create it only when needed.

For example:

It is recommended to be replaced by:

7. Use anomalies with caution

Exceptions are bad for performance. The first step in throwing an exception is to create a new object. The constructor of the Throwable interface calls a local synchronization method named fillInStackTrace (), and the fillInStackTrace () method checks the stack and collects call trace information. Whenever an exception is thrown, the Java virtual machine must adjust the call stack because a new object is created during processing. Exceptions can only be used for error handling and should not be used to control program flow.

8. Do not use try in loops. Catch... It should be placed on the outermost layer

According to the opinions put forward by netizens, I think this point is open to question.

9. If you can estimate the length of the content to be added, specify the initial length for the underlying collections and tool classes implemented in an array.

For example, ArrayList, LinkedLlist, StringBuilder, StringBuffer, HashMap, HashSet and so on. Take StringBuilder as an example:

You can set its initialization capacity through the constructor of the class (not just the StringBuilder above), which can significantly improve performance. For example, StringBuilder, length represents the number of characters that the current StringBuilder can hold. Because when StringBuilder reaches the capacity of * *, it will double its current capacity and add 2. Whenever StringBuilder reaches its capacity of * *, it has to create a new character array and copy the contents of the old character array into the new character array-a very performance-consuming operation. Just imagine, if you can estimate that there are about 5000 characters to be stored in the character array without specifying the length, the second power closest to 5000 is 4096, regardless of the 2 added per expansion, then:

On the basis of 4096, apply for a character array of 8194 sizes, which is equivalent to applying for a character array of 12290 sizes at a time. If you can specify a character array of 5000 sizes at the beginning, it will save more than twice the space.

Copy the original 4096 characters into a new character array

In this way, it not only wastes memory space but also reduces the running efficiency of the code. Therefore, there is nothing wrong with setting a reasonable initialization capacity for the underlying collections and utility classes implemented in arrays, which will bring immediate results. Note, however, that HashMap is a collection implemented as an array + linked list, so don't set the initial size to the same size as you estimate, because the possibility of joining only one object on a table is almost zero. The initial size is recommended to be set to the N power of 2. If you can estimate that there are 2000 elements, you can set it to new HashMap or new HashMap.

10. When copying a large amount of data, use the System.arraycopy () command

11. Multiplication and division use shift operations

For example:

Using shift operation can greatly improve performance, because at the bottom of the computer, alignment operation is the most convenient and fastest, so it is recommended to modify it as follows:

The shift operation is fast, but it may make the code difficult to understand, so * add the appropriate comments.

12. Don't keep creating object references within the loop

For example:

This practice will result in the existence of count Object object references in memory. If the count is very large, memory will be consumed. It is recommended to change to:

In this way, there is only one Object object reference in memory, and each time new Object (), the Object object reference points to a different Object, but there is only one copy in memory, which saves a lot of memory space.

13. For the sake of efficiency and type checking, array should be used as much as possible, and ArrayList should be used when the array size cannot be determined.

14. Try to use HashMap, ArrayList and StringBuilder. Unless thread safety requires, Hashtable, Vector and StringBuffer are not recommended. The use of synchronization mechanism results in performance overhead for the latter three.

15. Do not declare the array as public static final

Because this is meaningless, it just defines the reference as static final, and the contents of the array can be changed at will. Declaring the array as public is a security vulnerability, which means that the array can be changed by external classes.

16. Try to use singletons on appropriate occasions

The use of singletons can reduce the burden of loading, shorten the time of loading, and improve the efficiency of loading, but it is not applicable to singletons everywhere. To put it simply, singletons are mainly applicable to the following three aspects:

(1) Control the use of resources and control the concurrent access of resources through thread synchronization

(2) to control the generation of examples in order to save resources.

(3) Control the sharing of data and enable communication between multiple unrelated processes or threads without establishing a direct association.

17. Try to avoid using static variables at will

You know, when an object is referenced by a variable defined as static, gc usually does not reclaim the heap memory occupied by the object, such as:

At this point, the life cycle of the static variable b is the same as that of class A, and if class An is not unloaded, the B object referenced by B will reside in memory until the program terminates.

18. Clear sessions that are no longer needed in a timely manner

To clear inactive sessions, many application servers have a default session timeout of 30 minutes. When the application server needs to save more sessions, if there is not enough memory, the operating system will transfer some of the data to disk, and the application server may dump some inactive sessions to disk according to the MRU (most frequently used recently) algorithm, or even throw an out-of-memory exception. If the session is to be dumped to disk, it must first be serialized, and serialization of objects is expensive in large clusters. Therefore, when the session is no longer needed, the invalidate () method of HttpSession should be called in time to clear the session.

19. Collections that implement RandomAccess interfaces, such as ArrayList, should be traversed using the most common for loop instead of the foreach loop

This is recommended by JDK to users. JDK API's interpretation of the RandomAccess interface is that the implementation of the RandomAccess interface is used to show that it supports fast random access. The main purpose of this interface is to allow general algorithms to change its behavior so that it can provide good performance when applied to random or consecutive access lists. Practical experience shows that if the class instance that implements the RandomAccess interface is randomly accessed, it is more efficient to use a normal for loop than to use a foreach loop; conversely, if it is accessed sequentially, it is more efficient to use Iterator. You can make a judgment using code similar to the following:

The underlying implementation principle of the foreach loop is the iterator Iterator, see Java syntax sugar 1: variable length parameters and foreach loop principle. So the second half of the sentence, "conversely, it is more efficient to use Iterator if it is accessed sequentially" means that those class instances accessed sequentially use foreach loops to traverse.

Use synchronization code blocks instead of synchronization methods

This has been made clear in the article "synchronized Lock method Block" in the multithreaded module. Unless you can determine that a whole method needs to be synchronized, try to use synchronization code blocks to avoid synchronizing those codes that do not need synchronization, thus affecting the efficiency of code execution.

21. Declare the constant as static final and name it in uppercase

This allows you to put this into the constant pool during compilation to avoid calculating the value of the generated constant at run time. In addition, naming the name of a constant in uppercase can easily distinguish between a constant and a variable.

22. Do not create objects that are not used or import classes that are not used.

This is meaningless. If "The value of the local variable i is not used" or "The import java.util is never used" appear in the code, please delete these useless contents.

23. Avoid using reflection when the program is running

For, see reflection. Reflection is a powerful function that Java provides to users, and powerful function often means inefficient. It is not recommended to use reflection mechanisms, especially Method's invoke method, frequently during program running. If necessary, it is recommended that classes that need to be loaded through reflection instantiate an object through reflection and put it into memory when the project starts-users only care about getting the fastest response when interacting with the peer, not about how long it takes for the peer to start the project.

Use database connection pooling and thread pooling

Both pools are used to reuse objects, the former to avoid frequently opening and closing connections, and the latter to avoid frequently creating and destroying threads

25. Use buffered input and output streams for IO operations

Buffered input and output streams, namely BufferedReader, BufferedWriter, BufferedInputStream, BufferedOutputStream, which can greatly improve the efficiency of IO

26. Scenes with more sequential insertion and random access use ArrayList, while scenes with more element deletions and intermediate inserts use LinkedList

Well, just understand the principles of ArrayList and LinkedList.

27. Don't let too many parameters in the public method

Public methods are externally provided methods. If you give too many formal parameters to these methods, there are two main disadvantages:

(1) it violates the idea of object-oriented programming. Java emphasizes that everything is an object, and too many formal parameters are not consistent with the idea of object-oriented programming.

(2) too many parameters will inevitably increase the error probability of method calls.

As for how many "too many" means, three or four. For example, if we write an insertStudentInfo method with JDBC, there are 10 student information fields to be inserted into the Student table. These 10 parameters can be encapsulated in an entity class as formal parameters of the insert method.

String constants are written first when string variables and string constants equals

This is a common trick if you have the following code:

It is recommended to modify it as follows:

This is mainly to avoid null pointer exceptions.

29. Please know that there is no difference between if (I = = 1) and if (1 = = I) in java, but in terms of reading habits, the former is recommended.

People usually ask if there is any difference between "if (I = 1)" and "if (1 minute = I)".

In "if (I = = 1)", the judgment condition holds, which is based on 0 and non-0, where 0 represents false and non-0 represents true, if there is such a code:

It is judged that "iTunes 1" is not valid, so it is denoted by 0, that is, false. But if:

In case a programmer is not careful, write "if (I = 1)" as "if (I = 1)", then there will be a problem. If you judge that the content in if is not 0, you will return true if you assign I to 1, but the false should be returned when I is 2 and the comparison value is 1. This situation is likely to occur in the development of if Cure + and will lead to some incomprehensible errors. Therefore, in order to avoid incorrect assignment operations in the if statement, it is recommended to write the if statement as follows:

In this way, even if the developer accidentally writes "1 = I", the CpicCraft + compiler can check the time, because we can assign I to a variable as 1, but not to a constant as 1.

However, in Java, the "if (I = 1)" syntax of CAccord Cure + is impossible, because once the syntax is written, Java compiles the error "Type mismatch: cannot convert from int to boolean". However, although there is no semantic difference between "if (I = = 1)" and "if (1 = = I)" in Java, it is better to recommend the former in terms of reading habits.

Do not use the toString () method on the array

Take a look at what is printed out with toString () on the array:

It is intended to print out the contents of the array, but it is possible to cause a null pointer exception because the array reference is is empty. However, although it makes no sense to the array toString (), it is possible to print out the contents of the collection toString (), because the collection's parent class, AbstractCollections, overrides the toString () method of Object.

31. Do not make a downward forced transformation of basic data types that are out of range.

This will never get the desired result:

We may expect some of them, but the result is:

Explain. Long in Java is 8 bytes and 64 bits, so 12345678901234 should be represented in the computer as follows:

0000 0000 0000 1011 0011 1010 0111 0011 1100 1110 0010 1111 1111 0010

An int-type data is 4 bytes and 32 bits, and the first 32 bits of the above binary data from the low bit are:

0111 0011 1100 1110 0010 1111 1111 0010

This binary string is represented as decimal 1942892530, so that's what we output on the console above. By the way, two conclusions can be drawn from this example:

(1) the default data type of integer is int,long l = 12345678901234L, which is beyond the range of int, so * has an L, indicating that it is a long number. By the way, the default type of floating point type is double, so when defining float, write "" float f = 3.5f "

(2) the next sentence "int ii = l + I;" will report an error, because long + int is a long and cannot be assigned to int.

32. Data not used in public collection classes must be remove in time.

If a collection class is common (that is, it is not a property in a method), the elements in the collection are not automatically released because there are always references to them. Therefore, if some data in the common set is not used and remove them out, it will cause the common set to grow and make the system have the hidden danger of memory leakage.

33. Convert a basic data type to a string, the basic data type .toString () is the fastest way, String.valueOf (data) takes the second place, and data + "" is the slowest.

There are three ways to convert a basic data type to general. I have an Integer data I. You can use i.toString (), String.valueOf (I), and I + ". How efficient the three methods are? see a test:

The running result is:

So in the future, when you convert a basic data type to String, the toString () method is preferred. As for why, it's simple:

(1) the bottom layer of the String.valueOf () method calls the Integer.toString () method, but shorts the judgment before calling it.

(2) forget about the Integer.toString () method and call it directly.

(3) StringBuilder is used in the bottom layer of I + ". First, the append method is used to concatenate, and then the toString () method is used to obtain the string.

Compared with the three, it is obvious that 2 is the fastest, the second is the second, and the third is the slowest.

34. Use the most efficient way to traverse the Map

There are many ways to traverse Map. Usually, what we need to do is to traverse Key and Value in Map. The recommended and efficient way is:

If you just want to traverse the key value of this Map, it would be more appropriate to use "Set keySet = hm.keySet ();".

35. Close () for resources is recommended to operate separately.

It means, for example, I have this code:

It is recommended to modify it as follows:

Although there is some trouble, it can avoid the leakage of resources. We think that if there is no modified code, in case XXX.close () throws an exception, then it will enter the catch block, YYY.close () will not be executed, and the resource YYY will not be recycled. If there is too much code like this, it may cause resource handle leakage. After changing to the following writing, it is guaranteed that XXX and YYY will be dropped by close anyway.

36. Remove must be used before or after the use of ThreadLocal

At present, almost all projects use thread pool technology, which is very good, the number of threads can be dynamically configured and threads can be reused.

However, if you use ThreadLocal in your project, be sure to remove it before or after use. This is because it is mentioned above that thread pool technology is a thread reuse, which means that when the code is running, a thread is finished and will not be destroyed but wait for the next use. Let's take a look at the reference that holds ThreadLocal.ThreadLocalMap in the Thread class:

The fact that the thread does not destroy means that the data in the ThreadLocal.ThreadLocalMap of the previous thread set still exists, so when the next thread reuses the Thread, it is likely that the get will get the data of the previous thread set rather than what it wants.

This problem is very obscure, once there is an error caused by this reason, it is very difficult to find this problem without relevant experience or solid foundation, so pay attention to this when writing code, which will reduce a lot of work for you later.

37. Remember to replace the devil number with the constant definition. The existence of the devil number will greatly reduce the readability of the code. Whether the string constant is defined by constant or not can be determined according to the situation.

38, long, or Long initial assignments with uppercase L instead of lowercase l, because the letter l is easily confused with the number 1, which is very detailed and worth noting

39. All overridden methods must retain the @ Override annotation

There are three reasons for this:

(1) it is clear that this method is inherited from the parent class

(2) getObject () and get0bject () methods. The fourth letter of the former is "O" and the fourth child of the latter is "0". You can immediately judge whether the rewrite is successful by adding the @ Override annotation.

(3) modify the method signature in the abstract class, and the implementation class will report a compilation error immediately.

40. It is recommended to use the newly introduced Objects tool class in JDK7 to compare the equals of objects, directly a.equals (b), the risk of null pointer exception

41. Do not use "+" for string concatenation in the loop body, but use StringBuilder to append continuously.

Tell me why I don't use "+" for string concatenation, if I have a method:

Decompile the compiled .class file with javap-c to intercept the key parts:

It means that every time the virtual machine encounters the "+" operator to concatenate the string, it will new a StringBuilder, then call the append method, and * * call the toString () method to convert the string assignment to the oriStr object, that is, how many times the loop will new the number of StringBuilder (), which is a waste of memory.

42. Do not capture runtime exception classes that inherit from RuntimeException defined in the Java class library

Exception handling is inefficient, and most of RuntimeException's runtime exception classes can be avoided by programmers, such as:

(1) ArithmeticException can circumvent by judging whether the divisor is empty.

(2) NullPointerException can circumvent by judging whether the object is empty or not.

(3) IndexOutOfBoundsException can avoid by judging the length of array / string.

(4) ClassCastException can be circumvented by instanceof keyword

(5) ConcurrentModificationException can use iterators to avoid

43. Avoid the use of Random instances by multiple threads. Although sharing the instance is thread-safe, it will lead to performance degradation due to competition for the same seed. After JDK7, you can use ThreadLocalRandom to get random numbers.

Explain the performance degradation caused by competing for the same seed, for example, take a look at the nextInt () method implementation of the Random class:

The next (int bits) method is called, which is a protected method:

And the seed here is a global variable:

When multiple threads get random numbers at the same time, they will compete for the same seed, resulting in a reduction in efficiency.

44. Static class, singleton class, factory class set their constructor to private

This is because static classes, singleton classes, and factory classes do not need to be externally new out. After setting the constructor to private, we ensure that these classes do not produce instance objects.

Thank you for your reading, the above is the content of "what is the method of Java code optimization". After the study of this article, I believe you have a deeper understanding of what the method of Java code optimization is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report