Some Java
Code Optimization solution. The latest project only has 1 MB of memory available and must be very tricky ~ To avoid memory overflow
L release unused objects (leave empty)
For example:
When a is not blank, A = new object () // when the code is executed, two objects exist in the memory.
It is better to write a = NULL; A = new object ();
Set unnecessary objects to null.
L memory overflow usually occurs in the constructor. In the constructor, the variable is new when necessary, and then null is set after use.
Loading all images at a time can easily cause memory peaks.
AvailableIf (IMG = NULL ){
Create...
}
L use the static factory method to replace the constructor (however, pay attention to release, the release of the singleton mode can only be done by yourself ).
L use local variables
L partial redrawing for graphic repainting
L avoid setting or calling variables by calling get set in the same class, and use public
L avoid generating the same variable or calling the same function in a loop
L use static final private as much as possible for common variables
L if you can use byte, you do not need Int or string.
L do not use double
L use system. appaycopy () to copy a large amount of data ()
L avoid using generics
L try not to use try catch. Do not write try catch in the loop body when necessary
L string operations use stringbuffer
Code 2 is twice the execution speed of code 1:
Code 1:
String title = new string ("Hello everyone ");
Title + = "welcome ";
Title + = "read"
// Five objects will be generated in the stack: "Hello everyone", "welcome", "read", "Hello everyone", and "Hello everyone! Read"
Code 2:
Stringbuffer Title = new stringbuffer ("Hello everyone");
Tltle. append ("Welcome");
Title. append ("Read");
L The stringbuffer constrtor will create a default size (usually 16) character array. In use, if the size exceeds this value, the memory will be re-allocated, a larger array will be created, the original array will be copied, and then the old array will be discarded. In most cases, you can specify the size when creating the stringbuffer, which avoids automatic growth when the capacity is insufficient to improve performance.
L use zero value as the judgment Condition
For (INT I = A. Length ()-1; I> = 0; I --)BetterFor (INT I = 0; I <A. Length (); I ++)
LDo not reinitialize Variables
By default, Java initializes the variable to a definite value when calling the class constructor: all objects are set to null, and integer variables (byte, short, Int, long) set to 0, float and double to 0.0, and logical value to false
LMultiplication and divisionThe Division is slower than multiplication, so the multiplication is replaced by reciprocal.
Consider the following code:
For (val = 0; Val <100000; Val + = 5) {alterx = Val * 8; myresult = Val * 2 ;}
Replacing multiplication with shift operations can greatly improve performance. The modified code is as follows:
For (val = 0; Val <100000; Val + = 5) {alterx = Val <3; myresult = Val <1 ;}
L pass as few parameters as possible to frequently called Methods
L do not have empty methods, or there are few codes in the methods and few calls in the methods
L avoid type conversion
EnableUse profiler to find out where optimization is needed
Improve your
Algorithm , Write the best code