【问题标题】:JavaScript heap out of memory error while creating build in angular 2JavaScript 在 Angular 2 中创建构建时出现内存溢出错误
【发布时间】:2017-09-08 05:23:56
【问题描述】:

我想在 Angular 2 中创建构建,我写了 ng build --prod --aot 命令并出现以下错误。

  180823 ms: Mark-sweep 1338.1 (1437.6) -> 1338.1 (1437.6) MB, 1325.4 / 0.0 ms [allocation failure] [GC in old space requested].
  182147 ms: Mark-sweep 1338.1 (1437.6) -> 1338.1 (1437.6) MB, 1323.7 / 0.0 ms [allocation failure] [GC in old space requested].
  183495 ms: Mark-sweep 1338.1 (1437.6) -> 1344.6 (1418.6) MB, 1347.4 / 0.0 ms [last resort gc].
  184844 ms: Mark-sweep 1344.6 (1418.6) -> 1351.3 (1418.6) MB, 1348.6 / 0.0 ms [last resort gc].


<--- JS stacktrace --->

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

Security context: 0x5ec47cfb51 <JS Object>
    1: token(aka token) [0x5ec4704381 <undefined>:1724] [pc=0x3e2345467b26] (this=0x5ec4704381 <undefined>,type=0x5ec4704fb1 <String[4]: name>,value=0x3363e2ffb931 <String[62]: __WEBPACK_IMPORTED_MODULE_35__angular_material_button_button__>,is_comment=0x5ec4704381 <undefined>)
    2: arguments adaptor frame: 2->3
    3: read_word(aka read_word) [0x5ec4704381 <undefined>:~1957] [pc=0x3e23435a05fb]...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [@angular/cli]
 2: 0x1096a4c [@angular/cli]
 3: v8::Utils::ReportApiFailure(char const*, char const*) [@angular/cli]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [@angular/cli]
 5: v8::internal::Factory::NewTransitionArray(int) [@angular/cli]
 6: v8::internal::TransitionArray::Insert(v8::internal::Handle<v8::internal::Map>, v8::internal::Handle<v8::internal::Name>, v8::internal::Handle<v8::internal::Map>, v8::internal::SimpleTransitionFlag) [@angular/cli]
 7: v8::internal::Map::ShareDescriptor(v8::internal::Handle<v8::internal::Map>, v8::internal::Handle<v8::internal::DescriptorArray>, v8::internal::Descriptor*) [@angular/cli]
 8: v8::internal::Map::CopyAddDescriptor(v8::internal::Handle<v8::internal::Map>, v8::internal::Descriptor*, v8::internal::TransitionFlag) [@angular/cli]
 9: v8::internal::Map::CopyWithField(v8::internal::Handle<v8::internal::Map>, v8::internal::Handle<v8::internal::Name>, v8::internal::Handle<v8::internal::FieldType>, v8::internal::PropertyAttributes, v8::internal::Representation, v8::internal::TransitionFlag) [@angular/cli]
10: v8::internal::Map::TransitionToDataProperty(v8::internal::Handle<v8::internal::Map>, v8::internal::Handle<v8::internal::Name>, v8::internal::Handle<v8::internal::Object>, v8::internal::PropertyAttributes, v8::internal::Object::StoreFromKeyed) [@angular/cli]
11: v8::internal::LookupIterator::PrepareTransitionToDataProperty(v8::internal::Handle<v8::internal::JSObject>, v8::internal::Handle<v8::internal::Object>, v8::internal::PropertyAttributes, v8::internal::Object::StoreFromKeyed) [@angular/cli]
12: v8::internal::StoreIC::LookupForWrite(v8::internal::LookupIterator*, v8::internal::Handle<v8::internal::Object>, v8::internal::Object::StoreFromKeyed) [@angular/cli]
13: v8::internal::StoreIC::UpdateCaches(v8::internal::LookupIterator*, v8::internal::Handle<v8::internal::Object>, v8::internal::Object::StoreFromKeyed) [@angular/cli]
14: v8::internal::StoreIC::Store(v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Name>, v8::internal::Handle<v8::internal::Object>, v8::internal::Object::StoreFromKeyed) [@angular/cli]
15: v8::internal::Runtime_StoreIC_Miss(int, v8::internal::Object**, v8::internal::Isolate*) [@angular/cli]
16: 0x3e2341a092a7
Aborted (core dumped)

我也尝试在我的项目中使用以下命令:

node --max-old-space-size=8192 node_modules/@angular/compiler-cli/src/main.js -p tsconfig-aot.json

并得到以下错误:

错误:ENOENT:没有这样的文件或目录,lstat 'tsconfig-aot.json' 在错误(本机) 在 Object.fs.lstatSync (fs.js:982:18) 在 Object.main (/root/Desktop/rc1-dev/client/node_modules/@angular/tsc-wrapped/src/main.js:20:16) 在 main (/root/Desktop/rc1-dev/client/node_modules/@angular/compiler-cli/src/main.js:14:16) 在对象。 (/root/Desktop/rc1-dev/client/node_modules/@angular/compiler-cli/src/main.js:30:5) 在 Module._compile (module.js:570:32) 在 Object.Module._extensions..js (module.js:579:10) 在 Module.load (module.js:487:32) 在 tryModuleLoad (module.js:446:12) 在 Function.Module._load (module.js:438:3) 编译失败

【问题讨论】:

  • 什么版本的 CLI?构建机器中有多少 RAM?
  • RC 0 和 RAM 4GB
  • CLI 已经脱离 RC,升级 CLI 有影响吗?另外,您是否有一台具有更多 RAM 的机器可以尝试构建?在这个线程 github.com/angular/angular-cli/issues/1652 我看到有人提到超过 4 GB 的 RAM 内存不足。
  • 我可以运行 ng build ,但尝试使用 --prod 或 --aot 构建时出错

标签: node.js angular angular-cli


【解决方案1】:

对于 package.json 中基于 webpack 的应用程序 > 脚本相应地增加值 max_old_space_size=5048。

"webpack-dev-server": "node --max_old_space_size=5048 node_modules/webpack-dev-server/bin/webpack-dev-server.js",
"webpack": "node --max_old_space_size=5048 node_modules/webpack/bin/webpack.js"

【讨论】:

    【解决方案2】:

    尝试通过以下脚本在 json 包中运行构建脚本:

    "scripts": {
       "build-prod": "node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --prod --aot --no-sourcemap"
    }
    

    然后运行“npm run build-prod”。

    【讨论】:

      【解决方案3】:

      使用ng build --prod --aot --no-sourcemap(很确定 --aot 和 --no-sourcemap 现在是默认的)为生产而构建。

      Gzipping 已从 CLI 本身中删除。您的网络服务器可以 为您处理 gzipping,或者您可以通过 gulp 任务进行管道传输或 类似于构建后压缩你的 dist 文件夹。

      srchttps://github.com/angular/angular-cli/issues/5482

      【讨论】:

      【解决方案4】:

      不要使用 angular-cli 在生产环境中托管您的应用!

      angular-cli developers do not recommend it as you can see here!

      对于开发版本,您只需要 1 GB 内存,但生产版本非常占用资源,可能需要超过 2 GB 的 RAM。

      对于生产,您应该在具有超过 2GB 内存的 PC 中构建您的应用,并将 /dist 文件夹传输到您的生产服务器。

      对于您的生产服务器,您可以使用任何服务器。

      如果您想坚持使用 nodejs,我建议您使用 saco 在生产环境中托管您的单页 Web 应用程序。

      【讨论】:

      • 这是有道理的。到底谁在 prod 服务器上构建源代码?
      猜你喜欢
      • 2017-11-25
      • 2019-09-30
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 2016-08-14
      相关资源
      最近更新 更多