A performance optimization strategy that was misled by the Java programmer

Source: Internet
Author: User

We often see some Java performance-optimized books or concepts that do not define variables within a loop, which can take up too much memory-impacting performance and be defined outside the loop. Having been in touch with Java for so long, it is believed that many Java programmers are misled by this code performance optimization strategy.

Looking at the following two examples, example 1 defines a variable outside of a loop, and Example 2 defines a variable within a loop.

/** * 循环外定义变量 */private static void outer() {    Javastack javastack = null;    for (int i = 0; i < 10; i++) {        javastack = new Javastack();    }}/** * 循环内定义变量 */private static void inner() {    for (int i = 0; i < 10; i++) {        Javastack javastack = new Javastack();    }}

Let's start by analyzing the two examples.

Define variables outside the loop

Out-of-loop definition variables, each time a reference in a variable loop points to a different instance of an object, each time the object instance is changed, the last object that was pointed to is destroyed until the final loop. Thus, after the loop is over, the variable exists and points to the last object instance within the loop, and the other objects are destroyed.

In this way, the life cycle variables in the circulating body are spread out into the loop, and if the variable is still used outside the loop, it can result in unpredictable consequences for the subsequent business. This problem is often encountered in the author's work, see the following example.

/** * 循环外定义变量 */private static void outer() {    Javastack javastack1 = null;    for (int i = 0; i < 10; i++) {        javastack1 = new Javastack();    }   Javastack javastack2 = userDao.getUser(10);   }

The above defines a javastack2 , if at this time in the subsequent code or passed to another method is written wrong, javastack1 then it is not a problem? This is only on the one hand, and if you use the same variable name, when the variable is reused when an exception occurs, the exception should be null value, the result is the value of the previous loop body.

Define variables within a loop

The loop defines the variable, and the outside of the loop is slightly different, each time a new local variable is created to point to the new object instance, each variable and object life cycle is limited to the loop body, and each loop ends the local variable and the object instance will be destroyed with the end of the loop body, so there is no more memory to use this argument.

Summarize

Both usages create the same number of object instances, except that the same number of local variables are repeatedly created within the loop, and the stack memory garbage collection frequency is higher, but the performance impact of stack memory is negligible for the performance impact of heap garbage collection and the business impact of the variable life cycle.

Therefore, it is recommended to use a loop-defined variable, which restricts the life cycle of a variable to the scope of the loop body, and does not cause serious problems with reusing variables on the business.

Recommended: Spring Boot & Cloud's Strongest technology tutorials

A performance optimization strategy that was misled by the Java programmer

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.