I have heard somewhere that if we have a large object and we want to re-initialize it

BigObject bigObject = new BigObject(); // do something bigObject = new BigObject(); 

It is better to first equate it to null and only then initialize it again.

 BigObject bigObject = new BigObject(); // do something bigObject = null; bigObject = new BigObject(); 

Allegedly, then the garbage collector will work more efficiently. Is it true?

    4 answers 4

    “There is one case in which the use of forced zeroing is not only useful, but actually obligatory, when a reference to an object has a greater scope than the one in which it is actually used or is considered valid in the program specification. This includes such cases as using instead a local variable of a static field or an object instance field for storing a reference to a temporary buffer or using an array to store references that may remain accessible to the runtime but not for the implicit semantics of the program. " (with)
    I would advise you to familiarize yourself with the history of the development of memory utilization technology and then fix the garbage collection and performance.

      The answer is yes and no. If when creating an object the garbage collection starts, then if you do not nullify the link, then it turns out that the old link is still alive, because the assignment will take place after the object is created. So, theoretically, setting the link to null will reduce the likelihood of a fall by OOM. On the other hand, a similar situation most likely means that something was not thought through in your application. And what's more, unnecessary links are a waste of resources. Not to mention that setting a variable to null will not be visible on other processors, so the collector may still not see the changes. So really, it makes no sense. And it certainly does not make the collector work more efficiently.

      PS: so actually, the manipulations with the link are not on the side, but there is very little use from them.

        On the IBM website it was said that zeroing references only confuses the collector. Garbage collection is a one-time process, started by a Wirth machine and collecting all the inaccessible links at the time of launch (as the standard says). Any dances with pointers to his side.

          Unless specifically to answer your question - no, this is not true.

          Assigning null is only appropriate if between = null; and the following = new BigObject(); any lengthy actions are performed, with a duration exceeding the pauses between assemblies (with the standard strategy they will be no less than a minute, and in most cases more, especially if the heap is free.

          Those. when the lifetime of a variable is much longer than the required lifetime of the object itself.

          By the way, this has nothing to do with IBM, all modern JVMs somehow or all clean the heap almost equally.