In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I would like to share with you the details of Java performance optimization, which are relevant knowledge points, detailed content and clear logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
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 instances in order to achieve the purpose of saving resources; third, control data sharing without establishing a direct correlation. Enable communication between multiple unrelated processes or threads.
two。 Try to avoid the random use of static variables
When an object is defined as a reference to the static variable, GC usually does not reclaim the memory occupied by the object, such as
Public class A {private static B b = new B ();}
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 JAVA core API, there are many examples of using final, such as java, lang, and String, and specifying final for the String class prevents users from overriding the length () method. In addition, if a class is final, then all methods of that class are final. The java compiler looks for opportunities to inline (inline) all final methods (this is related to the specific compiler implementation), which can improve performance by an average of 50%.
For example, change the getter/setter method that accesses variables in the instance to "final:
The simple getter/setter method should be set to final, which tells the compiler that the method will not be overloaded, so it can be changed to "inlined", for example:
Class MAF {public void setSize (int size) {_ size = size;} private int _ size;} correct class DAF_fixed {final public void setSize (int size) {_ size = size;} private int _ size;}
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. Therefore, the synchronize approach is minimized, and you should try to use method synchronization instead of code block synchronization.
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
String str = "hello"
The above method creates a "hello" string, and JVM's character cache pool also caches the string
String str = new String ("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. Multithreads should try their best to use HashMap and ArrayList without thread safety.
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 this constructor
Public HashMap (int initialCapacity, float loadFactor)
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 (int iTuno Tinci 2; int num = a > > 3)
However, it is important to note that comments should be added when using shifts, because the shifts are not intuitive and difficult to understand.
17. Try to use shift instead of 'axib' operation
Similarly, for'* 'operations, using shift operations will be faster and more efficient
Such as:
Int num = a * 4; int num = a * 8
It should be changed to:
Int num = a
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.