最近我遇到了一个node.js API的问题,我的内存随着每个请求变得越来越大.我正在Heroku上使用他们的免费版本托管我的服务器,该版本只有512MB的RAM.在周末获得大量流量之后,我开始从Heroku开始超出内存错误,所以我开始在我的代码中搜索内存泄漏无济于事.我没有留下任何物品,一切都应该得到清理,坦率地说,我迷路了.
但是,在做了一些研究之后,我发现node.js在达到max-old-space-size变量时运行垃圾收集器,在64位系统上默认为1024MB.我把它设置为410(我可用内存的80%)但是想知道我是否应该在代码中处理这个问题?显然,升级我的实例并且只有正常的默认上限是理想的,但现在不是一个选项.
例:
// lets assume there is some apiGet function
// that calls back with a very very large object with
// the following structure:
// {
// status: "success",
// statusCode: 200,
// messages: [],
// data: { users: [ huge array of users ] }
// }
// we have a manipulateData function that's going
// to do some stuff to the returned data and the
// call some callback to give the data to the …Run Code Online (Sandbox Code Playgroud)