In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the suggestions for code to improve performance in java". In daily operation, I believe that many people have doubts about what suggestions are made to code to improve performance in java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what suggestions to code to improve performance in java"! Next, please follow the editor to study!
1. Try to use singletons on appropriate occasions
The use of singletons can reduce the burden of loading, shorten the loading time, 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:
First, control the use of resources and control the concurrent access of resources through thread synchronization
Second, control the generation of examples in order to achieve the purpose of saving resources
Third, control data sharing and let multiple unrelated processes or threads communicate without establishing a direct association.
two。 Try to avoid the random use of static variables
You know, when an object is defined as a reference to the stataic variable, gc usually does not reclaim the memory occupied by the object, as shown in
Java code
PublicclassA {
StaticBb=newB ()
}
At this point, the life cycle of the static variable b is synchronized with class A, and if class A does not unload, then the b object will reside in memory until the program terminates.
3. Try to avoid creating Java objects too often.
Try to avoid new objects in frequently called methods, loops, because the system not only takes time to create objects, but also takes time to garbage collect and deal with these objects, within our control, the maximum reuse of objects, it is best to replace objects with basic data types or arrays.
4. Use final modifiers whenever 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. Specifying final for the String class prevents the consumer from overriding the length () method. In addition, if a class is final, then all methods of that class are final. The Java compiler will look for opportunities to inline (inline) all final methods (this is related to the specific compiler implementation). This can improve the performance by an average of 50%.
5. Use local variables as much as possible
The parameters passed when the method is called and the temporary variables created in the call are stored in the Stack, which is faster. Other variables, such as static variables, instance variables, and so on, are created in the Heap and are slow.
6. Try to deal with the use of both packaging types and basic types.
Although the wrapper type and the basic type can be converted to each other in the process of use, the memory areas generated by them are completely different, the basic type data generation and processing are processed in the stack, and the wrapper type is an object. Is to generate an instance in the heap.
In the collection class object, the wrapper type is applicable to the processing needed for the object, and the basic type is advocated for other processing.
7. Use synchronized cautiously and minimize synchronize
As we all know, the realization of synchronization takes a lot of system overhead as a price, and may even cause deadlock, so try to avoid unnecessary synchronization control. When the synchronize method is called, the current object is locked directly, and other threads cannot call other methods of the current object until the method is executed. So the method of synchronize is as small as possible, and you should try to use method synchronization instead of code block synchronization.
8. Try to use StringBuilder and StringBuffer for string concatenation
I won't say much about this.
9. Try not to use the finalize method
In fact, it is a very bad choice to clean up resources in the finalize method. Because of the heavy workload of GC, especially when reclaiming Young generation memory, it will mostly cause applications to pause, so choosing to use the finalize method for resource cleaning will lead to a greater burden on GC and less efficient programs.
10. Try to use basic data types instead of objects
Stringstr= "hello"
The above method creates a "hello" string, and JVM's character cache pool also caches the string
Stringstr=newString ("hello")
At this time, in addition to creating strings, the bottom layer of the String object referenced by str also contains a char [] array. This char [] array stores, in turn, the hmeme _ LJI _ LJO.
11. Single thread should use HashMap and ArrayList as much as possible.
HashTable, Vector and so on use synchronization mechanism, which degrades the performance.
twelve。 Create HashMap as reasonably as possible
When you want to create a larger hashMap, take full advantage of another constructor
PublicHashMap (intinitialCapacity,floatloadFactor)
To prevent HashMap from doing hash refactoring many times, capacity expansion is a very performance-consuming thing. By default, initialCapacity is only 16, while loadFactor is 0.75. how much capacity you need, you'd better accurately estimate the optimal size you need, and the same is true for Hashtable,Vectors.
13. Minimize the double calculation of variables
Such as
For (inti=0;i
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.