Home >Java >javaTutorial >Some things that should be done 'for performance' in Java programming

Some things that should be done 'for performance' in Java programming

伊谢尔伦
伊谢尔伦Original
2016-11-29 10:46:26932browse

The recent machine memory is full again. In addition to adding new machine memory, we should also review our code. Many codes are written too casually. These bad habits or lack of understanding of programming languages ​​should be suppressed. .

Some things that should be done for performance in Java programming

The following are some things that should be done as much as possible in Java programming, summarized with reference to online resources.

 1. Try to use singletons in appropriate situations

Using singletons can reduce the burden of loading, shorten loading time, and improve loading efficiency, but not all places are suitable for singletons. Simply put, singletons Mainly applicable to the following three aspects:

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

 Second, control the generation of instances to achieve the purpose of saving resources;

 Third, Control data sharing and enable communication between multiple unrelated processes or threads without establishing a direct relationship.

 2. Try to avoid using static variables at will

 You must know that when an object is defined as referenced by a stataic variable, gc usually will not reclaim the memory occupied by this object, such as

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

At this time, the static variable b The life cycle is synchronized with class A. If class A will not be unloaded, then object b will reside in memory until the program terminates.

  3. Try to avoid creating too many Java objects

Try to avoid new objects in frequently called methods and loops, because the system not only spends time creating objects, but also spends time garbage collecting these objects. And processing, within the scope that we can control, reuse objects to the maximum extent, it is best to replace objects with basic data types or arrays.

 4. Try to use the final modifier

 Classes with final modifiers cannot be derived. In the Java core API, there are many examples of applying final, such as java.lang.String. Specifying final for the String class prevents users from overriding the length() method. In addition, if a class is final, all methods of the class are final. The Java compiler will look for opportunities to inline all final methods (this depends on the specific compiler implementation). This can improve performance by an average of 50%.

 5. Try to use local variables

 The parameters passed when calling the method and the temporary variables created during the call are all saved in the stack (Stack), which is faster. Other variables, such as static variables, instance variables, etc., are created in the Heap and are slower.

 6. Try to handle the usage situations of both packaged types and basic types

 Although packaged types and basic types can be converted to each other during use, the memory areas generated by them are completely different. Basic types Data generation and processing are all handled in the stack. The packaging type is an object and an instance is generated in the heap.

 In collection class objects, the packaging type is applicable to the processing required by the object. For other processing, it is recommended to use basic types.

 7. Use synchronized with caution and minimize the method of synchronize

 We all know that achieving synchronization requires a lot of system overhead as a cost, and may even cause deadlock, so try to avoid unnecessary synchronization control. When the synchronize method is called, the current object is directly locked, and other threads cannot call other methods of the current object until the method is executed. Therefore, the synchronize method should be as small as possible, and method synchronization should be used instead of code block synchronization as much as possible.

  8. Try to use StringBuilder and StringBuffer for string connection

  I won’t go into details about this.

 9. Try not to use the finalize method

 In fact, it is a very bad choice to complete resource cleanup in the finalize method. Due to the heavy workload of GC, especially when recycling Young generation memory, it will often cause application problems. The program is suspended, so choosing to use the finalize method to clean up resources will cause a greater burden on the GC and worse program running efficiency.

 10. Try to use basic data types instead of objects

 String str = "hello";

 The above method will create a "hello" string, and the JVM's character buffer pool will also cache this string;

String str = new String("hello");

At this time, in addition to creating a string, the bottom layer of the String object referenced by str also contains a char[] array. This char[] array stores h, e, and l in sequence. ,l,o

 11. Single threads should try to use HashMap and ArrayList

  HashTable, Vector, etc. use synchronization mechanism, which reduces performance.

 12. Create a HashMap as reasonably as possible

 When you want to create a relatively large hashMap, make full use of another constructor

 public HashMap(int initialCapacity, float loadFactor)

 Prevent HashMap from hash reconstruction multiple times , Expansion is a very performance-consuming thing. By default, the initialCapacity is only 16, and the loadFactor is 0.75. How much capacity is needed? It is best to accurately estimate the optimal size you need. The same is true for Hashtable and Vectors. reason.

 13. Minimize repeated calculation of variables

  如

  for(int i=0;i

  应该改为

  for(int i=0,len=list.size();i

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

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

  如

  A a = new A();

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

  应该改为

  if(i==1){

  A a = new A();

  list.add(a);}

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

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

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

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

  如

  int num = a / 4;

  int num = a / 8;

  应该改为

  int num = a >> 2;

  int num = a >> 3;

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

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

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

  如

  int num = a * 4;

  int num = a * 8;

  应该改为

  int num = a

  int num = a

  18. 尽量确定StringBuffer的容量

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

  如:StringBuffer buffer = new StringBuffer(1000);  

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

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

  例如:

Public void test(){
Object obj = new Object();
……
Obj=null;
}

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

Public void test(){
Object obj = new Object();
……
Obj=null;
//执行耗时,耗内存操作;或调用耗时,耗内存的方法
……
}

这时候就有必要将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. 尽量避免非常大的内存分配

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

  26. 慎用异常

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

  如果您创建一个 Exception ,就得付出代价。好在捕获异常开销不大,因此可以使用 try-catch 将核心内容包起来。从技术上讲,您甚至可以随意地抛出异常,而不用花费很大的代价。招致性能损失的并不是 throw 操作——尽管在没有预先创建异常的情况下就抛出异常是有点不寻常。真正要花代价的是创建异常。幸运的是,好的编程习惯已教会我们,不应该不管三七二十一就 抛出异常。异常是为异常的情况而设计的,使用时也应该牢记这一原则。

相关回复:

  xuanyuan 写道

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

  re:同意,不过文中有个地方说错了,使用synchronized关键字并不一定都是锁定当前对象的,要看具体的锁是什么。如果是在方法上加的synchronized,则是以对象本身为锁的,如果是静态方法则锁的粒度是类。

  ---------------

  9.尽量不要使用finalize方法

  re:同意,其实不推荐用finalize方法的根本原因在于,JVM的规范并不保证何时执行该方法,所以用这个方法来释放资源很不合适,有可能造成长时间资源得不到释放。

  ---------------

  16.尽量使用移位来代替'a/b'的操作;17.尽量使用移位来代替'a*b'的操作

  re:个人不太同意这两条。这样做确实有更好的性能,但是却牺牲了可读性。这两个操作符对很多程序员来说并不直观。我认为在如今硬件价格不那么昂贵的情况下,略微牺牲一些性能,换来更好的可读性和可维护性是好的选择。

  wuzhengju 写道

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

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

  例如:

Public void test(){
Object obj = new Object();
……
Obj=null;
}

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

Public void test(){
Object obj = new Object();
……
Obj=null;
//执行耗时,耗内存操作;或调用耗时,耗内存的方法
……
}

如果Object obj = new Object(); 如果这对象并不是大对象,这有必要吗?Obj=null;只是告诉jvm这个对象已经成为垃圾,至于什么时候回收,还不能确定! 这可读性也不好!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn