我正在使用Mongodb在aws t2micro实例上运行nodeJS应用程序。这就是我在服务器上的index.js文件的样子
const db = require('./src/db');
const coins = require('./src/scrapers/populate-coins');
const scrape = require('./src/scrapers/index.js');
const settings = require('./src/trading/init-settings.js');
const trading = require('./src/trading/index.js');
const server = require('./src/index.js');
此处刮擦和交易是在服务器处理API请求时在setInterval内运行的进程。我正在使用一个角度前端,并将整个事件部署在单个aws t2micro实例上,具有1GB RAM和16GB存储空间。事情进展顺利,直到db中的数据无关紧要。一旦我在其中一个表中有大于10K行的数据,API调用开始花费大量时间,最多1分钟。在开发工具中,它表明等待(TTFB)耗时约40秒。检查日志后我得到了这个
0|trading | Warning: connect.session() MemoryStore is not
0|trading | designed for a production environment, as it will leak
0|trading | memory, and will not scale past a single process.
0|trading | FATAL ERROR: Committing semi space failed. Allocation failed - process out of memory
0|trading | 1: node::Abort() [node /home/ec2-user/app/bc-trading/index.js]
0|trading | 2: 0x13740dc [node /home/ec2-user/app/bc-trading/index.js]
0|trading | 3: v8::Utils::ReportOOMFailure(char const*, bool) [node /home/ec2-user/app/bc-trading/index.js]
0|trading | 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [node /home/ec2-user/app/bc-trading/index.js]
0|trading | 5: v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node /home/ec2-user/app/bc-trading/index.js]
0|trading | 6: v8::internal::Heap::CollectGarbage(v8::internal::GarbageCollector, v8::internal::GarbageCollectionReason, char const*, v8::GCCallbackFlags) [node /home/ec2-user/app/bc-trading/index.js]
0|trading | 7: v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [node /home/ec2-user/app/bc-trading/index.js]
0|trading | 8: v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [node /home/ec2-user/app/bc-trading/index.js]
答案 0 :(得分:0)
我建议添加日志记录,以检查所处理对象的大小是否较大或执行该功能并监视服务器内存/ cpu使用情况,它可能是mongoDb或正在使用所有资源的节点进程,因此导致错误/响应缓慢,并且还会在每个功能开始之前和结束之后记录服务器指标。
您还应该考虑到,如果数据库中有足够的记录(即使是开发环境),则在1 gb的ram上运行angular,node和mongoDb也会引起问题,节点垃圾收集器可能无法释放内存,并且最终会积聚一切,使一切变慢。