You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用多Webpack配置时触发JavaScript堆内存不足错误求助

环境信息
  • OS:Ubuntu 20.04
  • Node版本:10.19.0
  • Npm版本:6.13.4
错误日志

Webpack正在监听文件……

<--- 最近几次GC信息 --->

[61869:0x3bd1f40]    70396 ms: Scavenge 1376.0 (1408.9) -> 1375.5 (1409.4) MB, 2.0 / 0.0 ms  (average mu = 0.113, current mu = 0.031) allocation failure 
[61869:0x3bd1f40]    70403 ms: Scavenge 1376.3 (1409.4) -> 1376.0 (1409.4) MB, 2.1 / 0.0 ms  (average mu = 0.113, current mu = 0.031) allocation failure 
[61869:0x3bd1f40]    70409 ms: Scavenge 1376.7 (1409.4) -> 1376.1 (1409.4) MB, 1.8 / 0.0 ms  (average mu = 0.113, current mu = 0.031) allocation failure 

<--- JS调用栈 --->

==== JS调用栈详情 =========================================

    0: ExitFrame [pc: 0x1cc2e85be1d]
    1: StubFrame [pc: 0x1cc2e84b476]
Security context: 0x292c6829e6e9 <JSObject>
    2: replace [0x292c682905e9](this=0x324e2654f9c1 <Very long string[2361875]>,0x295d21844be9 <JSRegExp <String[44]: [\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]>>,0x295d21844c21 <JSFunction (sfi = 0x26176cf315d1)>)
    3: make_string(aka make_string) [0x21aba38a2ff9] [0x31ffe51826f1 <undefined>:4027] [bytecode=0x26176cf3...

致命错误:堆内存限制附近标记压缩无效,分配失败 - JavaScript堆内存不足

1: 0x8fa090 node::Abort() [node]
 2: 0x8fa0dc  [node]
 3: 0xb0052e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: 0xb00764 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xef4c72  [node]
 6: 0xef4d78 v8::internal::Heap::CheckIneffectiveMarkCompact(unsigned long, double) [node]
 7: 0xf00e52 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
 8: 0xf01784 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 9: 0xf043f1 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [node]
10: 0xecd4e6 v8::internal::Factory::AllocateRawArray(int, v8::internal::PretenureFlag) [node]
11: 0xecdd6a v8::internal::Factory::NewFixedArrayWithFiller(v8::internal::Heap::RootListIndex, int, v8::internal::Object*, v8::internal::PretenureFlag) [node]
12: 0xece2f7 v8::internal::Factory::NewFixedArrayWithHoles(int, v8::internal::PretenureFlag) [node]
13: 0x11a4a4c  [node]
14: 0x11a6bf9 v8::internal::Runtime_RegExpExecMultiple(int, v8::internal::Object**, v8::internal::Isolate*) [node]
15: 0x1cc2e85be1d 
Aborted (core dumped)

npm错误日志:

npm ERR! code ELIFECYCLE
npm ERR! errno 134
npm ERR! my-app@0.1.0 watch: `webpack -d --watch`
npm ERR! Exit status 134
npm ERR! 
npm ERR! Failed at the my-app@0.1.0 watch script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Webpack配置
let config_one={some code}
let config_two={some code}

module.exports=[config_one,config_two]
解决方案

1. 增加Node.js堆内存限制

修改package.json中的watch脚本,添加--max-old-space-size参数扩大堆内存(示例设置为4GB):

"scripts": {
  "watch": "node --max-old-space-size=4096 node_modules/webpack/bin/webpack.js -d --watch"
}

2. 优化多配置构建逻辑

  • 拆分构建任务,避免同时运行两个大型配置:
"scripts": {
  "watch:config1": "webpack --config config1.js --watch",
  "watch:config2": "webpack --config config2.js --watch"
}

分别在两个终端执行对应脚本,分散内存压力。

  • 提取两个配置中的公共loader、plugin到单独文件复用,减少重复初始化的内存消耗。

3. 排查内存泄漏点

从调用栈看,内存消耗集中在超长字符串的正则替换操作:

  • 优化项目中处理大文本的正则逻辑,避免复杂匹配;
  • 拆分超长字符串的处理流程,避免一次性加载过大内容;
  • 升级Node.js到v12+版本(当前10.x版本较老,内存管理机制有优化空间),同时同步升级Webpack及相关依赖至稳定版。

内容的提问来源于stack exchange,提问作者Jatin Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 05:50:34