npm run build 内存溢出处理办法

在内存小的服务器对webpack工程打包的时候遇到的一个问题,

<--- Last few GCs --->

[20015:0x3447a40]  4599326 ms: Scavenge 1315.8 (1426.2) -> 1314.9 (1426.2) MB, 2.3 / 0.0 ms  (+ 2.7 ms in 1929 steps since last GC) allocation failure
[20015:0x3447a40]  4599335 ms: Scavenge 1315.9 (1426.2) -> 1315.0 (1426.2) MB, 2.2 / 0.0 ms  (+ 2.7 ms in 1945 steps since last GC) allocation failure
[20015:0x3447a40]  4599343 ms: Scavenge 1316.0 (1426.2) -> 1315.1 (1426.2) MB, 2.1 / 0.0 ms  (+ 2.5 ms in 1960 steps since last GC) allocation failure


<--- JS stacktrace --->
Cannot get stack trace in GC.
FATAL ERROR: Scavenger: promoting marked
 Allocation failed - process out of memory
 1: node::Abort() [ember]
 2: 0x126389c [ember]
 3: v8::Utils::ReportOOMFailure(char const*, bool) [ember]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [ember]
 5: 0xa65f9b [ember]
 6: void v8::internal::ScavengingVisitor<(v8::internal::MarksHandling)0, (v8::internal::PromotionMode)0, (v8::internal::LoggingAndProfiling)1>::EvacuateObject<(v8::internal::ScavengingVisitor<(v8::internal::MarksHandling)0, (v8::internal::PromotionMode)0, (v8::internal::LoggingAndProfiling)1>::ObjectContents)1, (v8::internal::AllocationAlignment)0>(v8::internal::Map*, v8::internal::HeapObject**, v8::internal::HeapObject*, int) [ember]
 7: v8::internal::Scavenger::CheckAndScavengeObject(v8::internal::Heap*, unsigned char*) [ember]
 8: v8::internal::Heap::Scavenge() [ember]
 9: v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [ember]
10: v8::internal::Heap::CollectGarbage(v8::internal::GarbageCollector, char const*, char const*, v8::GCCallbackFlags) [ember]
11: v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [ember]
12: v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [ember]
13: 0x29ece76063a7
Aborted (core dumped)


    
npm ERR! Linux 4.10.0-20-generic
npm ERR! argv "/home/chrmod/.nvm/versions/node/v7.4.0/bin/node" "/home/chrmod/.nvm/versions/node/v7.4.0/bin/npm" "start"
npm ERR! node v7.4.0
npm ERR! npm  v4.0.5
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `ember server -p 3000` 
npm ERR! Exit status 134                                            
npm ERR!
复制代码

其中有一种办法是通过扩大node内存处理,其修改命令如下

node --max_old_space_size=2048 test.js
复制代码

因为是使用vue-cli-service,直接增加node参数,不是很方便。找到了如下方法

分配系统swap

  1. 查看是否开启swap
sudo swapon -s
复制代码

  1. 查看系统磁盘
df -h
复制代码

  1. 分配自己适合的大小
sudo fallocate -l 4G /swapfile
复制代码

这里选择4G大小

ls -lh /swapfile
复制代码

4. 启用swap

  • 查看文档权限并创建
sudo chmod 600 /swapfile
sudo mkswap /swapfile
复制代码

  • 修改文档权限并开启swap
sudo swapon /swapfile
sudo swapon -s
复制代码

  1. 查看内存分配与使用
free -m
复制代码

至此,已完成的那个swap内存分配

你可能感兴趣的:(npm run build 内存溢出处理办法)