Content-Length: 353824 | pFad | https://redirect.github.com/preactjs/preact/issues/4680

2D6 Fix memory leak by JoviDeCroock · Pull Request #4680 · preactjs/preact · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak #4680

Merged
merged 4 commits into from
Feb 18, 2025
Merged

Fix memory leak #4680

merged 4 commits into from
Feb 18, 2025

Conversation

JoviDeCroock
Copy link
Member

@JoviDeCroock JoviDeCroock commented Feb 17, 2025

Fixes #4679

Basically what happens is that when we encounter a fragment we unwrap it to return is children, this way when elements get wrapped with fragments we don't risk losing state in between renders.

This has a consequence when it is used in hooks environments as a hook with a closure will capture the outer scope. As can be seen in the reproduction, if an effect (or any other hook) closure takes a snapshot of the outer closure it will take the Fragment we unwrap into memory, this means that during diff we'll effectively start mutating its children. Note that it's important that the hook has no dependencies for this to be reproduced.

Every time we re-render we'll take a new snapshot of the closure which retains a copy of Fragment with pointers to all of its populated children. We unwrap the Fragment which still points at its children which point back at the parent of the Fragment. The closure points at the Fragment, the Fragment points at its children (props.children) and the children point at the parent component of Fragment.

With the implemented measure we clear props.children of the Fragment so that we effectively break this chain of pointers and allow the Fragment to be gc'd in-between renders caused by updates to the same component.

Fun fact, when we implement #4679's reproduction with class components there is no memory leak, this was one of the first pointers to me that hooks and their closures were the culprit.

Copy link

github-actions bot commented Feb 17, 2025

📊 Tachometer Benchmark Results

Summary

⏳ Benchmarks are currently running. Results below are out of date.

duration

  • create10k: unsure 🔍 -0% - +1% (-1.11ms - +11.99ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -2% - +2% (-0.41ms - +0.26ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -2% - +1% (-1.23ms - +0.76ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -0% - +2% (-0.04ms - +0.31ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -2% - +1% (-1.13ms - +0.52ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -1% - +5% (-0.03ms - +0.10ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -2% - +1% (-0.81ms - +0.31ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -3% - +4% (-0.91ms - +1.16ms)
    preact-local vs preact-main

usedJSHeapSize

  • create10k: unsure 🔍 -0% - +0% (-0.03ms - +0.00ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -2% - +2% (-0.03ms - +0.03ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -4% - +4% (-0.39ms - +0.43ms)
    preact-local vs preact-main
  • many-updates: faster ✔ 0% - 1% (0.00ms - 0.04ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -1% - +0% (-0.02ms - +0.01ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 +0% - +0% (+0.00ms - +0.00ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -3% - +1% (-0.04ms - +0.01ms)
    preact-local vs preact-main
  • update10th1k: faster ✔ 0% - 2% (0.00ms - 0.05ms)
    preact-local vs preact-main

Results

⏳ Benchmarks are currently running. Results below are out of date.
create10k

duration

VersionAvg timevs preact-localvs preact-main
preact-local971.18ms - 981.65ms-unsure 🔍
-0% - +1%
-1.11ms - +11.99ms
preact-main967.05ms - 974.91msunsure 🔍
-1% - +0%
-11.99ms - +1.11ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local19.18ms - 19.18ms-unsure 🔍
-0% - +0%
-0.03ms - +0.00ms
preact-main19.18ms - 19.21msunsure 🔍
-0% - +0%
-0.00ms - +0.03ms
-
filter-list

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.46ms - 16.93ms-unsure 🔍
-2% - +2%
-0.41ms - +0.26ms
preact-main16.54ms - 17.01msunsure 🔍
-2% - +2%
-0.26ms - +0.41ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.54ms - 1.58ms-unsure 🔍
-2% - +2%
-0.03ms - +0.03ms
preact-main1.54ms - 1.58msunsure 🔍
-2% - +2%
-0.03ms - +0.03ms
-
hydrate1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local75.90ms - 77.60ms-unsure 🔍
-2% - +1%
-1.23ms - +0.76ms
preact-main76.47ms - 77.49msunsure 🔍
-1% - +2%
-0.76ms - +1.23ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local9.54ms - 10.14ms-unsure 🔍
-4% - +4%
-0.39ms - +0.43ms
preact-main9.54ms - 10.10msunsure 🔍
-4% - +4%
-0.43ms - +0.39ms
-
many-updates

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.40ms - 16.58ms-unsure 🔍
-0% - +2%
-0.04ms - +0.31ms
preact-main16.20ms - 16.50msunsure 🔍
-2% - +0%
-0.31ms - +0.04ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.76ms - 3.78ms-faster ✔
0% - 1%
0.00ms - 0.04ms
preact-main3.77ms - 3.81msslower ❌
0% - 1%
0.00ms - 0.04ms
-
replace1k
  • Browser: chrome-headless
  • Sample size: 100
  • Built by: CI #4430
  • Commit: 36ebb0b

duration

VersionAvg timevs preact-localvs preact-main
preact-local64.64ms - 65.70ms-unsure 🔍
-2% - +1%
-1.13ms - +0.52ms
preact-main64.85ms - 66.10msunsure 🔍
-1% - +2%
-0.52ms - +1.13ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local2.97ms - 2.99ms-unsure 🔍
-1% - +0%
-0.02ms - +0.01ms
preact-main2.97ms - 3.00msunsure 🔍
-0% - +1%
-0.01ms - +0.02ms
-

run-warmup-0

VersionAvg timevs preact-localvs preact-main
preact-local29.17ms - 29.71ms-unsure 🔍
-1% - +2%
-0.41ms - +0.46ms
preact-main29.08ms - 29.76msunsure 🔍
-2% - +1%
-0.46ms - +0.41ms
-

run-warmup-1

VersionAvg timevs preact-localvs preact-main
preact-local32.73ms - 33.53ms-unsure 🔍
-3% - +1%
-0.91ms - +0.49ms
preact-main32.77ms - 33.92msunsure 🔍
-1% - +3%
-0.49ms - +0.91ms
-

run-warmup-2

VersionAvg timevs preact-localvs preact-main
preact-local25.10ms - 25.47ms-unsure 🔍
-1% - +2%
-0.27ms - +0.39ms
preact-main24.95ms - 25.49msunsure 🔍
-2% - +1%
-0.39ms - +0.27ms
-

run-warmup-3

VersionAvg timevs preact-localvs preact-main
preact-local25.24ms - 26.12ms-unsure 🔍
-3% - +2%
-0.85ms - +0.43ms
preact-main25.42ms - 26.36msunsure 🔍
-2% - +3%
-0.43ms - +0.85ms
-

run-warmup-4

VersionAvg timevs preact-localvs preact-main
preact-local25.85ms - 26.62ms-unsure 🔍
-2% - +2%
-0.58ms - +0.63ms
preact-main25.75ms - 26.68msunsure 🔍
-2% - +2%
-0.63ms - +0.58ms
-

run-final

VersionAvg timevs preact-localvs preact-main
preact-local19.86ms - 20.21ms-unsure 🔍
-1% - +1%
-0.21ms - +0.29ms
preact-main19.81ms - 20.18msunsure 🔍
-1% - +1%
-0.29ms - +0.21ms
-
text-update
  • Browser: chrome-headless
  • Sample size: 220
  • Built by: CI #4430
  • Commit: 36ebb0b

duration

VersionAvg timevs preact-localvs preact-main
preact-local2.05ms - 2.14ms-unsure 🔍
-1% - +5%
-0.03ms - +0.10ms
preact-main2.01ms - 2.11msunsure 🔍
-5% - +1%
-0.10ms - +0.03ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.12ms - 1.12ms-unsure 🔍
+0% - +0%
+0.00ms - +0.00ms
preact-main1.12ms - 1.12msunsure 🔍
-0% - -0%
-0.00ms - -0.00ms
-
todo

duration

VersionAvg timevs preact-localvs preact-main
preact-local34.27ms - 34.82ms-unsure 🔍
-2% - +1%
-0.81ms - +0.31ms
preact-main34.31ms - 35.28msunsure 🔍
-1% - +2%
-0.31ms - +0.81ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.24ms - 1.27ms-unsure 🔍
-3% - +1%
-0.04ms - +0.01ms
preact-main1.25ms - 1.29msunsure 🔍
-1% - +4%
-0.01ms - +0.04ms
-
update10th1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local30.58ms - 32.26ms-unsure 🔍
-3% - +4%
-0.91ms - +1.16ms
preact-main30.68ms - 31.91msunsure 🔍
-4% - +3%
-1.16ms - +0.91ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local2.94ms - 2.96ms-faster ✔
0% - 2%
0.00ms - 0.05ms
preact-main2.95ms - 3.00msslower ❌
0% - 2%
0.00ms - 0.05ms
-

tachometer-reporter-action v2 for CI

Copy link

github-actions bot commented Feb 17, 2025

Size Change: +107 B (+0.14%)

Total Size: 78.4 kB

Filename Size Change
dist/preact.js 4.72 kB +14 B (+0.3%)
dist/preact.min.js 4.73 kB +15 B (+0.32%)
dist/preact.min.module.js 4.75 kB +14 B (+0.3%)
dist/preact.min.umd.js 4.75 kB +16 B (+0.34%)
dist/preact.mjs 4.74 kB +16 B (+0.34%)
dist/preact.module.js 4.74 kB +16 B (+0.34%)
dist/preact.umd.js 4.76 kB +16 B (+0.34%)
ℹ️ View Unchanged
Filename Size
compat/dist/compat.js 4.12 kB
compat/dist/compat.mjs 4.05 kB
compat/dist/compat.module.js 4.05 kB
compat/dist/compat.umd.js 4.19 kB
debug/dist/debug.js 3.83 kB
debug/dist/debug.mjs 3.83 kB
debug/dist/debug.module.js 3.83 kB
debug/dist/debug.umd.js 3.91 kB
devtools/dist/devtools.js 260 B
devtools/dist/devtools.mjs 274 B
devtools/dist/devtools.module.js 274 B
devtools/dist/devtools.umd.js 346 B
hooks/dist/hooks.js 1.54 kB
hooks/dist/hooks.mjs 1.57 kB
hooks/dist/hooks.module.js 1.57 kB
hooks/dist/hooks.umd.js 1.61 kB
jsx-runtime/dist/jsxRuntime.js 978 B
jsx-runtime/dist/jsxRuntime.mjs 952 B
jsx-runtime/dist/jsxRuntime.module.js 952 B
jsx-runtime/dist/jsxRuntime.umd.js 1.05 kB
test-utils/dist/testUtils.js 473 B
test-utils/dist/testUtils.mjs 477 B
test-utils/dist/testUtils.module.js 477 B
test-utils/dist/testUtils.umd.js 555 B

compressed-size-action

@coveralls
Copy link

coveralls commented Feb 17, 2025

Coverage Status

coverage: 99.609%. remained the same
when pulling 72154eb on fix-memory-leak
into 44ef318 on main.

Copy link
Member

@marvinhagemeister marvinhagemeister left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, really good work on debugging and finding this!!

Copy link
Member

@rschristian rschristian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome find!

@JoviDeCroock JoviDeCroock merged commit 9a8dc3d into main Feb 18, 2025
12 checks passed
@JoviDeCroock JoviDeCroock deleted the fix-memory-leak branch February 18, 2025 05:54
@JoviDeCroock JoviDeCroock mentioned this pull request Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fragments cause memory leak
5 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://redirect.github.com/preactjs/preact/issues/4680

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy