s-KaiNet / spfx-fast-serve

Improve your SharePoint Framework development flow by speeding up the "serve" command :rocket:
MIT License
135 stars 11 forks source link

Fast Serve recompile fails when file is referenced in multiple files in a large project #116

Closed attCurtisHennessy closed 11 months ago

attCurtisHennessy commented 11 months ago

We have a large project, around 50 web parts, with a ~500 line .ts file referenced in each of the webpart ts files. I get heap limit allocation failed:

Reached heap limit Allocation failed - JavaScript heap out of memory, but the project is launched with 8GB of RAM, and as you can see fast serve is failing at ~4GB of RAM. It doesnt seem to respect the max-old-space-size flag. Whereas when we run this project with just the standard gulp serve --max-old-space-size=8192 we get no RAM failures. Also the initial compile (which I assume is just the standard gulp serve) works fine but when we edit this file, the recompile fails (just with fast serve, with gulp serve it works but takes 2-3 mins per recompile)

"serve-test": "gulp bundle --optimize-for-size --custom-serve --max-old-space-size=8192 --max_old_space_size=8192 && fast-serve --max-old-space-size=8192 --optimize-for-size"

`<--- Last few GCs --->

[21860:000001A068B7AFD0] 90816 ms: Mark-sweep (reduce) 3993.6 (4078.0) -> 3987.6 (4048.0) MB, 100.0 / 0.3 ms (+ 201.5 ms in 24 steps since start of marking, biggest step 18.2 ms, walltime since start of marking 414 ms) (average mu = 0.937, current mu [21860:000001A068B7AFD0] 91702 ms: Mark-sweep (reduce) 4088.7 (4148.4) -> 4086.8 (4148.4) MB, 726.9 / 0.2 ms (average mu = 0.782, current mu = 0.180) allocation failure scavenge might not succeed

<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory 1: 00007FF6423E151F v8::internal::CodeObjectRegistry::~CodeObjectRegistry+121999 2: 00007FF64236B386 DSA_meth_get_flags+64118 3: 00007FF64236C402 DSA_meth_get_flags+68338 4: 00007FF642CA2C94 v8::Isolate::ReportExternalAllocationLimitReached+116 5: 00007FF642C8D25D v8::SharedArrayBuffer::Externalize+781 6: 00007FF642B3081C v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1468 7: 00007FF642B2D934 v8::internal::Heap::CollectGarbage+4244 8: 00007FF642B2B2B0 v8::internal::Heap::AllocateExternalBackingStore+2000 9: 00007FF642B48FC0 v8::internal::FreeListManyCached::Reset+1408 10: 00007FF642B49675 v8::internal::Factory::AllocateRaw+37 11: 00007FF642B5EF3F v8::internal::FactoryBase::NewRawTwoByteString+79 12: 00007FF64294254D v8::internal::String::SlowFlatten+477 13: 00007FF6426AD01B v8::internal::WasmTableObject::Fill+603 14: 00007FF642CACA36 v8::String::Utf8Length+22 15: 00007FF64238C11D v8::internal::Malloced::operator delete+17149 16: 00007FF642C5D656 v8::internal::Builtins::code_handle+172806 17: 00007FF642C5D249 v8::internal::Builtins::code_handle+171769 18: 00007FF642C5D50C v8::internal::Builtins::code_handle+172476 19: 00007FF642C5D370 v8::internal::Builtins::code_handle+172064 20: 00007FF642D30A51 v8::internal::SetupIsolateDelegate::SetupHeap+494641 21: 000001A06C06366B`

Any guidance would be greatly appreciated

INFODUMP:

v8.getHeapStatistics converted to MB (index) │ Values │ ├─────────────────────────────┼──────────────┤ │ total_heap_size │ '91.47 MB' │ │ total_heap_size_executable │ '2.30 MB' │ │ total_physical_size │ '91.47 MB' │ │ total_available_size │ '8106.85 MB' │ │ used_heap_size │ '86.42 MB' │ │ heap_size_limit │ '8195.00 MB' │ │ malloced_memory │ '1.01 MB' │ │ peak_malloced_memory │ '17.11 MB' │ │ does_zap_garbage │ '0.00 MB' │ │ number_of_native_contexts │ '0.00 MB' │ │ number_of_detached_contexts │ '0.00 MB' │ │ total_global_handles_size │ '0.21 MB' │ │ used_global_handles_size │ '0.21 MB' │ │ external_memory │ '2.47 MB' │

node version: 16.19.0 spfx version same behaviour on 1.15.2 and 1.18.0 (1.18.0 just failed to compile faster) no external libraries used in the file, just raw ts

s-KaiNet commented 11 months ago

Please try the below approach:

install cross-env npm i cross-env -DE, add new serve npm scripts:

"serve-mem": "cross-env NODE_OPTIONS=--max-old-space-size=8192 npm run serve-test",
"serve-test": "gulp bundle --custom-serve && fast-serve"

run npm run serve-mem and see if the issue is still available.

s-KaiNet commented 11 months ago

You could also try with SPFx 1.18 and nodejs 18, if nothing helps try to increase max-old-space-size to 16GB

attCurtisHennessy commented 11 months ago

Please try the below approach:

install cross-env npm i cross-env -DE, add new serve npm scripts:

"serve-mem": "cross-env NODE_OPTIONS=--max-old-space-size=8192 npm run serve-test",
"serve-test": "gulp bundle --custom-serve && fast-serve"

run npm run serve-mem and see if the issue is still available.

This worked like a charm, thank you so much for your help.

This was being a major pain, hope this helps others who end up with large projects. You're a lifesaver! @s-KaiNet

s-KaiNet commented 11 months ago

You're welcome! I checked some things with node 18 and seems fast-serve might overwrite max-old-space-size settings. Thus please don't use nodejs 18 yet, later today I will make a fix, so that max-old-space-size is also respected by fast-serve when running on node 18. I will keep the issue open in order not to forget making this fix.

s-KaiNet commented 11 months ago

Issue with node 18 was also fixed.