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(watch): memory leak #590

Merged
merged 5 commits into from
Jan 8, 2020
Merged

fix(watch): memory leak #590

merged 5 commits into from
Jan 8, 2020

Conversation

yangmingshan
Copy link
Contributor

I think computed have same problem, but unless expose a stop api, I don't think there is an easy way to fix it.

@yyx990803
Copy link
Member

Did you run into a real memory leak with this? I think this is actually fine because watch effects are only recorded if watch() is called synchronously within setup(). No new watchers will be recorded after the setup() of the component, therefore the watchers recorded by an instance is deterministic - i.e. it's not possible to have a long-living component that keeps recording an infinitely growing number of watchers.

@yyx990803
Copy link
Member

Oh nvm, it's possible with this.$watch.

Copy link
Member

@yyx990803 yyx990803 left a comment

Choose a reason for hiding this comment

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

It's ok for computed because there is no API to create instance-bound computed outside of setup().

const effects = currentInstance.effects || (currentInstance.effects = [])
effects.push(effect)
return () => {
const index = effects.findIndex(_effect => _effect === effect)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const index = effects.findIndex(_effect => _effect === effect)
const index = effects.indexOf(effect)

// record effects created during a component's setup() so that they can be
// stopped when the component unmounts
export function recordEffect(effect: ReactiveEffect) {
export function recordEffect(effect: ReactiveEffect): RemoveRecord | void {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
export function recordEffect(effect: ReactiveEffect): RemoveRecord | void {
export function recordEffect(effect: ReactiveEffect): (() => void) | void {

@@ -33,11 +33,20 @@ import {

import { currentInstance } from './component'

type RemoveRecord = () => void
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
type RemoveRecord = () => void

@yangmingshan
Copy link
Contributor Author

No, I didn't run into a real memory leak, I just thought it have risk, actually I didn't think about this.$watch 😂

But this.$watch won't be recorded in effects list right? So, do we need to change the implementation? Can I try?

@yangmingshan
Copy link
Contributor Author

Forget it, I break KeepAlive tests and I don't know why.

@yyx990803 yyx990803 merged commit 453e688 into vuejs:master Jan 8, 2020
@yangmingshan yangmingshan deleted the memory branch January 9, 2020 02:15
yangmingshan added a commit to vue-mini/vue-mini that referenced this pull request Jan 9, 2020
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.

2 participants