We often see books or ideas about Java performance tuning, such as 44 Java Code Performance Tuning Summaries. The idea is not to define variables inside the loop, which can take up too much memory and affect performance, but outside the loop. Having been around Java for so long, I believe that many Java programmers have been misled by this code performance optimization strategy.

Take a look at the following two examples: Example 1 defines variables outside the loop and Example 2 defines variables inside the loop.

/** * private static void */ private static voidouter() {
	Javastack javastack = null;
	for(int i = 0; i < 10; i++) { javastack = new Javastack(); }} /** * private static voidinner() {
	for(int i = 0; i < 10; i++) { Javastack javastack = new Javastack(); }}Copy the code

Let’s look at these two examples first.

Define variables outside the loop

Variables are defined outside the loop, and each reference within the variable loop refers to a different object instance. Each time the object instance is changed in the loop, the object that was last pointed to is destroyed until the last loop. This way, after the loop ends, the variable will still exist, pointing to the last object instance in the loop, and all other objects will be destroyed.

In this way, the life cycle variable that should be in the loop is spread out of the loop, and if it is still used outside the loop, it can lead to unpredictable consequences for the subsequent business. This kind of problem often meets in the author’s work, see the following example.

/** * private static void */ private static voidouter() {
	Javastack javastack1 = null;
	for (int i = 0; i < 10; i++) {
		javastack1 = new Javastack();
	}

   Javastack javastack2 = userDao.getUser(10);
   
}
Copy the code

Defines a javastack2 above, if at this point in the subsequent code or transfer to other ways to write wrong, used the javastack1, that at this time do not have a problem? That’s just one thing, but if you use the same variable name, and an exception occurs when that variable is reused, it should be null, and you get the value in the previous loop body.

Define variables inside the loop

Variables defined within a loop, and circulation is slightly different, every time to create a new local variable pointing to the new object instances, each variable and object lifecycle is limited to within the loop body, and each loop over the local variable and object instances will be destroyed by the end of the loop body, so there is no takes up more memory.

conclusion

Two USES will create the same number of object instances, only the cycle will be repeated to create the same number of local variables and stack memory garbage collection frequency will be higher, but for the pile of garbage collection performance impact and variable impact of business life cycle, the stack memory performance impact is negligible.

Therefore, it is recommended to define variables within a loop, which limits the lifetime of variables to the scope of the loop body and avoids serious problems caused by business reuse of variables.

Follow the wechat public account of Java technology stack, stack leader will continue to share Java dry goods tutorial, the public account will be the first time to push, continue to pay attention to. In the public account background reply: Java, get stack length arrangement of more Java tutorial, are actual combat dry goods, the following is only part of the preview.

  • Do you really understand the transient keyword?
  • 1. Synchronized?
  • With Java 11 released, Strings can still play like this!
  • Are Strings in Java really immutable?
  • Five differences between sleep() and wait()