Nodejs在findOne Sequelize方法期间堆内存不足,并进行了紧急加载



My NodeJS应用程序当前使用Sequelize对象跨多个阶段工作。此对象是Model.findOne操作的结果,该操作包括其他几个模型,类似于纯SQL中多个表的左联接。

transaction  = await Transaction.findOne({
where: {
id: transaction.id
},
include: transactionIncludes //this includes other models such as client, orderDetail, orderLog, etc
})
}
return transaction

有时,我的应用程序可以很好地挂载这个事务对象。然而,当我尝试扩展它时,我经常会遇到以下OOM错误,例如:

<--- Last few GCs --->
[736:0x60632a0]    72273 ms: Mark-sweep (reduce) 2036.3 (2045.7) -> 2036.2 (2045.7) MB, 13.2 / 0.0 ms  (+ 0.1 ms in 4 steps since start of marking, biggest step 0.1 ms, walltime since start of marking 184 ms) (average mu = 0.943, current mu = 0.951) final[736:0x60632a0]    72342 ms: Mark-sweep (reduce) 2041.9 (2051.4) -> 2041.9 (2051.4) MB, 10.9 / 0.0 ms  (+ 0.5 ms in 4 steps since start of marking, biggest step 0.3 ms, walltime since start of marking 64 ms) (average mu = 0.931, current mu = 0.833) finali
<--- JS stacktrace --->
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
1: 0xb2c2b0 node::Abort() [node]
2: 0xa4025c node::FatalError(char const*, char const*) [node]
3: 0xd1d11e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
4: 0xd1d497 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
5: 0xed68f5  [node]
6: 0xee7d3d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
7: 0xee8bf7 v8::internal::Heap::FinalizeIncrementalMarkingIfComplete(v8::internal::GarbageCollectionReason) [node]
8: 0xeec6b0 v8::internal::IncrementalMarkingJob::Task::RunInternal() [node]
9: 0xddae0b non-virtual thunk to v8::internal::CancelableTask::Run() [node]
10: 0xb9a7f4 node::PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<v8::Task, std::default_delete<v8::Task> >) [node]
11: 0xb9c669 node::PerIsolatePlatformData::FlushForegroundTasksInternal() [node]
12: 0x159d8a6  [node]
13: 0x15afe14  [node]
14: 0x159e1f8 uv_run [node]
15: 0xa65fe5 node::SpinEventLoop(node::Environment*) [node]
16: 0xb6ed86 node::NodeMainInstance::Run(node::EnvSerializeInfo const*) [node]
17: 0xaef83a node::Start(int, char**) [node]
18: 0x7f8cda3cb09b __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6]
19: 0xa62f6c  [node]
Aborted (core dumped)

转储的核心大约有2吉的内存。我想在不增加RAM的情况下解决这个问题。

编辑:有人要求我展示带有模型关联的include,它们在这里:

[redacted]

如果Sequelize查询中包含多个hasMany关联(在同一级别或嵌套级别上(,则需要为所有关联指示separate: true,因此Sequelize将单独执行所有此类包含的查询,以避免记录倍增:

export const transactionIncludes = [
{model: CartaoPagamentos, as: 'cartaopagamento', type: 'hasOne', foreignKey: 'transaction_id'},
{model: Cliente, as: 'cliente', type: 'belongsTo', foreignKey: 'cliente_id'},
{model: Fila, as: 'fila', type: 'hasOne', foreignKey: 'transaction_id'},
{model: Voo, as: 'voo', type: 'hasMany', foreignKey: 'transaction_id', include: VooIncludes, separate: true }, 
{model: Passageiros, as: 'passageiro', type: 'hasMany', foreignKey: 'transaction_id', separate: true },
{model: Cartaomilhas, as: 'cartaomilha', type: 'hasOne', foreignKey: 'transaction_id'},
{model: Robot, as: 'robot', type: 'belongsTo', foreignKey: 'robo_id'},
];

此外,请查看我在这里的回答(关于hasMany的部分,以更好地了解当您在没有separate: true选项的Sequelize查询中指示多个hasMany时,引擎盖下发生了什么。

最新更新