我有一个 Express.js Web 应用程序,由于内存问题,它经常崩溃:
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap
out of memory
Run Code Online (Sandbox Code Playgroud)
当我启动服务时,内存使用量约为 170Mb(由 htop 报告为 RES 内存)。然后我开始向它发出请求,内存开始上升。我曾经memwatch尝试跟踪看起来像内存泄漏的问题,但memwatch.HeapDiff()报告如下:
{ before: { nodes: 2093729, size_bytes: 197165296, size: '188.03 mb' },
after: { nodes: 2491264, size_bytes: 232097040, size: '221.34 mb' },
...
Run Code Online (Sandbox Code Playgroud)
...我了解到应用程序正在使用 221.34mb 作为堆;但htop坏掉时内存使用量约为2GB。
问题是,如果我的假设是正确的,那么什么可能会使用 htop 仍将其报告为 RES 内存的非堆内存?
我有一段遗留代码,它在每次请求时在服务器上呈现一个react组件,这显然存在内存泄漏.我已经解决了这个代码的问题:
componentWillMount: function () {
var onLogin = this.props.onLogin || function () {},
onLogout = this.props.onLogout || function () {};
this.on('authChange', function () {
console.log('user authenticated:', this.state.isAuthenticated);
return this.state.isAuthenticated
? onLogin(this.state)
: onLogout(this.state);
}.bind(this));
},
Run Code Online (Sandbox Code Playgroud)
我相信在每个请求this对象都存储一个新的监听器,但我不明白为什么在this完成组件的渲染时元素没有被标记为垃圾.