“渲染时 NestJS 应用程序中 JavaScript 堆内存不足错误”

问题描述 投票:0回答:1

我正在 Render 上部署 NestJS 应用程序,在运行时遇到“JavaScript 堆内存不足”错误。以下是我迄今为止采取的详细信息和步骤:

错误信息:

致命错误:达到堆限制分配失败 - JavaScript 堆内存不足

环境:

  • Node.js 版本: 20.15.1
  • NestJS 版本: 9.x
  • 渲染配置:使用默认渲染实例

采取的步骤:

  1. 增加内存分配: 我将
    --max-old-space-size=2048
    添加到
    package.json
    中的启动脚本中,以增加 Node.js 内存限制。这是修改后的
    package.json
    脚本:
   "scripts": {
     "start": "node --max-old-space-size=2048 node_modules/.bin/nest start"
   }
tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "typeRoots": ["./node_modules/@types", "./types"]
  }
}

附加信息: 该应用程序处理来自 MongoDB 数据库的大型数据集。 我使用 Mongoose 进行数据库操作。 问题: 我可以应用任何其他配置或优化来进一步提高内存效率吗? 有没有更好的方法来处理 NestJS 应用程序中的大型数据集以防止内存泄漏? 我的应用程序代码中是否存在需要调查潜在内存问题的特定区域?

node.js mongodb memory-leaks nestjs typescript-typings
1个回答
0
投票

那为什么不简单地增加到4048呢?我曾经在构建过程中遇到过这个错误,所以这条线就可以工作。

NODE_OPTIONS=--max-old-space-size=4096 npm run build
© www.soinside.com 2019 - 2024. All rights reserved.