如何要求Node.js中的垃圾收集器运行?
在启动时,我的Node.js应用似乎使用了大约200MB的内存。如果我不理一会儿,它会收缩到9MB左右。
从应用程序内部到:
- 检查应用程序正在使用多少内存?
- 要求垃圾收集器运行?
我问的原因是,我从磁盘上加载了许多文件,这些文件是临时处理的。这可能导致内存使用量。但是,在GC运行之前,我不想加载更多的文件,否则我的内存可能会用完。
有什么建议吗?
At startup, it seems my node.js app uses around 200MB of memory. If I leave it alone for a while, it shrinks to around 9MB.
Is it possible from within the app to:
- Check how much memory the app is using ?
- Request the garbage collector to run ?
The reason I ask is, I load a number of files from disk, which are processed temporarily. This probably causes the memory usage to spike. But I don't want to load more files until the GC runs, otherwise there is the risk that I will run out of memory.
Any suggestions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
- expose-gc
flag启动节点进程,则可以调用global.gc()
迫使节点运行垃圾收集。请记住,您的节点应用程序中的所有其他执行均已暂停,直到GC完成为止,因此不要经常使用它,否则会影响性能。您可能需要在代码中拨打GC调用时包括一张检查,这样,如果没有标志的节点运行,则情况并不糟糕:
If you launch the node process with the
--expose-gc
flag, you can then callglobal.gc()
to force node to run garbage collection. Keep in mind that all other execution within your node app is paused until GC completes, so don't use it too often or it will affect performance.You might want to include a check when making GC calls from within your code so things don't go bad if node was run without the flag:
当您无法通过任何原因将
传递给您的节点进程中时将gc
flacs flak flacs flak ,您可以尝试以下操作:注意:
When you cannot pass the
--expose-gc
flag to your node process on start for any reason, you may try this:Notes: