webpack ng serve runs out of memory
Please provide us with the following information:
- OS? Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?) Windows 10
- Versions. Please run
ng --version. If there's nothing outputted, please run in a Terminal:node --versionand paste the result here: angular-cli: 1.0.0-beta.11-webpack.2 node: 6.3.1 os: win32 x64 - Repro steps. Was this an app that wasn't created using the CLI? What change did you do on your code? etc. Keep ng serve running and serving a cli built app while coding for some time (a few hours?)
- The log given by the failure. Normally this include a stack trace and some more information.
...
94% asset optimization
<--- Last few GCs --->
12936118 ms: Mark-sweep 1360.3 (1435.0) -> 1356.1 (1435.0) MB, 959.3 / 0 ms [all
ocation failure] [GC in old space requested].
12936973 ms: Mark-sweep 1356.1 (1435.0) -> 1356.0 (1435.0) MB, 854.9 / 0 ms [all
ocation failure] [GC in old space requested].
12938085 ms: Mark-sweep 1356.0 (1435.0) -> 1356.0 (1435.0) MB, 1112.2 / 0 ms [la
st resort gc].
12939012 ms: Mark-sweep 1356.0 (1435.0) -> 1355.8 (1435.0) MB, 926.7 / 0 ms [las
t resort gc].
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 000001F1975C9E51 <JS Object>
1: replace [native string.js:~129] [pc=0000018EE86B74E5] (this=000000EF14282
331 <String[61]: D:/it/node_modules/@angular/common/src/pipes/common_pipes.js>,
N=000000EF140824C9 <JS RegExp>,O=00000135F03D7F21 <String[10]: !(webpack)>)
2: shorten [D:\it\node_modules\webpack\lib\RequestShortener.js:~41] [pc=000
0018EE4290DA2] (this=0000020E88D58019 <a RequestShortener with map 00000313C3C4.
..
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memo
ry
- Mention any other details that might be useful.
Only happened once so far but thought it worth mentioning
Thanks! We'll be in touch soon.
Yes of course.
Do you have any tools that I can use to get the numbers you need ?
I read the all issue thread again and the post of @hansl helped me a lot.
This command : node --max_old_space_size=2048 ./node_modules/.bin/ng build was the one I was looking for.
I also use this syntax with ng serve and it does not crash anymore.
I think CLOC is the easy for this purpose:
https://github.com/AlDanial/cloc
"npm install -g cloc"
Ok this is the result :

Is that just your "src" directory? If you run it at the top of the project, it might (I don't know what filtering it does built-in) count all the stuff in node_modules, which are potentially much larger than your application.
In my case:
components: 188
directives: 3
services: 8
modules:109
pipes: 3
templates: 178
It doens't include node_modules.
Below are the other modules my project uses:
"@angular/material": "2.0.0-alpha.10",
"angular2-color-picker": "1.2.1",
"angular-2-dropdown-multiselect": "0.3.0",,
"ng2-charts": "1.4.1",
"ng2-ckeditor": "1.1.4",
"ng2-img-cropper": "0.7.1"
I ran cloc src
It does not include the node_modules.
@naveedahmed1 That does not appear to be a particularly large application, I'm surprised at the size that it is bumping into RAM limitations in the build. It had been my impression the boundary was quite a lot further out. (Unless maybe some of those components/services/etc. have a remarkably large amount of code in them.)
@iwoogy Yours appears to be a quite large Angular project, that is more the size range I would expect to react the limits of the tools. I think at this point in the development of CLI, you might possibly need to pick up a more customized set of build tooling to work at that scale.
The key mystery is: why was one of these two projects was able to get so much larger before running into limitations? There might be a clue in there that could help the team fix the problem.
Do you have any suggestions of the set of tools I should use for large projects ?
Here are my numbers

This is only my angular code in the src dir
About the additional numbers: if this is another example of a not-all-that-big application running up against the limits, it could be useful to the CLI team when their attention is on this item. At the moment is marked as "priority 3 nice to have", but maybe one of them will have an update on it.
About the question of other tools: typically the advice I've been giving (I work at a company where we help big companies launch their Angular efforts with training etc.) is that if CLI does not meet your needs yet, go look at the several available "seed" projects, experiment with each, and see which one seems like the closest fit. I don't have a list handy as I write this, but there are quite a few out there. (I also always give you advice: CLI is getting better all the time, even if you need to stop something else for a while, come back later!)
CLI is working well so far, I would build on a separate machine with more RAM for now.
Ok, I was able to compile with ng build --aot --prod
@mithun-daa and @iwoogy can you please try this on Windows machine.
In node_modules.bin folder modify the below files and replace contents with the following:
Modify ng.cmd
@IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" --max_old_space_size=5048 "%~dp0\..\angular-cli\bin\ng" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.JS;=;%
node --max_old_space_size=5048 "%~dp0\..\angular-cli\bin\ng" %*
)
Modify ngc.cmd
@IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" --max_old_space_size=5048 "%~dp0\..\@angular\compiler-cli\src\main.js" %*
) ELSE (
@SETLOCAL
@SET PATHEXT=%PATHEXT:;.JS;=;%
node --max_old_space_size=5048 "%~dp0\..\@angular\compiler-cli\src\main.js" %*
)
PS: In some cases you should be able to compile even without modifying ngc.cmd.
Restart machine and then in command prompt move to yourproject\node_modules.bin> directory and run below command:
ng build --aot --prod
Sorry guys, I don't have a Windows Machine.
One observation though I was able to compile with --prod --aot, the generated output files are quite large for example one the files 0.f3869100b45355cbf408.chunk.js is 8.7Mb and compilation took about 20 minutes.
@kylecordes I have some new informations that may help.
I was using angular cli version beta.15. ng serve with 4Gb of RAM was enough to run it.
I've just updated to version beta.20-4 and now I have to allocate 12Gb of RAM to avoid the out of memory problem.
This is very good additional information, the CLI developers can see from this that the problem with this area is not only an old problem still needing attention eventually, it has worsened with recent betas.
Some more context that might help.
I needed to upgrade angular-cli because I had to use lazy loading.
I'm wondering if the lazy loading feature is not the cause of the extra memory consumption.
@iwoogy How did you allocated the RAM? Using max-old-space-size? I have a beefy Windows machine with 20GB of RAM and I still run out of memory.
@mithun-daa have you tried https://github.com/angular/angular-cli/issues/1652#issuecomment-261269197 ?
My machine is 16GB of RAM, I think it swaps.
I have upgraded to version beta.18 now.
I have to use : node --max_old_space_size=12288 ./node_modules/.bin/ng serve