Asp Net Core 2.0: resolve error CALL_AND_RETRY_LAST Allocation failed – JavaScript heap out of memory

This error is caused by a memory leak problem of node.js.

On a Visual Studio 2017 Asp Net Core 2.0 project started from the Angular CLI Template, that use WebPack to manage the build process, after some publish builds, due to file size increase, we can get this error:

node node_modules/webpack/bin/webpack.js --env.prod
EXEC(0,0): Error : CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

To resolve this error we need to expand the memory size used by node.js.

This is possible setting the max_old_space_size parameter on the node command line with the [size] expressed in bytes:

--max_old_space_size=[size]

But how can we set this value into the publish process.

This is possible changing the .csproj file in this way:

  • Open the .csproj of the project that we are publishing.
  • Find the PublishRunWebpack target.
  • Set the value on each Exec Command which refers to node.
  • Save the changes.

For example, if we want to set the value to 8000 bytes:

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
 <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
 <Exec Command="npm install" />
 <Exec Command="node --max_old_space_size=8000 node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
 <Exec Command="node --max_old_space_size=8000 node_modules/webpack/bin/webpack.js --env.prod" />

<!-- Include the newly-built files in the publish output -->
 <ItemGroup>
 <DistFiles Include="wwwroot\dist\**; ClientApp\dist\**" />
 <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
 <RelativePath>%(DistFiles.Identity)</RelativePath>
 <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
 </ResolvedFileToPublish>
 </ItemGroup>
 </Target>

After saving the changes, we can start the publish built process and see that the error doesn’t appear.

I hope this post helps you.

Leave a comment