Is memory freed by assigning null?

3

I have this code

clase MiObjeto = new clase("Nombre1");

MiObjeto = null;

Did I free space in Name1 memory by assigning it the null value?

    
asked by Novato 30.01.2018 в 19:01
source

3 answers

4

Technically if you would be releasing it. When you assign null to a variable, you are leaving it inaccessible. Then in java there is a process in the virtual machine called garbage collector that runs every so often and frees the memory of inaccessible objects.

    
answered by 30.01.2018 в 19:09
1

Instantly not. What you have done is remove the reference that existed to the newly created object. The memory will be released when the GC is executed, detect that the object has no references and delete it.

    
answered by 30.01.2018 в 19:09
1

No, you just changed the memory reference (you stopped using it), the Java Garbage Collector takes care of deleting the memory locations that are no longer being used.

In conclusion:

  • You do not erase it instantly, but if you tell the garbage collector to erase it for you, by not using it anymore (The garbage collector runs every so often).

A little more about the Garbage Collector

    
answered by 30.01.2018 в 19:09