Performance Optimization | 30 Ge Java performance optimization techniques, would you?

In Java programs, largely because of performance problems is not the Java language, but the program itself. Develop good coding practice is very important, can significantly improve the performance of the program.

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


1, as far as possible in the case of a suitable embodiment with a single

Singleton can reduce the burden of load, reduce the time to load and improve the efficiency of the load, but not all places are suitable for a single case, in simple terms, a single case mainly applies to the following three aspects:

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

Second, control generates instance, in order to save resource;

Third, control data sharing, under conditions not directly related to the establishment, so that communication between multiple unrelated processes or threads.

2, try to avoid using static random variable

When an object is defined as a static variable is referenced, then the GC does not reclaim the object is usually occupied by memory, such as

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


At this static variable b life cycle synchronized with the Class A, Class A if not uninstall, then b objects of permanent memory, until the program is terminated.

3, try to avoid too much too often to create Java objects

Try to avoid frequent method calls, circulating new objects, due to the system not only takes time to create objects, but also to spend time on these objects for garbage collection and disposal, within the range that we can control, maximize reuse of objects , the best use an array of basic data types or objects instead.

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


4, try to use the final modifier

With the final modifier class is not derived. In the core JAVA API, there are many examples of the final application, e.g. java, lang, String, prevents the user specified final covering length () method of class String. Further, if a class is final, then all such methods are final. java compiler will look for opportunities to inline (inline) all of the final method (which the compiler and the specific implementation-dependent), a move that can improve the performance by an average of 50%.

Such as: let within the instance variable access getter / setter methods become "final:

Simple getter / setter methods should be set to the final, which will tell the compiler that this method will not be overloaded, it can become "inlined", example:

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


5, to make use of local variables

调用方法时传递的参数以及在调用中创建的临时变量都保存在栈(Stack)中,速度较快;其他变量,如静态变量、实例变量等,都在堆(Heap)中创建,速度较慢。

6、尽量处理好包装类型和基本类型两者的使用场所

虽然包装类型和基本类型在使用过程中是可以相互转换,但它们两者所产生的内存区域是完全不同的,基本类型数据产生和处理都在栈中处理,包装类型是对象,是在堆中产生实例。在集合类对象,有对象方面需要的处理适用包装类型,其他的处理提倡使用基本类型。

7、慎用synchronized,尽量减小synchronize的方法

都知道,实现同步是要很大的系统开销作为代价的,甚至可能造成死锁,所以尽量避免无谓的同步控制。synchronize方法被调用时,直接会把当前对象锁了,在方法执行完之前其他线程无法调用当前对象的其他方法。所以,synchronize的方法尽量减小,并且应尽量使用方法同步代替代码块同步。

9、尽量不要使用finalize方法

实际上,将资源清理放在finalize方法中完成是非常不好的选择,由于GC的工作量很大,尤其是回收Young代内存时,大都会引起应用程序暂停,所以再选择使用finalize方法进行资源清理,会导致GC负担更大,程序运行效率更差。

10、尽量使用基本数据类型代替对象

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


上面这种方式会创建一个“hello”字符串,而且JVM的字符缓存池还会缓存这个字符串;

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


此时程序除创建字符串外,str所引用的String对象底层还包含一个char[]数组,这个char[]数组依次存放了h,e,l,l,o

11、多线程在未发生线程安全前提下应尽量使用HashMap、ArrayList

HashTable、Vector等使用了同步机制,降低了性能。

12、尽量合理的创建HashMap

当你要创建一个比较大的hashMap时,充分利用这个构造函数

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


避免HashMap多次进行了hash重构,扩容是一件很耗费性能的事,在默认中initialCapacity只有16,而loadFactor是 0.75,需要多大的容量,你最好能准确的估计你所需要的最佳大小,同样的Hashtable,Vectors也是一样的道理。

13、尽量减少对变量的重复计算

如:

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


应该改为:

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


并且在循环中应该避免使用复杂的表达式,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快。

14、尽量避免不必要的创建

如:

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


应该改为:

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


15、尽量在finally块中释放资源

程序中使用到的资源应当被释放,以避免资源泄漏,这最好在finally块中去做。不管程序执行的结果如何,finally块总是会执行的,以确保资源的正确关闭。

16、尽量使用移位来代替'a/b'的操作

"/"是一个代价很高的操作,使用移位的操作将会更快和更有效

如:

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


应该改为:

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


但注意的是使用移位应添加注释,因为移位操作不直观,比较难理解。

17、尽量使用移位来代替'a*b'的操作

同样的,对于'*'操作,使用移位的操作将会更快和更有效

如:

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


应该改为:

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


18、尽量确定StringBuffer的容量

StringBuffer 的构造器会创建一个默认大小(通常是16)的字符数组。在使用中,如果超出这个大小,就会重新分配内存,创建一个更大的数组,并将原先的数组复制过来,再丢弃旧的数组。在大多数情况下,你可以在创建 StringBuffer的时候指定大小,这样就避免了在容量不够的时候自动增长,以提高性能。

如:

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


19、尽量早释放无用对象的引用

大部分时,方法局部引用变量所引用的对象会随着方法结束而变成垃圾,因此,大部分时候程序无需将局部,引用变量显式设为null。

例如:

Java代码

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


上面这个就没必要了,随着方法test()的执行完成,程序中obj引用变量的作用域就结束了。但是如果是改成下面:

Java代码

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


这时候就有必要将obj赋值为null,可以尽早的释放对Object对象的引用。

20、尽量避免使用二维数组

二维数据占用的内存空间比一维数组多得多,大概10倍以上。

21、尽量避免使用split

除非是必须的,否则应该避免使用split,split由于支持正则表达式,所以效率比较低,如果是频繁的几十,几百万的调用将会耗费大量资源,如果确实需要频繁的调用split,可以考虑使用apache的StringUtils.split(string,char),频繁split的可以缓存结果。

22、ArrayList & LinkedList

一个是线性表,一个是链表,一句话,随机查询尽量使用ArrayList,ArrayList优于LinkedList,LinkedList还要移动指针,添加删除的操作LinkedList优于ArrayList,ArrayList还要移动数据,不过这是理论性分析,事实未必如此,重要的是理解好2者得数据结构,对症下药。

23、尽量使用System.arraycopy ()代替通过来循环复制数组

System.arraycopy() 要比通过循环来复制数组快的多。

24、尽量缓存经常使用的对象

尽可能将经常使用的对象进行缓存,可以使用数组,或HashMap的容器来进行缓存,但这种方式可能导致系统占用过多的缓存,性能下降,推荐可以使用一些第三方的开源工具,如EhCache,Oscache进行缓存,他们基本都实现了FIFO/FLU等缓存算法。

25、尽量避免非常大的内存分配

有时候问题不是由当时的堆状态造成的,而是因为分配失败造成的。分配的内存块都必须是连续的,而随着堆越来越满,找到较大的连续块越来越困难。

Interview Necessary Skills: 30 Java performance optimization techniques, would you?


26、慎用异常

当创建一个异常时,需要收集一个栈跟踪(stack track),这个栈跟踪用于描述异常是在何处创建的。构建这些栈跟踪时需要为运行时栈做一份快照,正是这一部分开销很大。当需要创建一个 Exception 时,JVM 不得不说:先别动,我想就您现在的样子存一份快照,所以暂时停止入栈和出栈操作。栈跟踪不只包含运行时栈中的一两个元素,而是包含这个栈中的每一个元素。

If you create an Exception, you have to pay the price, the good news is not abnormal capture overhead, so you can use try-catch will be the core wrap. Technically, you can even casually thrown, without spending a great price. Incur performance loss is not the throw operation - although it is thrown in the absence of pre-created unusual anomaly is a bit unusual. The true cost of takes is to create exceptions, fortunately, good programming practice has taught us, it should not be willy-nilly throw an exception. Abnormal is abnormal and design, but also should keep in mind when using this principle.

27, try to reuse objects

In particular when a String object, using concatenation occurs StringBuffer place, not only because the system takes time to generate the object, may also need to spend time after these objects for garbage collection and disposal. Thus generating excessive object will have a big impact to the performance of the program.

28, do not repeat initialize variables

By default, when the class constructor call, java will initialize variables to determine the values ​​of all the objects is set to null, an integer variable to 0, float and double variable is set to 0.0, the logical value is set to false. When a class is derived from another class, this is especially should pay attention to, because when you create an object using the new keyword, all constructors are constructor in the chain is automatically invoked.

Here is a note to the member variable is set to an initial value but you need to call other methods, the best on a method. For example initXXX (), since a direct call assignment may be because the method has not been initialized and the class pointer exception shorting, such as: public int state = this.getState ().

29, in java Oracle's application development + in, java embedded SQL language should make full use of capitalization, in order to reduce the burden of parsing Oracle parser.

30, the java programming process, database connections, I / O stream operations, after use, to close the release of resources. Because the operation of these large objects can cause large overhead system.

end: If you think this article helpful to you, remember to praise point forward, your support is my updated power.

Guess you like

Origin www.cnblogs.com/wyf0518/p/11462045.html