简体   繁体   中英

Tensorflow.js confusing memory leak problem?

it seems that this program causes memory leak.when executing this,the memory keep increasing to 2GB and OOM. i am a new developer of tfjs and i found that the simple eg. causes OOM in my computer

let tf=require("@tensorflow/tfjs");
let leak=()=>{

        let model=tf.sequential();

        tf.tidy(()=>{
            model.add(tf.layers.dense({
                units: 6,
                inputShape: [3]
            }));

        })

        model.dispose();

        console.log(tf.memory().numTensors);
}

let loop=()=>{

    for(let i=0;i<100;i++){
        leak();
    }   

    setTimeout(loop,1);
};

setTimeout(loop,1);

No tensors is effectively used so tf.memory().numTensors will return 0 which means that there is no data kept by tf backend.

Even without considering using tfjs, the following code will crash after a certain time.

let leak = () => {

    console.log("tf.memory().numTensors");
}

let loop = () => {

    for (let i = 0; i < 100; i++) {
        leak();
        console.log('i', i)
    }
  
    setTimeout(loop, 1);
};

setTimeout(loop, 1);

There are two many callbacks that are stacked in the event loop. This keeps on increasing as setTimeout is called for each millisecond. In the above code the memory used is not that significant. And it will take longer to notice the issue. But when using tf object, the object stacked is bigger.

Increasing the time before the execution, can allow the callback to be executed and Garbage Collected before the next call of the callback or will simply reduce how many items are stacked in the event loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM