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

Memory leak in lit-labs/context #3954

Closed
AndrewJakubowicz opened this issue Jun 14, 2023 · 1 comment · Fixed by #3956
Closed

Memory leak in lit-labs/context #3954

AndrewJakubowicz opened this issue Jun 14, 2023 · 1 comment · Fixed by #3956
Assignees

Comments

@AndrewJakubowicz
Copy link
Member

Which package(s) are affected?

Context (@lit-labs/context)

Description

Issue captured by: https://discord.com/channels/1012791295170859069/1097902841265393716/1116635815527858237

Text from the comment:

I thought this was fixed but it's clear that context has a memory leak. This is a small repro and and in Chrome you can see how the DOM nodes grow and the garbage collector can not remove the about view because it has subscribe: true. If you use Edge to look at detached elements it's more clear that the detached elements are building up as you navigate between home and about multiple times. This is a link to a repro https://lit-context-example.vercel.app/ and this is the repository if you wish to test it without subscribe set to true. When you do, the garbage collector can remove the about view as expected. https://github.com/marcuslindblom/lit-context-example

Thank you @marcuslindblom for reporting this issue

Reproduction

Live repro: https://lit-context-example.vercel.app/

Source code repro: https://github.com/marcuslindblom/lit-context-example

Repro:

  • Open https://lit-context-example.vercel.app/
  • In the performance profiler select memory. Garbage collect & then profile.
  • While profiling switch between Home & About page a couple times.
  • Trigger a garbage collection and stop profiling.
  • Notice that the number of nodes has increased & garbage collection did not clean up everything back to where we started.

This leak results in Nodes accumulating over time.

Workaround

N/A

Is this a regression?

No or unsure. This never worked, or I haven't tried before.

Affected versions

lit-labs/context latest

Browser/OS/Node environment

N/a

@AndrewJakubowicz AndrewJakubowicz changed the title Memory leak in lit-labs/Context Memory leak in lit-labs/context Jun 14, 2023
@AndrewJakubowicz AndrewJakubowicz self-assigned this Jun 14, 2023
@AndrewJakubowicz
Copy link
Member Author

AndrewJakubowicz commented Jun 14, 2023

The issue seems related to the callbacks map here:

private callbacks: Map<ContextCallback<T>, Disposer> = new Map();
maintaining callbacks and growing after every new subscription.

Verified by adding a console.log('we are adding a callback!', this.callbacks.size); message. When switching back and forth between components in the bug reproduction, the size of the callbacks grows. Nothing disposes the callbacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant