CentOS下npm run build不占用swap内存,报堆内存不足如何解决?
解决CentOS下
npm run build无法利用Swap内存导致JS堆内存不足的问题 问题场景
在CentOS系统执行npm run build(实际为Angular项目的ng build)时,触发JavaScript堆内存不足错误。系统配置为2GB物理内存+4GB Swap内存,但实时监控显示npm run build进程未使用Swap,仅耗尽物理内存后崩溃。
错误日志
# npm run build > fuse@9.0.0 build > ng build 93% after chunk asset optimization SourceMapDevToolPlugin main-es2015.js generate SourceMap <--- Last few GCs ---> [6311:0x50b4290] 182740 ms: Mark-sweep (reduce) 904.3 (936.0) -> 902.4 (935.6) MB, 1689.5 / 0.1 ms (average mu = 0.190, current mu = 0.245) allocation failure scavenge might not succeed [6311:0x50b4290] 182856 ms: Scavenge (reduce) 903.4 (935.6) -> 902.5 (935.6) MB, 2.5 / 0.0 ms (average mu = 0.190, current mu = 0.245) allocation failure [6311:0x50b4290] 182906 ms: Scavenge (reduce) 903.5 (935.6) -> 902.5 (935.6) MB, 4.1 / 0.0 ms (average mu = 0.190, current mu = 0.245) allocation failure <--- JS stacktrace ---> FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 1: 0xb02960 node::Abort() [ng build] 2: 0xa18149 node::FatalError(char const*, char const*) [ng build] 3: 0xcdd22e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [ng build] 4: 0xcdd5a7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [ng build] 5: 0xe94c15 [ng build] 6: 0xe956f6 [ng build] 7: 0xea3c1e [ng build] 8: 0xea4660 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [ng build] 9: 0xea75de v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [ng build] 10: 0xe68b1a v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [ng build] 11: 0x11e1886 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [ng build] 12: 0x15d54f9 [ng build] /tmp/build-e8d9a101.sh: line 1: 6311 Aborted ng build
系统内存状态
执行npm run build前
[root@vultr ~]# free -m total used free shared buff/cache available Mem: 1837 328 1133 101 375 1233 Swap: 4095 31 4064
崩溃时
[root@vultr ~]# sudo swapon --show NAME TYPE SIZE USED PRIO /swapfile file 4G 29.9M -2 [root@vultr ~]# free -m total used free shared buff/cache available Mem: 1837 1518 73 101 245 72 Swap: 4095 29 4066 [root@vultr ~]# free -m total used free shared buff/cache available Mem: 1837 1516 75 101 245 74 Swap: 4095 29 4066 [root@vultr ~]# free -m total used free shared buff/cache available Mem: 1837 1520 70 101 245 70 Swap: 4095 29 4066 [root@vultr ~]# free -m total used free shared buff/cache available Mem: 1837 450 1120 101 267 1130 Swap: 4095 31 4064
解决方案
1. 调整Node.js堆内存限制
Node.js默认的V8堆内存限制(64位系统约1.4GB)低于物理内存,导致还没触发系统Swap就先耗尽堆内存。需要手动扩大堆内存上限:
- 临时生效:执行build时直接指定参数
这里设置为3072MB(3GB),足够覆盖物理内存并允许系统使用Swap。NODE_OPTIONS="--max-old-space-size=3072" npm run build - 永久生效:将参数添加到shell配置文件
echo 'export NODE_OPTIONS="--max-old-space-size=3072"' >> ~/.bashrc source ~/.bashrc
2. 调整系统Swap调度策略
CentOS默认vm.swappiness值为10,系统会尽量避免使用Swap。提高该值让系统更积极地使用Swap:
- 临时调整:
sysctl vm.swappiness=60 - 永久调整:编辑
/etc/sysctl.conf添加配置echo 'vm.swappiness=60' >> /etc/sysctl.conf sysctl -p
3. 验证Swap使用情况
执行npm run build时,通过以下命令监控Swap是否被使用:
- 实时查看内存状态:
watch free -m - 查看进程内存占用:
top -p $(pgrep ng),观察VIRT(虚拟内存,包含Swap)和RES(物理内存)列
内容的提问来源于stack exchange,提问作者Ashar
相关产品推荐
相关产品推荐

