Does setting variable to null clear just the reference? [duplicate]

∥☆過路亽.° 提交于 2020-11-28 06:26:35

问题


What happens if I do:

Object obj = new Object();
obj = null;

Does it remove the object from memory, or clear just the reference?

More formally, consider the code:

Object obj = new Object();
Object temp = obj;
obj = null;

Why is temp still not null? Shouldn't it have been removed from memory?


回答1:


In your first example:

obj -------> |OBJECT|

After setting obj to null:

obj          |OBJECT|

No reference to the object exists so it "doesn't exist" anymore because it has become unreachable.


In your second example:

obj -------> |      |
             |OBJECT|
temp ------> |      |

After setting obj to null:

obj          |      |
             |OBJECT|
temp ------> |      |

You can see that temp still references the object so it still "exists".




回答2:


Setting an object to null, clears the reference. If there is no longer any reference to that object, as in your first example, the memory can be freed, when the JVM runs the GC (Grarbage Collector) the next time.

As long as there is still a reference to the object (second example) this cannot happen.

Furthermore, there is a difference between freeing the memory by GC and returning the memory back to the OS, so it can be reused. Usually, the JVM keeps once allocated memory until the JVM exists or the OS has run out of free memory.




回答3:


Object obj = new Object();
obj = null;

Above: It just removes the reference, does not delete the object. However, GC will clear the object from memory because there is no other reference to that object.

In this:

Object obj = new Object();
Object temp = obj;
obj = null;

temp is still a reference to object, so object stays in memory.




回答4:


Java works with garbage collection. So you will NEVER know what time an object will be removed from memory. When you remove the last reference to an object, it is passed to the garbage collector and the garbage collector will delete the object sometimes.

In your second example you create an object and give the reference to obj. Then you reference obj by temp. now you have 2 references to your object. obj AND temp. When you now remove the reference obj, the object is still referenced by temp and so it wont be given to the garbage collector.




回答5:


In your first example, obj becomes immediately eligible for garbage collection. It's not yet removed from memory, and attempts to use the reference will result in a NullPointerException.

In your second example, the assignment to temp creates a copy of the reference originally referred to by obj. Setting obj to null wouldn't make the object eligible for garbage collection, so it's very much still around.




回答6:


No this is not removed from memory just the reference have been set to null. In java GC(Garbage Collector) runs at specific duration only when it needs some more memory or routine check. Until then the object remains in HEAP memory.

As JAVA do not have pointers and destructors, HEAP storage is purely maintained by GC.




回答7:


The two pieces of code behave different.

In the former, the object reference created by new Object() will be eligible for GC. In the latter, the object reference created by new Object() won't be eligible for GC because it's still referenced by temp.



来源:https://stackoverflow.com/questions/33441652/does-setting-variable-to-null-clear-just-the-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!