Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory #255

Closed
Christian-Athom opened this issue Sep 17, 2018 · 10 comments

Comments

@Christian-Athom
Copy link

Do you want to request a feature or report a bug?
BUG

What is the current behavior?
When bundling for debug, the Metro bundler will crash with: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory. The project contains a lot of images and JS files

If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.
The project can be considered as a large project with with a lot of images and JS files. When using react native 0.56, the bundling would take +/- 20-30 seconds with the same project.

What is the expected behavior?
TheMetro bundler should not choke on projects with lots of images

Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.

Metro 0.45.3
Node 8.11.3

React Native Environment Info:
System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
Memory: 745.65 MB / 16.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 8.11.3 - ~/.nvm/versions/node/v8.11.3/bin/node
npm: 5.6.0 - ~/.nvm/versions/node/v8.11.3/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 11.4, macOS 10.13, tvOS 11.4, watchOS 4.3
Android SDK:
Build Tools: 27.0.3, 28.0.0
API Levels: 23, 25, 26, 27, 28
IDEs:
Android Studio: 3.1 AI-173.4819257
Xcode: 9.4.1/9F2000 - /usr/bin/xcodebuild
npmPackages:
react: ^16.5 => 16.5.1
react-native: ^0.57.0 => 0.57.0
npmGlobalPackages:
react-native-cli: 2.0.1
react-native-git-upgrade: 0.2.7
react-native-rename: 2.2.2

@rafeca
Copy link
Contributor

rafeca commented Sep 18, 2018

Thanks for reporting! I'm pretty sure that this issue is related to #253

Can you try adding the --max-workers to the CLI command and check if it fixes it?

@paulmand3l
Copy link

paulmand3l commented Sep 19, 2018

I've been having the same issue. Adding --max-workers=4 fixed debug builds for me. Average file transformation duration in the 10s of ms range.

Unfortunately, my release builds are also crashing.

Here's the crash:

react-native bundle --verbose --platform android --dev false --entry-file index.js --reset-cache --bundle-output "tmp.js" --max-workers=4

transform[stdout]: 
transform[stdout]: <--- Last few GCs --->
transform[stdout]: 
transform[stdout]: [98181:0x102801600]   200790 ms: Mark-sweep 1419.7 (1607.1) -> 1419.7 (1591.1) MB, 4401.5 / 0.0 ms  (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 4402 ms) last resort GC in old space requested
transform[stdout]: [98181:0x102801600]   205262 ms: Mark-sweep 1419.7 (1591.1) -> 1419.7 (1591.1) MB, 4471.8 / 0.0 ms  last resort GC in old space requested
transform[stdout]: 
transform[stdout]: 
transform[stdout]: <--- JS stacktrace --->
transform[stdout]: 
transform[stdout]: ==== JS stack trace =========================================
transform[stdout]: 
transform[stdout]: Security context: 0xc079d325ee1 <JSObject>
transform[stdout]:     2: add_parameter [0xc0758d02311 <undefined>:2932] [bytecode=0xc076a6d74f1 offset=69](this=0xc0784850591 <Object map = 0xc07240216d9>,token=0xc07dcd06d01 <AST_Token map = 0xc07240220d1>)
transform[stdout]:     4: binding_element(aka binding_element) [0xc0758d02311 <undefined>:3204] [bytecode=0xc076a6d59f9 offset=2026](this=0xc0758d02311 <undefined>,used_parameters=0xc0784850591 <Object map = 0xc07240216d9>,symbo...
transform[stdout]: 
transform[stderr]: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
transform[stderr]:  1: node::Abort() [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]:  2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]:  3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]:  4: 
transform[stderr]: v8::internal::Factory::NewTransitionArray(int) [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]:  5: 
transform[stderr]: 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) [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]:  6: 
transform[stderr]: v8::internal::Map::CopyReplaceDescriptors(v8::internal::Handle<v8::internal::Map>, v8::internal::Handle<v8::internal::DescriptorArray>, v8::internal::Handle<v8::internal::LayoutDescriptor>, v8::internal::TransitionFlag, v8::internal::MaybeHandle<v8::internal::Name>, char const*, v8::internal::SimpleTransitionFlag) [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]:  7: 
transform[stderr]: v8::internal::Map::CopyAddDescriptor(v8::internal::Handle<v8::internal::Map>, v8::internal::Descriptor*, v8::internal::TransitionFlag) [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]:  8: 
transform[stderr]: 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::PropertyConstness, v8::internal::Representation, v8::internal::TransitionFlag) [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]:  9: 
transform[stderr]: 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::PropertyConstness, v8::internal::Object::StoreFromKeyed) [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]: 10: 
transform[stderr]: v8::internal::LookupIterator::PrepareTransitionToDataProperty(v8::internal::Handle<v8::internal::JSObject>, v8::internal::Handle<v8::internal::Object>, v8::internal::PropertyAttributes, v8::internal::Object::StoreFromKeyed) [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]: 11: 
transform[stderr]: v8::internal::Object::AddDataProperty(v8::internal::LookupIterator*, v8::internal::Handle<v8::internal::Object>, v8::internal::PropertyAttributes, v8::internal::Object::ShouldThrow, v8::internal::Object::StoreFromKeyed) [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]: 12: 
transform[stderr]: v8::internal::Runtime_SetProperty(int, v8::internal::Object**, v8::internal::Isolate*) [/Users/username/.nvm/versions/node/v8.9.4/bin/node]
transform[stderr]: 13: 0x37117b10463d

React Native Environment Info:

    System:
      OS: macOS Sierra 10.12.6
      CPU: x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
      Memory: 1.88 GB / 16.00 GB
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 8.9.4 - ~/.nvm/versions/node/v8.9.4/bin/node
      Yarn: 1.7.0 - /usr/local/bin/yarn
      npm: 5.6.0 - ~/.nvm/versions/node/v8.9.4/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 11.2, macOS 10.13, tvOS 11.2, watchOS 4.2
    IDEs:
      Android Studio: 3.1 AI-173.4907809
      Xcode: 9.2/9C40b - /usr/bin/xcodebuild
    npmPackages:
      react: 16.5.0 => 16.5.0 
      react-native: 0.57.0 => 0.57.0 
    npmGlobalPackages:
      create-react-native-app: 1.0.0
      react-native-cli: 2.0.1

I tried the "--max_old_space_size=4096" trick that some sources suggest, but no luck. Same crash in the same spot.

I tried the logging trick from the linked issue and discovered that I'm always crashing on the same file:

...
{ action_name: 'Transforming file',
  action_phase: 'start',
  file_name: '/Users/username/Code/project/node_modules/pegjs/lib/parser.js',
  log_entry_label: 'Transforming file',
  start_timestamp: [ 76200, 418201461 ] }
{ action_name: 'Transforming file',
  action_phase: 'end',
  file_name: '/Users/username/Code/project/node_modules/pegjs/lib/parser.js',
  duration_ms: 3226,
  log_entry_label: 'Transforming file' }
{ action_name: 'Cache set',
  log_entry_label: 'FileStore::511777981e95dc115bec02bf8c792de1283f7678ab78b3bc54c80e2c347128b993f217e5',
  log_session: '8c85907d9d09.ant.amazon.com-1537317141615',
  metro_bundler_version: '0.45.3' }
[long pause then the stack trace from above]

Here's the offending file: https://github.com/pegjs/pegjs/blob/v0.10.0/lib/parser.js

I recently upgraded to react-native 0.57.0 using react-native-git-upgrade and I'm using metro 0.45.3 and babel 7. Here's my .babelrc config if it helps.

{
  "presets": ["module:metro-react-native-babel-preset"],
}

I tried bundling just that file and it works fine: https://gist.github.com/paulmand3l/cc54bb6680a6ffeb9a4a1c31efcfe560

@rafeca
Copy link
Contributor

rafeca commented Sep 19, 2018

Thanks for the information!

The file that makes the thing crash may be a different one (since this one has already an action_phase: 'end' log). Can you check if there's any file before in the logs with action_phase: 'start' but no end? (it may be way before in the logs...)

@paulmand3l
Copy link

I think I figured it out. I tracked the files in dev-mode and noticed that it was getting stuck at 99.2% (file 1502/1508) for a long time. The file that was getting transformed after parser.js was the problem:

{ action_name: 'Transforming file',
  action_phase: 'end',
  file_name: '/Users/username/Code/project/node_modules/aws-sdk/dist/aws-sdk-react-native.js',
  duration_ms: 36722,
  log_entry_label: 'Transforming file' }

Solution here: aws/aws-sdk-js#1877

@paulmand3l
Copy link

Whelp, looks like the new react-native/metro doesn't like the "ignore" option.

facebook/react-native#20247

Any suggestions?

@jkimbo
Copy link
Contributor

jkimbo commented Sep 23, 2018

@paulmand3l I had the same issue with metro not liking the ignore option (metro v0.45.6) and I found the issue in reactNativeTransformer.js. The following patch should fix it:

patch-package
--- a/node_modules/metro/src/reactNativeTransformer.js
+++ b/node_modules/metro/src/reactNativeTransformer.js
@@ -154,7 +154,12 @@ function transform(_ref) {let filename = _ref.filename,options = _ref.options,sr
       // ES modules require sourceType='module' but OSS may not always want that
       sourceType: 'unambiguous' },
     babelConfig, {
-      ast: true }));const ast = _transformSync.ast;
+      ast: true }));
+    if (_transformSync === null) {
+      return { ast: null };
+    }
+
+    const ast = _transformSync.ast;
 
 
     return { ast };

The code that calls the transform function already handles the case where ast: null:

// Transformers can ouptut null ASTs (if they ignore the file). In that case
// we need to parse the module source code to get their AST.
let ast =
transformResult.ast || babylon.parse(sourceCode, {sourceType: 'module'});

@rafeca
Copy link
Contributor

rafeca commented Sep 23, 2018

@jkimbo oh that's actually an issue on the transformer. Do you want to send a PR to fix it?

@jkimbo
Copy link
Contributor

jkimbo commented Sep 24, 2018

@rafeca sure: #264

facebook-github-bot pushed a commit that referenced this issue Sep 27, 2018
Summary:
**Summary**

Fixes a bug when ignoring a file in the babel config: #255 (comment)

**Test plan**

Couldn't find any obvious tests that test this part of the code. Happy to add some tests if someone can point me in the right direction.
Pull Request resolved: #264

Differential Revision: D10063236

Pulled By: rafeca

fbshipit-source-id: 04205b856c513fd90c0ae022c2cbb4deeef0c993
@rafeca
Copy link
Contributor

rafeca commented Oct 4, 2018

Closing the issue 😄

@rafeca rafeca closed this as completed Oct 4, 2018
@tetreault
Copy link

I still have this issue. i'm at the very starting point of an AR react native app with Viro media. The metro bundler crashes consistently (but also sometimes is totally fine).

 BUNDLE  [ios, dev] ./index.js ░░░░░░░░░░░░░░░░ 0.0% (0/225)
<--- Last few GCs --->

[66779:0x102801600]    42060 ms: Mark-sweep 874.4 (957.4) -> 874.4 (924.9) MB, 59.6 / 0.0 ms  (average mu = 0.873, current mu = 0.000) last resort GC in old space requested
[66779:0x102801600]    42124 ms: Mark-sweep 874.4 (924.9) -> 874.4 (924.9) MB, 64.0 / 0.0 ms  (average mu = 0.760, current mu = 0.000) last resort GC in old space requested


<--- JS stacktrace --->

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

    0: ExitFrame [pc: 0x103de94841bd]
Security context: 0x1424f221e589 <JSObject>
    1: _send [0x142469faa4c1] [internal/child_process.js:715] [bytecode=0x1424a1acca91 offset=606](this=0x1424575618b9 <ChildProcess map = 0x1424ac78ade9>,message=0x142431982201 <JSArray[4]>,handle=0x14248af822e1 <undefined>,options=0x142431982289 <Object map = 0x1424ac78bc59>,callback=0x14248af822e1 <undefined>)
    2: send [0x142469faa469] [internal/child_...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
 2: node::OnFatalError(char const*, char const*) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
 3: v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
 4: v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
 5: v8::internal::Heap::AllocateRawWithRetry(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
 6: v8::internal::Factory::NewRawTwoByteString(int, v8::internal::PretenureFlag) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
 7: v8::internal::String::SlowFlatten(v8::internal::Handle<v8::internal::ConsString>, v8::internal::PretenureFlag) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
 8: v8::String::Utf8Length() const [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
 9: node::StringBytes::Size(v8::Isolate*, v8::Local<v8::Value>, node::encoding) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
10: int node::StreamBase::WriteString<(node::encoding)1>(v8::FunctionCallbackInfo<v8::Value> const&) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
11: void node::StreamBase::JSMethod<node::LibuvStreamWrap, &(int node::StreamBase::WriteString<(node::encoding)1>(v8::FunctionCallbackInfo<v8::Value> const&))>(v8::FunctionCallbackInfo<v8::Value> const&) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
12: v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo*) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
13: v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
14: v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) [/Users/stephentetreault/.nvm/versions/node/v10.4.0/bin/node]
15: 0x103de94841bd
16: 0x103de9493429
17: 0x103de9493429
18: 0x103de948c223
Abort trap: 6

node: v10.4.0
npm: 6.4.1
react native: 0.55.1
OS: mac OS mojave
device: iphone x running iOS 12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants