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

[ConfigProvider]ConfigProvider 在 SSR 的场景下会发生内存泄漏 #2920

Closed
Boelroy opened this issue Apr 23, 2021 · 3 comments
Closed
Assignees

Comments

@Boelroy
Copy link

Boelroy commented Apr 23, 2021

Component

ConfigProvider

Steps to reproduce

在 SSR 服务的压测下,我们发现 ConfigProvider 的内置缓存会发生内存的增长,没办法被 GC 掉

image

通过翻阅源码之后发现,虽然 这里 会在 componentWillUnmount 中移除掉这个缓存,但是在 React Issue 里面发现 facebook/react#3714 发现在 SSR 的场景下面并不会执行 componentWillUnmount 的生命周期。

所以看看能不能提供出来一个手动清除缓存的 API 或者 其他更好的手段去回收 ConfigProvider 里面的缓存。

@youluna
Copy link
Member

youluna commented Apr 23, 2021

constructor(...args) {
super(...args);
childContextCache.add(this, Object.assign({}, childContextCache.get(this, {}), this.getChildContext()));
setMomentLocale(this.props.locale);
setDateLocale(this.props.locale);
this.state = {
locale: this.props.locale,
};
}

带来的副作用

@youluna
Copy link
Member

youluna commented Apr 25, 2021

2个思路来解决:
1. Map 换成 WeakMap
2. 增加静态方法clearCache回收全局变量childContextCache

========
没办法换成WeakMap,因为现在关心顺序 #286

@Boelroy
Copy link
Author

Boelroy commented Apr 26, 2021

ConfigProvider 没提供 clearCache 的 types. 还需要修改一下

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

No branches or pull requests

2 participants