作者:elvinpeng,腾讯WXG前端开发工程师
Node.js使用的是V8引擎,会自动进行垃圾回收(GarbageCollection,GC),因而写代码的时候不需要像C/C++一样手动分配、释放内存空间,方便不少,不过仍然需要注意内存的使用,避免造成内存泄漏(MemoryLeak)。
内存泄漏往往非常隐蔽,例如下面这段代码你能看出来是哪儿里有问题吗?
lettheThing=null;letreplaceThing=function(){constnewThing=theThing;constunused=function(){if(newThing)console.log("hi");};//不断修改引用theThing={longStr:newArray(1e8).join("*"),someMethod:function(){console.log("a");},};//每次输出的值会越来越大console.log(process.memoryUsage().heapUsed);};setInterval(replaceThing,);
如果可以的话,欢迎加入我们