Reza
Reza

Reputation: 3919

Does this code lead to memory leak in JavaScript

Please take a look at the following code:

universe.makeFramework = function()
{
    const fw = Framework();
    fw.version = 0;
    fw.uni = this;
    universe.fws.add( fw ); // keep a reference of framework in a set
    fw.realise( universe.ctrl );
    idToObj.set( nextId, fw ); // keep a reference of framework in a map
    ++nextId;
}

Basically, I have a universe which could have one or more framework object. In this function (makeFramework) I create a new framework and keep its reference in one map and one set.

Now assume a situation that I want to remove this framework. So I have to remove the references from the map, set and also DOM. Is that enough? Should I worry about the const fw? Should I do something like below?

universe.makeFramework = function()
    {
        let fw = Framework();
        fw.version = 0;
        fw.uni = this;
        universe.fws.add( fw ); // keep a reference of framework in a set
        fw.realise( universe.ctrl );
        idToObj.set( nextId, fw ); // keep a reference of framework in a map
        ++nextId;
        fw = undefined;
    }

Upvotes: 0

Views: 36

Answers (1)

mbojko
mbojko

Reputation: 14679

No need. The garbage collector will take care of the fw constant after makeFramework has finished its run.

Upvotes: 1

Related Questions