Nuxt和Axios-JavaScript堆内存不足

时间:2020-04-03 10:49:48

标签: javascript axios nuxt.js

我不知道Nuxt和Axios发生了什么,在用户会话到期并且API返回状态401之后,如果我重新加载页面,页面会卡住,几分钟后,它将在控制台中返回此错误:

<--- Last few GCs --->

[85937:0x108000000]  4259490 ms: Mark-sweep 2025.8 (2050.9) -> 2025.0 (2050.9) MB, 1638.9 / 0.0 ms  (average mu = 0.111, current mu = 0.006) allocation failure scavenge might not succeed
[85937:0x108000000]  4261994 ms: Mark-sweep 2025.9 (2050.9) -> 2025.0 (2051.2) MB, 2495.0 / 0.0 ms  (average mu = 0.050, current mu = 0.004) allocation failure scavenge might not succeed


<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x100950919]
    1: StubFrame [pc: 0x1009523cb]
Security context: 0x0e33ea8c08d1 <JSObject>
    2: exec [0xe33ea8d49b1](this=0x0e3338818389 <JSRegExp <String[#10]: %[dfijoOs]>>,0x0e33ea8f5d61 <String[#51]: Cannot %s headers after they are sent to the client>)
    3: [Symbol.match] [0xe33ea8d4899](this=0x0e3338818389 <JSRegExp <String[#10]: %[dfijoOs]>>,0x0e33ea8f5d61 <String[#51]: Cannot %s headers after they are sen...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Writing Node.js report to file: report.20200403.123410.85937.0.001.json
Node.js report completed
 1: 0x100080c68 node::Abort() [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
 2: 0x100080dec node::errors::TryCatchScope::~TryCatchScope() [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
 3: 0x100185167 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
 4: 0x100185103 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
 5: 0x10030b2f5 v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
 6: 0x10030c9c4 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
 7: 0x100309837 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
 8: 0x1003077fd v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
 9: 0x100312fba v8::internal::Heap::AllocateRawWithLightRetry(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
10: 0x100313041 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
11: 0x1002e035b v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
12: 0x100618718 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
13: 0x100950919 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]
14: 0x1009523cb Builtins_SubString [/Users/aaa/.nvm/versions/node/v12.16.1/bin/node]

我正在使用的Node版本是 12.16.1

Axios插件文件是这样的:

import https from 'https'

export default function({ $axios, redirect }) {
  const agent = new https.Agent({
    rejectUnauthorized: false
  })

  // Axios default options
  $axios.defaults.baseURL = process.env.apiBaseUrl
  $axios.setHeader('Content-Type', 'application/json')
  $axios.setHeader('Access-Control-Allow-Origin', '*')

  $axios.onRequest(config => {
    config.httpsAgent = agent
  })

  $axios.onError(({ response }) => {
    const statusCode = response.status
    const data = response.data
    switch (statusCode) {
      case 401:
        redirect('/session-expired')
        break
      case 404:
        let path = '/404'
        redirect(path)
        break
      case 500:
        return this.$nuxt.error({ statusCode })
      default:
        return false
    }
  })
}

@nuxt/axios版本是最新版本:5.9.7。

我认为问题出在onError方法中,因为一旦我将其注释掉,便可以运行该应用程序...为什么会发生?

2 个答案:

答案 0 :(得分:1)

Nuxt的dev模式似乎具有内存泄漏的悠久历史。尝试将standalone: true添加到build中的nuxt.config对象中,这样看起来可能像这样:

export default {
  build: {
    standalone: true,
 }
}

如果这对您没有帮助,也许其中一些github问题和拉取请求可以:#6478 #5452 #286 #685

答案 1 :(得分:0)

我也有这个问题,但我找到了一个非常明显的答案。

请检查您的组件的名称。就我而言,我有 2 个具有相同名称的组件。

export default {
   name: "Test"
}

这样我就无法运行我的应用程序,而且我遇到了和你一样的错误。也许这会帮助某人