Java Program Performance optimization

Source: Internet
Author: User

1. Try to avoid using static variables arbitrarily

When an object is defined as a variable reference to static, the GC typically does not reclaim the memory used by the object

public class A {

Static b b = new B (); The life cycle of static variable B is synchronized with Class A, so long as Class A is not unloaded, the B object will reside in memory until the program terminates

}


2. Handling of packaging types and basic types of use of the site

Although the packing type and the basic type can be converted to each other during use, the memory area produced by both of them is completely different, and the basic type data is generated and processed in the stack;

The wrapper type is an object that produces an instance in the heap.


3. Use synchronized carefully

The implementation of synchronization is a significant cost to the system, and may even cause deadlocks, so try to avoid unnecessary synchronization control.


4. Single thread try to use HashMap, ArrayList

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


5. Minimize the repeated calculation of variables

for (int i = 0; i < list.size (); i++)//loop, cyclic conditions are calculated repeatedly

Should be changed to for (int i = 0, Len = list.size (); i < Len; i++)


6. Try to avoid unnecessary creation

A = new A ();

if (i = = 1) { list.add (a); }  

Should be changed to: if (i = = 1) { A A = new A (); List.add (a); }  


7. Try to determine the capacity of the StringBuilder

The StringBuilder constructor creates an array of characters with a default size (usually 16), which, if exceeded, allocates memory, creates a larger array, copies the original array, and discards the old array. In most cases, you can specify the size when creating the StringBuilder, which avoids auto-growth when the capacity is insufficient to improve performance.


8. Release references to useless objects as early as possible

Most of the time, the object referenced by the method local reference variable becomes garbage as the method ends, so most of the time the program does not have to set the local reference variable to NULL, but if the object is large and takes up more memory, it should be manually empty when not in use.



Java Program Performance optimization

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.