First let's talk about the question:
What I'm doing this time is the different effects of a new object inside and outside the loop.
You can see that the effect of this new object in different positions is different.
After multiple inquiries and verifications, the following conclusions can be drawn:
* EasyUITree tree=new EasyUITree();
* The same memory space is placed outside. It seems that there are many trees in your list, but they are all the same tree object. The content of the tree is the content of your last modification, so in the end The nodes of the tree are all the same,
and when you put it in it, a new memory space is opened every time, that is, a new object tree, so the list stores all the independent tree objects, and the content is naturally different. same
*/
So: outside the loop, it is always the same object, and the loop puts the latest value.
In the body of the loop, different objects are created, and objects corresponding to the different values are put in each time.
Here are some explanations of the specific jvm:
In fact, in Java's GC mechanism, if an object has a specific reference, it will not be automatically reclaimed by GC.
That is:
Object obj = new Object();//Writing in 100 cycles means that you have 100 references corresponding to 100 objects
, so 100 objects will occupy memory for a period of time, until the memory is insufficient and GC actively reclaims
obj = new Object();//Writing in 100 loops means that you use 1 reference to call 100 objects 100 times respectively,
so when the latter object is init, the former object is already in "no reference state", which will be very Fast and automatic collection by GC
(before your loop is over, there may have been multiple GC collections, which is very important)
That is to say, the second method can better manage memory.
psJava high-performance programming is more in line with the computer's recognition and efficiency through the programmer's control of the code.
If the subject thinks security is more important (but I didn't expect any security problems), then you can follow your method.
After all, the times are To make progress, programming does not need to be done step by step, it is good to realize functions and ensure efficiency.
Link: https://www.zhihu.com/question/31751468/answer/88626975
Here, let me mention a digression. The JVM has an excellent algorithm. The method stack of the JVM. A method call generates a stack frame. The stack frame calculates how many local variables you have when the method is called. This calculation will filter Scope, such as creating a local variable after your loop, the JVM thinks that there is only one local variable, because your Object obj = new Object(); inside the curly brackets, there will be no curly brackets, so you only need to A location to store local variables is enough, that is to say, when running to the braces, use this location to execute the code. After the braces are out, the next local variable can then use this location on the stack to execute the following code.
Reprinted from: http://www.cnblogs.com/fengli9998/p/6702271.html