【问题标题】:Express JS MognoDB allocation failedExpress JS MongoDB 分配失败
【发布时间】:2021-06-19 03:27:57
【问题描述】:

今天我在他的控制台中收到了两次错误消息,只有重新启动后端才能解决它。我将 Node/expressjs 用于后端,将 reactjs 用于前端。后端服务于两个网站(管理员和客户端),它会导致这种错误吗?还是我只需要在 mongodb 上购买高级集群?也许只是那里没有可用空间了。请帮我弄清楚如何解决它。谢谢!

[9860:000001FB55904120]  6734212 ms: Scavenge 23.8 (28.2) -> 23.1 (29.2) MB, 2.8 / 0.0 ms  (average mu = 0.999, current mu = 1.000) allocation failure
[9860:000001FB55904120]  6734237 ms: Scavenge 23.8 (28.2) -> 23.2 (29.2) MB, 4.8 / 0.0 ms  (average mu = 0.999, current mu = 1.000) allocation failure
[9860:000001FB55904120]  6734552 ms: Scavenge 24.1 (28.2) -> 23.4 (29.2) MB, 6.3 / 0.0 ms  (average mu = 0.999, current mu = 1.000) allocation failure


<--- JS stacktrace --->

FATAL ERROR: Committing semi space failed. Allocation failed - JavaScript heap out of memory
 1: 00007FF60314052F napi_wrap+109311
 2: 00007FF6030E5256 v8::internal::OrderedHashTable<v8::internal::OrderedHashSet,1>::NumberOfElementsOffset+33302
 3: 00007FF6030E6026 node::OnFatalError+294
 4: 00007FF6039B163E v8::Isolate::ReportExternalAllocationLimitReached+94
 5: 00007FF6039964BD v8::SharedArrayBuffer::Externalize+781
 6: 00007FF60384094C v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1516
 7: 00007FF603848E9F v8::internal::Heap::PageFlagsAreConsistent+2559
 8: 00007FF60383DA61 v8::internal::Heap::CollectGarbage+2033
 9: 00007FF60383BC65 v8::internal::Heap::AllocateExternalBackingStore+1317
10: 00007FF60385C057 v8::internal::Factory::NewFillerObject+183
11: 00007FF60358C0B1 v8::internal::interpreter::JumpTableTargetOffsets::iterator::operator=+1409
12: 00007FF603A39FED v8::internal::SetupIsolateDelegate::SetupHeap+463949
13: 00000343072A216B
[nodemon] app crashed - waiting for file changes before starting...

这是错误发生前的请求:

POST /api/8547685765867gurhgfbgjbfgngbgjhtiu5895785hefjsfsfGGGGGGGGGGGGGGGGGGGGGGGGGGG847389jnem/admin/articlesaweek - - ms - -

这是路由器:

router.post("/" + process.env.HASHFORADMIN +"/admin/articlesaweek", async (req, res) => {
  res.send(await adminactions.articlesInAWeek(req, res))
})

这是控制器的功能

articlesInAWeek : async (req, res) => {
    const data =  [ (await Article.find({date: { $gt: new Date((new Date( )).getFullYear(),
              new Date().getMonth(),
              new Date(Date.now()).getDay()
            )}})).length, (await Article.find({date: { $gt: new Date((new Date( )).getFullYear(),
              new Date().getMonth(),
              new Date(Date.now()).getDay() - 1
            )}})).length, (await Article.find({date: { $gt: new Date((new Date( )).getFullYear(),
              new Date().getMonth(),
              new Date(Date.now()).getDay() - 2
            )}})).length, (await Article.find({date: { $gt: new Date((new Date( )).getFullYear(),
              new Date().getMonth(),
              new Date(Date.now()).getDay() - 3
            )}})).length, 
            (await Article.find({date: { $gt: new Date((new Date( )).getFullYear(),
              new Date().getMonth(),
              new Date(Date.now()).getDay() - 4
            )}})).length,
            (await Article.find({date: { $gt: new Date((new Date( )).getFullYear(),
              new Date().getMonth(),
              new Date(Date.now()).getDay() - 5
            )}})).length,
            (await Article.find({date: { $gt: new Date((new Date( )).getFullYear(),
              new Date().getMonth(),
              new Date(Date.now()).getDay() - 6
            )}})).length
            ];
            return data;
  },

【问题讨论】:

    标签: javascript node.js database mongodb express


    【解决方案1】:

    该错误是因为您调用了多个查找查询 每个查询获取记录并保存在堆内存中。 它超过了堆大小 因此将查询更改为以下,然后您只能获取文档数量,而不是将所有文档加载到堆中

    articlesInAWeek: async (req, res) => {
        const data = [(await Article.countDocuments({ date: { $gt: new Date((new Date()).getFullYear(), new Date().getMonth(), new Date(Date.now()).getDay()) } })),
        (await Article.countDocuments({
            date: {
                $gt: new Date((new Date()).getFullYear(),
                    new Date().getMonth(),
                    new Date(Date.now()).getDay() - 1
                )
            }
        })),
        (await Article.countDocuments({
            date: {
                $gt: new Date((new Date()).getFullYear(),
                    new Date().getMonth(),
                    new Date(Date.now()).getDay() - 2
                )
            }
        })),
        (await Article.countDocuments({
            date: {
                $gt: new Date((new Date()).getFullYear(),
                    new Date().getMonth(),
                    new Date(Date.now()).getDay() - 3
                )
            }
        })),
        (await Article.countDocuments({
            date: {
                $gt: new Date((new Date()).getFullYear(),
                    new Date().getMonth(),
                    new Date(Date.now()).getDay() - 4
                )
            }
        })),
        (await Article.countDocuments({
            date: {
                $gt: new Date((new Date()).getFullYear(),
                    new Date().getMonth(),
                    new Date(Date.now()).getDay() - 5
                )
            }
        })),
        (await Article.countDocuments({
            date: {
                $gt: new Date((new Date()).getFullYear(),
                    new Date().getMonth(),
                    new Date(Date.now()).getDay() - 6
                )
            }
        }))
        ];
        return data;
    },
    

    【讨论】:

      【解决方案2】:

      V8 中对内存使用有严格的标准限制。您最多只能使用 1.7 GB。虽然你可以试试。

      node yourScript.js --max-old-space-size=8192

      也在查看您的代码之后。而不是将文档加载到内存中并查找长度。尝试使用aggregationcount

      【讨论】:

        猜你喜欢
        • 2015-10-11
        • 2022-08-18
        • 1970-01-01
        • 2016-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-21
        • 1970-01-01
        相关资源
        最近更新 更多