· When you perform too many string operations to avoid unnecessary creation of objects that must eventually experience garbage collection, you can use the stringbuffer function instead of String concatenation.
· Avoid writing data to the Java console consecutively to reduce the cost of string operations, text formatting, and output.
· If necessary, use the primitive types of variables to avoid the cost of object creation and operation.
· Cache frequently-used objects to reduce the amount of garbage collection required and avoid re-object creation.
· Group local operations as much as possible to reduce calls to Java local interfaces (JNI.
· Use the synchronization method only when multiple tasks must be restricted in the JVM and operating system.
· Unless necessary, avoid calling the garbage collector. If you have to call it, do so only in idle time or some non-critical stages.
· Try to use an integer instead of a long integer, because the 32-bit operation is faster than the 64-bit operation.
· Try to declare the method as final. JVM can better handle final methods.
· Use the keyword static final when creating constants to reduce the number of times variables need to be initialized.
· Avoid unnecessary "casts" and "instanceof" references, because the data type conversion in Java is completed at runtime.
· Avoid using vectors if the array meets the requirements.
· Add an item to or remove it from the end of the vector.
· Use the-O option to compile a Java file.
· Avoid allocating objects in a loop.
· Use the buffer I/O and adjust the buffer size.
· Use the connection pool and prepare the cache statement for database access.
· Connect to the database using the connection pool and repeat the connection instead of opening or closing the connection repeatedly.
· Maximizes and minimizes the thread creation and destruction cycles.
· Minimize competition for shared resources.
· Minimize the creation of Short lifecycle objects.
· Avoid remote method calls.
· Use callback to avoid blocking remote method calls.
· Avoid creating objects for method access only.
· Keep the synchronization method out of the loop.
· Stores string and character data in unicode format in the database.
· Sort classpath to make the most common libraries appear first.
· Avoid using complex expressions in cyclic conditions.
· Replace the loop with "system. arraycopy ()" to copy the array.
· Change the getter/setter method of the variable used to access the instance to "final ".
· Use the displacement operation to replace "A/B" and "a * B ".
· The synchronized method is not called in the loop.
· Remove the try/Catch Block from the loop.
· Avoid unnecessary equality judgment for boolean values.
· Use stringtokenizer to replace string analysis methods such as "indexof ()" and "substrng.
· Use X more? Y: Z to replace conditional judgment
· Do not instantiate variables in the loop body
· Determine the capacity of stringbuffer
· Use stack variables whenever possible
· Try not to use reverse operations (!)
· Try to perform the instanceof operation with an interface instead of a class
· When parsing XML, if you do not need to serialize, verify you can consider using the xpp model (XML pull Parser (xpp) http://www.extreme.indiana.edu/soap/xpp/
· To create an array of elements in a set instead of iterations for some reason during map element operations, use this array to iterate the elements. If you do not need an intermediate array, do not create it. Instead, use the iterator iteration element.
Int mapsize = AMAP. Size ();
Iterator keyvaluepairs1 = AMAP. entryset (). iterator ();
For (INT I = 0; I <mapsize; I ++)
{
Map. Entry entry = (Map. Entry) keyvaluepairs1.next ();
Object key = entry. getkey ();
Object value = entry. getvalue ();
...
}
Recommended:
Object [] keyvaluepairs2 = AMAP. entryset (). toarray ();
For (INT I = 0; I <REM; I ++ ){
{
Map. Entry entry = (Map. Entry) keyvaluepairs2 [I];
Object key = entry. getkey ();
Object value = entry. getvalue ();
...
}
· Map is usually suitable for access by key rather than by value. tests that use containskey () and containsvalue () to traverse all elements in the hashmap show that the time required for containsvalue () is much longer.
· The hash value can be considered
Int hashvalue = (key. hashcode () & 0x7fffffff) % table. length;
It is essentially equal
Int hashvalue = maths. Abs (key. hashcode () % table. length;
However, a faster mechanism is used to obtain the same function with a positive value.
· Adjust the map size. Using the default size of map will lead to multiple resizing operations, with a high overhead. In the server mode (Java-server) 50% more time is required, and almost twice the time is required in client mode! The odd number of buckets enables MAP to improve execution efficiency by reducing the number of conflicts
· To declare the map variable, use the interface declaration whenever possible: for example:
Map criticalmap = new hashmap (); // good
Hashmap criticalmap = new hashmap (); // difference
· Jdk5.0 and later versions of JVM can be set when running the program (Java -- XX: permsize = 128 m
) To improve heap allocation and garbage collection efficiency (when many classes are to be loaded into the VM Program)
· When the collection object is to be recycled, do not use iterator. hasnext () and enumerator. hasmoreelements () to obtain the size using collection. Size.
· String. Equals () can not only determine whether two strings are equal, but also more efficiently determine whether the string length is 0.
· When using I/O operations, select buffered I/O classes.
· Using private and static methods and final classes, they can be converted into inline forms during compilation.
· Use jar or ZIP files whenever possible
· It is very slow to create the doubles type from the string type.
You can view the optimization points of bytecode search to view the steps of bytecode:
Javac-G demo. Java
Javap-C-P demo
For the significance of bytecode, refer to JDK documents.