System Performance Optimization (6) -- Java exception handling performance optimization

Source: Internet
Author: User

It takes a lot of time to execute a catch code block and throw an exception. In this process, the performance loss is mainly caused by obtaining a snapshot of the thread stack when an exception is created. To throw an exception, you must first create a new object throwable class constructor to call the method named fillinstacktrace. The fillinstacktrace method checks the stack and collects Call trace information. Since a new object is created during the processing, the JVM must adjust the call stack as long as an exception is thrown, and the system resource overhead will increase.

1. Optimize the compiler and runtime. put several methods in a try/Catch Block instead of using try/catch blocks for each method call.

try{     Some.method1();  }catch(method1Exception e){     handle exception 1 }try{     Some.method2();  }catch(method2Exception e){     handle exception 2  }try{     Some.method3();  }catch(method3Exception e){     handle exception 3  }

The following code should be optimized:

try{ Some.method1();     Some.method2();     Some.method3(); }catch(method1Exception e){     handle exception 1 }catch(method2Exception e){     handle exception 2 }catch(method3Exception e){     handle exception 3 }

2. Exceptions can only be used for error handling and should not be used to control program processes.

public class Test {    int value;    public int getValue() {    return value;    }    public void reset() {    value = 0;    }    // Calculates without exception    public void method1(int i) {    value = ((value + i) / i) << 1;    // Will never be true    if ((i & 0xFFFFFFF) == 1000000000) {    System.out.println("You'll never see this!");    }    }    // Could in theory throw one, but never will    public void method2(int i) throws Exception {    value = ((value + i) / i) << 1;    // Will never be true    if ((i & 0xFFFFFFF) == 1000000000) {    throw new Exception();    }    }    // This one will regularly throw one    public void method3(int i) throws Exception {    value = ((value + i) / i) << 1;    // i & 1 is equally fast to calculate as i & 0xFFFFFFF; it is both    // an AND operation between two integers. The size of the number plays    // no role. AND on 32 BIT always ANDs all 32 bits    if ((i & 0x1) == 1) {    throw new Exception();    }    }    public static void main(String[] args) {    int i;    long l;    Test t = new Test();    l = System.currentTimeMillis();    t.reset();    for (i = 1; i < 100000000; i++) {    t.method1(i);    }    l = System.currentTimeMillis() - l;    System.out.println(    "method1 took " + l + " ms, result was " + t.getValue()    );    l = System.currentTimeMillis();    t.reset();    for (i = 1; i < 100000000; i++) {    try {    t.method2(i);    } catch (Exception e) {    System.out.println("You'll never see this!");    }    }    l = System.currentTimeMillis() - l;    System.out.println(    "method2 took " + l + " ms, result was " + t.getValue()    );    l = System.currentTimeMillis();    t.reset();    for (i = 1; i < 100000000; i++) {    try {    t.method3(i);    } catch (Exception e) {    // Do nothing here, as we will get here    }    }    l = System.currentTimeMillis() - l;    System.out.println(    "method3 took " + l + " ms, result was " + t.getValue()    );    }}

The code above first creates three methods. Among the three methods, the first one does not throw an exception, the second one throws an exception when the conditions are met, and the third one is the same as the second one. (In fact, value exists in the three methods to prolong the running time of the program and has no practical significance .) From the running results, it can be seen that throwing an exception is resource-consuming. Although the performance is acceptable when no exception occurs, it is not recommended to use the exception Control Program process.

Optimization is not to do anything redundant. optimization is to minimize the overhead when the function is implemented.

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.