Javascript heap memory

73 Views Asked by At

Need to find out the files which are all causing heap memory error in my angular project how to find it by command or any other thing ..

I tried using ng build and found out some warning then after fixing those build is getting successfull, but when i gave ng s it returns heap memory error

1

There are 1 best solutions below

0
Християн Христов On

So the thing that is happening is that while building the app the v8 engine is not able to clean memory in time and it reaches its limits for memory consumption and throws errors.

The v8 is used for building the Angular/typescript code to js, where it has a limit of 2 GB for machines with RAM less than 16GB and processors less than x64 bit, for newer machines that with at least of 16GB RAM it allocates 4GB for the heap, you can check the following commit here for more info Finch:increase max_old_space_size to 4 GB based on availability.

In order for you to solve the issues you can go in several directions:

  1. Use the max_old_space_size that will increase manually the allocated heap, you can check the following answer that has examples JavaScript heap out of memory in Angular 7 or extend your npm scripts with the following
{
 buildProd: "node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng build --prod"
}

This will execute the Angular binaries in node environment, that has a max of 8GB heap.

Keep in mind that the --max-old-space should be less than the memory available on the machine.

  1. Migrate to a newer version, as Angular versions go up, the Angular team is optimizing the performance non stop, so there is the chance of updating to latest version to fix the issues

  2. If 2 is not an option and 1 is not helping because of some hardware restrictions, split your project into smaller buildable chunks, this is a long topic so probably you can google for concrete techniques