The difference between placing a new object inside the loop body and outside the loop in java

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.

ps
Java 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

 
In terms of memory, in the JVM's method stack and heap memory, when Object obj = new Object(); is run, Object obj is placed on the top of the method stack, and then generated in the heap An Object object, and finally the obj in the method stack points to the Object object. At this time, a circular memory action is completed. Then the second loop, new Object(); generates a new Object object, and then the obj in the method stack points to the new Object object. The Object object in the first loop is not referenced by the task variable and becomes garbage, waiting for the JVM Garbage collector recycling. Other cycles are analogous.
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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325935109&siteId=291194637