Some of the places in Java programming that "for performance" try to do

Source: Internet
Author: User

For the machine memory is full, in addition to new machine memory, but also should be well review our code, there are many code to write too casually, these bad habits or the language of the program is not understood to be a good suppression of repression.


1. Try to use a single case in the appropriate situation

First, control the use of resources, through thread synchronization to control the concurrent access of resources;

Third, control data sharing and allow multiple unrelated processes or threads to communicate without establishing a direct association.
2. Try to avoid using static variables arbitrarily

public class a{static b b = new B (); }

At this point, the lifetime of static variable B is synchronized with Class A, and if Class A does not unload, the B object will reside in memory until the program terminates.

3. Try to avoid too often creating Java objects


Try to avoid the frequently called methods, loops in the new object, because the system not only takes time to create objects, but also takes time to garbage collection and processing of these objects, in the scope of our control, to maximize the reuse of objects, it is best to use basic data types or arrays to replace the object.


4. Use the final modifier as much as possible

A class with a final modifier is not derived. In the Java Core API, there are many examples of final applications, such as java.lang.String. Specifying final for the string class prevents the consumer from overwriting the length () method. In addition, if a class is final, all methods of that class are final. The Java compiler looks for the opportunity Inline (inline) for all final methods (this is related to the specific compiler implementation). This will increase 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 saved in the stack, which is faster. Other variables, such as static variables, instance variables, and so on, are created in the heap and are slower.

6. Try to handle the use of both the packing type and the basic type

Although wrapper types and basic types can be converted to each other during use, they produce a completely different area of memory, with basic type data generation and processing in the stack, the wrapper type being the object, and the instance being generated in the heap.

In the collection class object, there are objects that require processing for the wrapper type, and other processing advocates the use of basic types.


7. Use synchronized carefully to minimize the Synchronize method

All know that the implementation of synchronization is a great cost of overhead, and may even lead to deadlock, so try to avoid unnecessary synchronization control. When the Synchronize method is called, it locks the current object directly, and other threads cannot invoke the other methods of the current object until the method finishes executing. So the method of synchronize is as small as possible, and should try to use method synchronization instead of code block synchronization.


8. Use StringBuilder and StringBuffer for string connections whenever possible

That's not much to say.

9. Try not to use the Finalize method

In fact, it is very bad choice to put the resource cleanup in the Finalize method, because the GC workload is very large, especially when recovering Young's memory, most of which will cause the application to pause, so the choice of using the Finalize method for resource cleanup will lead to greater GC burden, Programs run less efficiently.


10. Try to use basic data types instead of objects

String str = "Hello";

In this way, a "hello" string is created, and the character cache pool of the JVM also caches the string;

String str = new string ("Hello");

At this point the program, in addition to creating a string, str refers to the bottom of the string object also contains a char[] array, the char[] array is stored in turn h,e,l,l,o


11. Single thread should try to use HashMap, ArrayList

HashTable, vectors, etc. use the synchronization mechanism to reduce the performance.


12. Create HashMap as reasonable as possible

When you want to create a larger hashmap, take advantage of another constructor

Public HashMap (int initialcapacity, float loadfactor)

Avoid HashMap multiple hash refactoring, the expansion is a very cost-performance thing, in the default initialcapacity only 16, and Loadfactor is 0.75, how much capacity you need, you'd better be able to accurately estimate the best size you need, The same is true of the same hashtable,vectors.


13. Minimize the repeated calculation of variables

Such as

for (int i=0;i<list.size (); i++)//should be changed to for (int i=0,len=list.size (); I


This article is from the "beautiful Dē‖java Question" blog, please be sure to keep this source http://teny32.blog.51cto.com/8027509/1663086

Some of the places in Java programming that "for performance" try to do

Related 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.