我一直在使用Sails.js(Node.js的框架)开发应用程序,并且它在我的本地环境和AWS上都运行良好。然而,最近,其中一台服务器崩溃了“处理器内存不足”#34;错误。实际上,一台服务器现在在运行npm start
的一秒钟内崩溃。我之前通过制作服务器的图像并在较大的服务器上将其作为新实例启动来暂时修复此问题,但是这样的立即崩溃不仅仅是需要额外空间的问题。
我认为我的代码导致内存泄漏,但我无法找到任何线索来确定问题的根源。 在Node.js应用程序中调试内存分配问题的最佳工具是什么?
编辑:我知道the -inspect
flag并且我正在探讨这一点,但我特别好奇我如何监控记忆我的应用程序在远程服务器上运行。
这是崩溃服务器的错误输出,如果有任何帮助的话。它运行在一个带有Node v8.1.2和npm v5.6.0的t2.large EC2实例上。
[34mdebug: [39mhookPath: /home/bitnami/emotics-sails-api/node_modules/sails-auth/dist/api/hooks/auth
[34mdebug: [39mmarlinspike (auth): loading config from /home/bitnami/emotics-sails-api/node_modules/sails-auth/dist/config
[34mdebug: [39mmarlinspike (auth): loading Services from /home/bitnami/emotics-sails-api/node_modules/sails-auth/dist/api/services...
[34mdebug: [39mmarlinspike (auth): loading Models...
[34mdebug: [39mmarlinspike (auth): loading Controllers...
[34mdebug: [39mmarlinspike (auth): loading Policies...
[34mdebug: [39m-------------------------------------------------------
[34mdebug: [39m:: Wed Feb 28 2018 05:18:29 GMT+0000 (UTC)
[34mdebug: [39mEnvironment : development
[34mdebug: [39mPort : 80
[34mdebug: [39m-------------------------------------------------------
[31merror: [39mGrunt :: FATAL ERROR: NewSpace::Rebalance Allocation failed - process out of memory
1: node::Abort() [grunt]
2: 0x13647ec [grunt]
3: v8::Utils::ReportOOMFailure(char const*, bool) [grunt]
4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [grunt]
5: 0xa9686b [grunt]
6: v8::internal::MarkCompactCollector::EvacuateNewSpaceAndCandidates() [grunt]
7: v8::internal::MarkCompactCollector::CollectGarbage() [grunt]
8: v8::internal::Heap::MarkCompact() [grunt]
9: v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [grunt]
10: v8::internal::Heap::CollectGarbage(v8::internal::GarbageCollector, v8::internal::GarbageCollectionReason, char const*, v8::GCCallbackFlags) [grunt]
11: v8::internal::Factory::NewWeakCell(v8::internal::Handle<v8::internal::HeapObject>) [grunt]
12: v8::internal::SharedFunctionInfo::SetScript(v8::internal::Handle<v8::internal::SharedFunctionInfo>, v8::internal::Handle<v8::internal::Object>) [grunt]
13: v8::internal::Factory::NewSharedFunctionInfoForLiteral(v8::internal::FunctionLiteral*, v8::internal::Handle<v8::internal::Script>) [grunt]
14: v8::internal::Compiler::GetSharedFunctionInfo(v8::internal::FunctionLiteral*, v8::internal::Handle<v8::internal::Script>, v8::internal::CompilationInfo*) [grunt]
15: v8::internal::FullCodeGenerator::VisitFunctionLiteral(v8::internal::FunctionLiteral*) [grunt]
16: v8::internal::FullCodeGenerator::VisitForAccumulatorValue(v8::internal::Expression*) [grunt]
17: v8::internal::FullCodeGenerator::VisitAssignment(v8::internal::Assignment*) [grunt]
18: v8::internal::FullCodeGenerator::VisitExpressionStatement(v8::internal::ExpressionStatement*) [grunt]
19: v8::internal::FullCodeGenerator::Generate() [grunt]
20: v8::internal::FullCodeGenerator::MakeCode(v8::internal::CompilationInfo*, unsigned long) [grunt]
21: v8::internal::FullCodegenCompilationJob::ExecuteJobImpl() [grunt]
22: v8::internal::CompilationJob::ExecuteJob() [grunt]
23: 0xd97d90 [grunt]
24: 0xd98b28 [grunt]
25: 0xd98d8f [grunt]
26: 0xd9ce59 [grunt]
27: v8::internal::Compiler::Compile(v8::internal::Handle<v8::internal::JSFunction>, v8::internal::Compiler::ClearExceptionFlag) [grunt]
28: v8::internal::Runtime_CompileLazy(int, v8::internal::Object**, v8::internal::Isolate*) [grunt]
29: 0xcf123c0437d
我尝试更新npm,但内存也用完了。有没有办法在CLI中清理内存?可能会有一个我不知道的进程吗?
bitnami@ip-172-31-##-##:~/my-project$ sudo npm i -g npm
Error: could not get uid/gid
FATAL ERROR: Committing semi space failed. Allocation failed - process out of memory
1: node::Abort() [/opt/bitnami/nodejs/bin/.node.bin]
2: 0x13647ec [/opt/bitnami/nodejs/bin/.node.bin]
3: v8::Utils::ReportOOMFailure(char const*, bool) [/opt/bitnami/nodejs/bin/.node.bin]
4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/opt/bitnami/nodejs/bin/.node.bin]
5: v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/opt/bitnami/nodejs/bin/.node.bin]
6: v8::internal::Heap::CollectGarbage(v8::internal::GarbageCollector, v8::internal::GarbageCollectionReason, char const*, v8::GCCallbackFlags) [/opt/bitnami/nodejs/bin/.node.bin]
7: v8::internal::Factory::NewRawOneByteString(int, v8::internal::PretenureFlag) [/opt/bitnami/nodejs/bin/.node.bin]
8: v8::internal::Factory::NewConsString(v8::internal::Handle<v8::internal::String>, v8::internal::Handle<v8::internal::String>) [/opt/bitnami/nodejs/bin/.node.bin]
9: v8::internal::AstValueFactory::Internalize(v8::internal::Isolate*) [/opt/bitnami/nodejs/bin/.node.bin]
10: 0xd99742 [/opt/bitnami/nodejs/bin/.node.bin]
11: v8::internal::Compiler::GetSharedFunctionInfoForScript(v8::internal::Handle<v8::internal::String>, v8::internal::Handle<v8::internal::Object>, int, int, v8::ScriptOriginOptions, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Context>, v8::Extension*, v8::internal::ScriptData**, v8::ScriptCompiler::CompileOptions, v8::internal::NativesFlag) [/opt/bitnami/nodejs/bin/.node.bin]
12: v8::ScriptCompiler::CompileUnboundInternal(v8::Isolate*, v8::ScriptCompiler::Source*, v8::ScriptCompiler::CompileOptions) [/opt/bitnami/nodejs/bin/.node.bin]
13: v8::ScriptCompiler::CompileUnboundScript(v8::Isolate*, v8::ScriptCompiler::Source*, v8::ScriptCompiler::CompileOptions) [/opt/bitnami/nodejs/bin/.node.bin]
14: 0x1393897 [/opt/bitnami/nodejs/bin/.node.bin]
15: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [/opt/bitnami/nodejs/bin/.node.bin]
16: 0xb43b3b [/opt/bitnami/nodejs/bin/.node.bin]
17: v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) [/opt/bitnami/nodejs/bin/.node.bin]
18: 0x21e4b490437d
at /opt/bitnami/nodejs/lib/node_modules/npm/node_modules/uid-number/uid-number.js:37:16
at ChildProcess.exithandler (child_process.js:277:5)
at emitTwo (events.js:125:13)
at ChildProcess.emit (events.js:213:7)
at maybeClose (internal/child_process.js:897:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:208:5)
TypeError: Cannot read property 'get' of undefined
at errorHandler (/opt/bitnami/nodejs/lib/node_modules/npm/lib/utils/error-handler.js:205:17)
at /opt/bitnami/nodejs/lib/node_modules/npm/bin/npm-cli.js:83:20
at cb (/opt/bitnami/nodejs/lib/node_modules/npm/lib/npm.js:224:22)
at /opt/bitnami/nodejs/lib/node_modules/npm/lib/npm.js:262:24
at /opt/bitnami/nodejs/lib/node_modules/npm/lib/config/core.js:81:7
at Array.forEach (native)
at /opt/bitnami/nodejs/lib/node_modules/npm/lib/config/core.js:80:13
at f (/opt/bitnami/nodejs/lib/node_modules/npm/node_modules/once/once.js:25:25)
at afterExtras (/opt/bitnami/nodejs/lib/node_modules/npm/lib/config/core.js:178:20)
at Conf.<anonymous> (/opt/bitnami/nodejs/lib/node_modules/npm/lib/config/core.js:236:22)
at /opt/bitnami/nodejs/lib/node_modules/npm/node_modules/uid-number/uid-number.js:39:14
at ChildProcess.exithandler (child_process.js:277:5)
at emitTwo (events.js:125:13)
at ChildProcess.emit (events.js:213:7)
at maybeClose (internal/child_process.js:897:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:208:5)
/opt/bitnami/nodejs/lib/node_modules/npm/lib/utils/error-handler.js:205
if (npm.config.get('json')) {
^
TypeError: Cannot read property 'get' of undefined
at process.errorHandler (/opt/bitnami/nodejs/lib/node_modules/npm/lib/utils/error-handler.js:205:17)
at emitOne (events.js:115:13)
at process.emit (events.js:210:7)
at process._fatalException (bootstrap_node.js:329:26)