Skip to content

Angular 2 AoT Heep Errors #12184

Closed
Closed

Description

@naveedahmed1

AoT compilation resulting in " JavaScript heap out of memory" error

I am trying to compile my project by following the AoT guide available on angular.io

Below is my typesccript configuration file for aot

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "es2015",
    "moduleResolution": "node",
    "noImplicitAny": false,
    "suppressImplicitAnyIndexErrors": true,
    "noEmitOnError": true,
    "removeComments": true,
    "sourceMap": false,
    "target": "es5",
    "outDir": "../content/app/"
  },
  "angularCompilerOptions": {
    "genDir": "aot",
    "skipMetadataEmit": true
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

I am compiling using below command:

D:\MyProject\node_modules\.bin>ngc -p ../../app/tsconfig-aot.json

It takes times in compilation and ends up with below errors:

<--- Last few GCs --->

  215225 ms: Mark-sweep 1327.9 (1434.3) -> 1316.1 (1434.3) MB, 1233.5 / 0.0 ms [allocation failure] [GC in old space requested].
  216639 ms: Mark-sweep 1316.1 (1434.3) -> 1316.1 (1434.3) MB, 1413.5 / 0.0 ms [allocation failure] [GC in old space requested].
  217985 ms: Mark-sweep 1316.1 (1434.3) -> 1320.4 (1403.3) MB, 1346.7 / 0.0 ms [last resort gc].
  219337 ms: Mark-sweep 1320.4 (1403.3) -> 1324.6 (1403.3) MB, 1351.5 / 0.0 ms [last resort gc].


<--- JS stacktrace --->

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

Security context: 00000023CEBCFB61 <JS Object>
    1: copyProperties [D:\MyProject\node_modules\typescript\lib\typescript.js:1462] [pc=000001870A3EC95C] (this=000000AAE12C6A41 <an Object with map 00000279F6259FA1>,source=000002E70921E389 <an Object with map 00000279F62D8539>,target=000002ED3C5C7691 <an Object with map 000000D6F40CD1C9>)
    2: objectTypeRelatedTo [D:\MyProject\node_modules\typescript\lib\typescript.js:...

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

D:\MyProject\node_modules\.bin>

The project I am trying to compile has 467 files.

Activity

  1. DzmitryShylovich commented on Oct 9, 2016

    @DzmitryShylovich
    Contributor

    Hello, we reviewed this issue and determined that it doesn't fall into the bug report or feature request category. This issue tracker is not suitable for support requests, please repost your issue on StackOverflow using tag angular.

    If you are wondering why we don't resolve support issues via the issue tracker, please check out this explanation.

  2. naveedahmed1 commented on Oct 9, 2016

    @naveedahmed1
    ContributorAuthor

    Are you sure its not a bug in AoT compiler?

  3. lazarljubenovic commented on Oct 9, 2016

    @lazarljubenovic
    Contributor

    Even if it really was a bug, you still have to fill out the template which was presented to you while opening this issue (which you apparently just deleted).

  4. singhjusraj commented on Oct 28, 2016

    @singhjusraj

    Anyone found any solution to this?
    ngc runs fine for the first time when things are in JIT mode in main.ts, after first run, I change main,ts to AoT mode.
    Then ngc takes a while to compile and the process ends up out of memory.

    I couldn't find a solution anywhere
    Questions on StackOverflow are unanswered.
    I've tried compiling on both Windows and Mac and the process runs out of memory between 1400MB - 1460MB.

    It's been over a month for me and there is still no solution to this problem.
    Any help will be appreciated thanks..

  5. alexeagle commented on Oct 28, 2016

    @alexeagle
    Contributor

    How about giving node more memory by setting --max_old_space_size ?

  6. singhjusraj commented on Oct 28, 2016

    @singhjusraj

    how can I do that along with ngc?
    I've tried node --max-old-space-size=2048 ./node_modules/.bin/ngc -p tsconfig.json
    It doesn't work

  7. alexeagle commented on Oct 28, 2016

    @alexeagle
    Contributor

    Hmm, you could give it more memory than that, but perhaps there's something about the application you're building that causes ngc to leak memory or consume a great amount of memory.
    We build angular itself and haven't seen this, but we don't have any project that compiles a very large number of components in a single pass.

    Any idea how we can reproduce the issue?

  8. singhjusraj commented on Oct 28, 2016

    @singhjusraj

    @alexeagle thanks for helping me out here.
    In my previous comment I meant I'm unable to assign more memory to node. I'm probably not doing it correctly on command line.

    here is my script in package.json

    "scripts": { "ngc": "node --max-old-space-size=2048 ./node_modules/.bin/ngc -p tsconfig.json" },

    when I do npm run ngc its gives syntax error as following:
    `C:\Users\sukhdeep\Documents\Angular\gd_dev_server\node_modules.bin\ngc:2
    basedir=$(dirname "$(echo "$0" | sed -e 's,,/,g')")
    ^^^^^^^

    SyntaxError: missing ) after argument list
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:974:3
    `

    I'm probably not doing it right.

    About the project size, Yes I'll consider my project large as there is lot of HTML in the DOM and there is lot of two way data binding. My aot folder after first ngc compile comes at 15.8MB in size.

    @naveedahmed1 you have any input on your project size?

    I have another project which is written in exactly the same way, (I've followed proper typescript syntaxes and Angular best practices in both) but has far fewer modules and the aot folder size in that project is just 1.80MB. I don't have any heap issues in that project.

    To reproduce, I guess projects that have lot of HTML and data binding are all having this issue.
    My repo is private otherwise I would've just given it

  9. naveedahmed1 commented on Oct 28, 2016

    @naveedahmed1
    ContributorAuthor

    @Sukh9212 after seeing activity on this issue, I just recompiled my project by following the official AoT guide:

    https://angular.io/docs/ts/latest/cookbook/aot-compiler.html

    I have exact same tsconfig-aot.json file as in above guide except "noImplicitAny": false,.

    My App folder has 487 files including components, modules, directive, guards, services and templates.

    After compilation aot folder has 344 files and its size is 17.2MB.

    After updating main.ts to:

    import { platformBrowser } from '@angular/platform-browser';
    import { AppModuleNgFactory } from './aot/app.module.ngfactory';
    platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);
    

    I get the same error which you have mentioned above. One more thing my app is using Lazyloading.

    BTW, this is the error after modifying the main.tx

    <--- Last few GCs --->
    
      263129 ms: Mark-sweep 1312.5 (1434.6) -> 1312.4 (1434.6) MB, 1813.2 / 0.0 ms [allocation failure] [GC in old space requested].
      265031 ms: Mark-sweep 1312.4 (1434.6) -> 1312.3 (1434.6) MB, 1902.1 / 0.0 ms [allocation failure] [GC in old space requested].
      266953 ms: Mark-sweep 1312.3 (1434.6) -> 1315.6 (1407.6) MB, 1921.0 / 0.0 ms [last resort gc].
      268920 ms: Mark-sweep 1315.6 (1407.6) -> 1318.9 (1407.6) MB, 1966.8 / 0.0 ms [last resort gc].
    
    
    <--- JS stacktrace --->
    
    ==== JS stack trace =========================================
    
    Security context: 00000206D17CFB61 <JS Object>
        1: getTypeOfVariableOrParameterOrProperty [D:\Project\node_modules\typescript\lib\typescript.js:~20066] [pc=0000035B369D90BF] (this=00000206D17E6601 <JS Global Object>,symbol=00000170AC196F69 <a SymbolObject with map 0000020C13D6ABC1>)
        2: checkVariableLikeDeclaration [D:\Project\node_modules\typescript\lib\typescript.js:~31339] [pc=0000035B35F3EBF3] (this=00000...
    
    FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
    

    @alexeagle I can email you my app folder, which you would be able to compile. But since it has a dependence on backend in asp.net (which I cann't share) you wont be able to run the project.

    @Sukh9212 if you arent using Lazyloading, can you try to compile your project using Angular CLI and see if it works? In angular cli you can compile your project using below command.
    ng build --prod --aot

    For me its not working since my app is using lazyloading.

  10. singhjusraj commented on Nov 1, 2016

    @singhjusraj

    I'm not using lazy loading since I figured that if I do, ngc just ignores all the lazy loaded modules and doesn't generate AoT code for them. I'm not sure if it's supposed to do that or not.
    So not doing that and importing all the modules in my app.module file.

    My project isn't Angular CLI based so I won't be able to do that.
    It's actually not even a problem of which tool we are using. At the end of it, it's the same ngc compiler and doesn't matter how we invoke it, the process is gonna run out of memory

  11. naveedahmed1 commented on Nov 2, 2016

    @naveedahmed1
    ContributorAuthor

    Please have a look at below repository:

    https://github.com/steveblue/angular2-rollup

    and check the Production notes:

    https://github.com/steveblue/angular2-rollup#production

    It also suggests that there are some issues with ngc when using for large projects and it recommends using Closure Compiler after compiling with ngc.

    @vicb can you please help on this?

  12. singhjusraj commented on Nov 2, 2016

    @singhjusraj

    I think closure compiler isn't ready yet for Angular 2 see issue #8550 and this link
    Even if closure compiler works, still I would like ngc to do the entire job that it is supposed to do as it is demonstrated in the official tutorials.
    Angular 2 isn't in beta or RC anymore, but we still can't generate a production build out of it.
    Waiting for it for over a month and half now.

  13. singhjusraj commented on Nov 3, 2016

    @singhjusraj

    OK folks here is how I FINALLY compiled my project with ngc

    This doesn't work on windows for some reason, but it worked on a mac machine

    //first compile with main.ts in JIT mode
    ./node_modules/.bin/ngc -p tsconfig-aot.json

    //then change main.ts to AoT mode and run
    node --max-old-space-size=8192 ./node_modules/.bin/ngc -p tsconfig-aot.json

    I literally had to go as far as 8192 (8GB) to not run out of memory (it goes as far as 5-6GB for my project)

    //then build the rollup bundle
    node --max-old-space-size=8192 node_modules/.bin/rollup -c rollup.js

    if everything went well, now you should have a production bundle.

    I'm not using lazy loading as if I do, ngc just ignores all the lazy loaded modules and does not generate AoT files for them.

    My machine is

    screen shot 2016-11-03 at 10 29 06 am

  14. 16 remaining items

  15. alexeagle commented on Dec 14, 2016

    @alexeagle
    Contributor
  16. added
    area: core
    on Jan 5, 2017
  17. ritzm21 commented on Feb 10, 2017

    @ritzm21

    Hi all,
    I am also facing same heap error in my ionic 2 project as a result of adding many new components, directives in it. Here is the build script for ionic 2 project in package.json:
    "scripts": {
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve"
    },

    And I modified it based on above discussion as below:
    "scripts": {
    "ionic:build": "node --max-old-space-size=8192 ./node_modules/@ionic/app-scripts/bin/ionic-app-scripts.js build",
    "ionic:serve": "ionic-app-scripts serve"
    }
    ,

    But I am still facing javascript heap error. Can anybody help me to understand that above modified script is a right approach to increase the heap size for node?
    I am using Windows Command Line(cmd) tool to build the ionic 2 app.

  18. ValterSantosMatos commented on Mar 10, 2017

    @ValterSantosMatos

    @ritzm21 actually that worked for my Ionic2 project.

  19. tbosch commented on Apr 12, 2017

    @tbosch
    Contributor

    So we need to do 2 things:

    • allow build tools to compile big applications as multiple compilation units (i.e. multiple invocations of ngc). We already do this internally at Google, and we need to find a way to make this work nicely externally as well
    • change ngc's memory consumption to be independent of the project size
  20. added
    memory leak
    on Apr 12, 2017
  21. Neltherion commented on May 8, 2017

    @Neltherion

    any update on this? I used 8 GBs for a simple Bootstrap project and even 8 GBs couldn't do it!
    the command that worked for me is :
    node --max-old-space-size=8192 node_modules/rollup/bin/rollup -c rollup-config.js

  22. damiandennis commented on Nov 26, 2017

    @damiandennis

    I need to allocate 5GB just to compile a medium sized app.

  23. Koslun commented on Nov 27, 2017

    @Koslun

    For everyone posting their results I think a more detailed build configuration and project information is needed to get any real insight.

    Rollup, Webpack and other bundlers each have their own overhead, changing also vastly depending on version and configuration, including for instance which webpack plugins are used and how they are configured. Angular version is also important to understand what angular compiler is used. Where both the bundler and the angular compiler can consume a great amount of RAM. See an example breakdown here for meeting 2017-11-21: https://docs.google.com/document/d/1wMHjLdXrcGw-8N50mJqqiyFA_upkc6_fTd2vEhOM0-k/pub

    Following a larger regression for angular-cli 1.5, in terms of memory usage, reducing the memory usage is being actively worked on, see: angular/angular-cli#5618. Though this largely does not cover optimizing the angular compiler.

    This has partly added to Webpacks ModuleConcatenationPlugin being heavily optimized in terms of RAM usage. See webpack/webpack#5992 and webpack/webpack-sources#30.

  24. IgorMinar commented on Dec 22, 2017

    @IgorMinar
    Contributor

    It seems that this bug ended up being the catch all place for all kinds of memory related issues. We fixed a whole bunch of them in v4 and then again just after v5.0 was released. Please help us sort these issues out by open new issues with reproductions if you still have memory related problems. Thank you.

  25. angular-automatic-lock-bot commented on Sep 13, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  26. locked and limited conversation to collaborators on Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corefreq2: mediummemory leaktype: bug/fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions