我有一个2GB内存的VPS(Debian),我运行pm2
来执行一个node.js服务器应用程序。我有一个缓存,每天凌晨3点我都想把它转储到一个文件中作为备份。
每天下午3点,我都收到以下错误:
〈---最后几个GC---〉
[717119:0x 63345 b 0] 66921944毫秒:标记-扫描956.0(1043.7)-〉954.9(1043.7)MB,737.3 / 0.0毫秒(平均mu = 1.000,当前mu = 0.349)分配失败;清除可能无法成功[717119:0x 63345 b 0] 66922975毫秒:标记-扫描962.8(1043.7)-〉961.9(1059.7)MB,978.0 / 0.0毫秒(平均mu = 0.999,当前mu = 0.052)分配失败;清除可能不会成功
〈--- JS堆栈跟踪---〉
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory 1: 0xb6e500 node::Abort() [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js] 2: 0xa7e632 [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js] 3: 0xd47f20 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js] 4: 0xd482c7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js] 5: 0xf25685 [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js] 6: 0xf37b6d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node /home/userName/web/appName.com/public_html/appNameServer/stoc> 7: 0xf1226e v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node /home/userName/web/app> 8: 0xf13637 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node /home/userName/web/stoc> 9: 0xef3b80 v8::internal::Factory::AllocateRaw(int, v8::internal::AllocationType, v8::internal::AllocationAlignment) [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js] 10: 0xeeb5f4 v8::internal::FactoryBasev8::internal::Factory::AllocateRawWithImmortalMap(int, v8::internal::AllocationType, v8::internal::Map, v8::internal::AllocationAlignment) [node /home/userName/web/stoc>11: 0xeed988 v8::internal::FactoryBasev8::internal::Factory::NewRawTwoByteString(int, v8::internal::AllocationType) [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js] 12: 0x131ddec v8::internal::IncrementalStringBuilder::Extend() [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js] 13: 0x10349d8 v8::internal::JsonStringifier::SerializeString(v8::internal::Handlev8::internal::String) [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js] 14: 0x1035c52 v8::internal::JsonStringifier::Result v8::internal::JsonStringifier::Serialize_(v8::internal::Handlev8::internal::Object, bool, v8::internal::Handlev8::internal::Object) [node /home/di>15: 0x10378af v8::internal::JsonStringifier::Result v8::internal::JsonStringifier::Serialize_(v8::internal::Handlev8::internal::Object, bool, v8::internal::Handlev8::internal::Object) [node /home/di>16: 0x10378af v8::internal::JsonStringifier::Result v8::internal::JsonStringifier::Serialize_(v8::internal::Handlev8::internal::Object, bool, v8::internal::Handlev8::internal::Object) [node /home/di>17: 0x10378af v8::internal::JsonStringifier::Result v8::internal::JsonStringifier::Serialize_(v8::internal::Handlev8::internal::Object, bool, v8::internal::Handlev8::internal::Object) [node /home/di>18: 0x10378af v8::internal::JsonStringifier::Result v8::internal::JsonStringifier::Serialize_(v8::internal::Handlev8::internal::Object, bool, v8::internal::Handlev8::internal::Object) [node /home/di>19: 0x10378af v8::internal::JsonStringifier::Result v8::internal::JsonStringifier::Serialize_(v8::internal::Handlev8::internal::Object, bool, v8::internal::Handlev8::internal::Object) [node /home/di>20: 0x10378af v8::internal::JsonStringifier::Result v8::internal::JsonStringifier::Serialize_(v8::internal::Handlev8::internal::Object, bool, v8::internal::Handlev8::internal::Object) [node /home/di>21: 0x1037f13 v8::internal::JsonStringifier::SerializeJSReceiverSlow(v8::internal::Handlev8::internal::JSReceiver) [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js] 22: 0x1039897 v8::internal::JsonStringifier::Result v8::internal::JsonStringifier::Serialize_(v8::internal::Handlev8::internal::Object, bool, v8::internal::Handlev8::internal::Object) [node /home/d>23: 0x103ae5f v8::internal::JsonStringify(v8::internal::Isolate*, v8::internal::Handlev8::internal::Object, v8::internal::Handlev8::internal::Object, v8::internal::Handlev8::internal::Object) [node /hom>24: 0xdcdbf7 v8::internal::Builtin_JsonStringify(int, unsigned long*, v8::internal::Isolate*) [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js] 25: 0x16e9af9 [node /home/userName/web/appName.com/public_html/appNameServer/appNameServer.js]
我知道这个错误很简单,简单的解决办法就是增加RAM,但我还是想分享一些数据:
1.在崩溃之前,应用程序消耗了640MB(毛),这意味着缓存本身消耗的比这个少。
1.当我运行这个node -e 'console.log(v8.getHeapStatistics().heap_size_limit/(1024*1024))'
时,结果是1662。
1.当我在本地运行缓存转储时,导出项目大约需要3秒,但在服务器上查看pm2日志时,从尝试导出缓存项目到服务器再次备份需要14秒,好像导出需要更长的时间,或者崩溃/重启需要这么长时间,不确定。
2022年12月27日下午3时00分00秒016分导出到json 2|应用程序名称|
2022年12月27日零时00分14秒747秒:服务器正在https上运行,端口为...
根据上面的数字,640mb(总)和至少还有一个1gb留给节点的事实,我不明白为什么首先会有一个内存堆。
请指教,谢谢
1条答案
按热度按时间rdrgkggo1#
节点选项(--最大旧空间大小)
在我的情况下
但是
因此,您可以使用此变量修改可用内存量。
正在使用缓冲区写入文件
您可能正在使用
但也许你可以换成
那么你就不必同时在内存中存储文件的所有内容。
使用IDE分析
我用类似的工具解决了类似的案例:
https://www.jetbrains.com/help/webstorm/v8-cpu-and-memory-profiling.html#ws_node_cpu_profiling
在控制台中,你可以简单地用
--porf
标志启动你的脚本。它将创建巨大的,难以阅读的,但非常详细的日志,告诉内存分配。