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 are the details of Java code performance optimization

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "what are the details of Java code performance optimization", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the details of Java code performance optimization?"

Use singletons in appropriate situations

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:

Control the use of resources and control concurrent access to resources through thread synchronization

Control the generation of examples to achieve the purpose of saving resources

Control data sharing and enable communication between multiple unrelated processes or threads without establishing a direct association. Sorted out a complete version of PDF in the Java interview dictionary

Avoid using static variables at will

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.

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.

Use the final modifier

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;}

Correction

Class DAF_fixed {final public void setSize (int size) {_ size = size;} private int _ size;} uses local variables

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.

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.

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.

Do not 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.

Use basic data types instead of object 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.

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.

Reasonable creation of HashMap

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.

Reduce the double calculation of variables

Such as:

For (int item0 Tinci 2nint 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.

Use shift instead of 'axib' operation

Similarly, for'* 'operations, using shift operations will be faster and more efficient

Such as:

Int num = a * 4int 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.

Share To

Development

Wechat

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

12
Report