Skip to content

SSR of page that imports a large file causes huge spike in memory usage #8592

@FeldrinH

Description

@FeldrinH

Describe the bug

During the initial server side render of a page that imports a large file the server memory usage spikes to approximately 100x the size of the file. On my system this causes node to run out of memory and crash when importing files larger than 30MB.

This seems to happen for imported JSON files as well as text files imported using Vite's ?raw parameter.

Curiously the memory usage spike only seems to occur when using the dev server (npm run dev). It does not occur when building the project with npm run build and then using the preview server (npm run preview).

The memory usage spike also does not seem to occur during client side rendering. If you navigate to the page that imports the large file using a client side routed link and the import happens on the client everything works fine.

Reproduction

https://github.com/FeldrinH/sveltekit-oom-repro

Logs

C:\...\sveltekit-oom-repro>npm run dev

> sveltekit-oom-repro@0.0.1 dev
> vite dev



  VITE v4.0.4  ready in 707 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help

<--- Last few GCs --->

[14108:0000027B8A881300]    97882 ms: Mark-sweep 2035.1 (2081.8) -> 2035.1 (2081.8) MB, 984.5 / 0.0 ms  (average mu = 0.218, current mu = 0.010) allocation failure; scavenge might not succeed
[14108:0000027B8A881300]    99854 ms: Mark-sweep 2050.9 (2081.8) -> 2050.9 (2113.8) MB, 1964.3 / 0.0 ms  (average mu = 0.111, current mu = 0.004) allocation failure; scavenge might not succeed


<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 00007FF6DE509E7F node_api_throw_syntax_error+175967
 2: 00007FF6DE490C06 SSL_get_quiet_shutdown+65750
 3: 00007FF6DE491FC2 SSL_get_quiet_shutdown+70802
 4: 00007FF6DEF2A214 v8::Isolate::ReportExternalAllocationLimitReached+116
 5: 00007FF6DEF15572 v8::Isolate::Exit+674
 6: 00007FF6DED973CC v8::internal::EmbedderStackStateScope::ExplicitScopeForTesting+124
 7: 00007FF6DED945EB v8::internal::Heap::CollectGarbage+3963
 8: 00007FF6DEDAA823 v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath+2099
 9: 00007FF6DEDAB0CD v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath+93
10: 00007FF6DEDBA903 v8::internal::Factory::NewFillerObject+851
11: 00007FF6DEAABEB5 v8::internal::DateCache::Weekday+1349
12: 00007FF6DEFC78B1 v8::internal::SetupIsolateDelegate::SetupHeap+558193
13: 00007FF65F2BE29A

C:\...\sveltekit-oom-repro>

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 2.68 GB / 7.87 GB
  Binaries:
    Node: 18.13.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.19.3 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 109.0.5414.75
    Edge: Spartan (44.19041.1266.0), Chromium (108.0.1462.76)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @sveltejs/adapter-auto: ^1.0.0 => 1.0.1
    @sveltejs/kit: ^1.0.0 => 1.1.3
    svelte: ^3.54.0 => 3.55.1
    vite: ^4.0.0 => 4.0.4

Severity

serious, but I can work around it

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions