使用ngonchanges挂钩的内存泄漏

发布于 2025-01-23 10:20:59 字数 290 浏览 1 评论 0原文

当使用ngonchanges挂钩以访问对孩子组件中的输入属性的更改时,我正面临内存泄漏的问题。 我正在从父组件中发送大量数据(带有27000个项目的数组),并且数组中的数据以至少30Hz的频率更改。 我认为,垃圾收集器不会在更改中删除presotValue的引用:SimpleChanges对象的频率与上述频率相同。是否有一种手动方式可以从对象处置数据,或者有更有效的方法可以访问挂钩以外的输入属性上的更改?如果这不是内存泄漏的根本原因,请纠正我?如果需要,我可以共享我的代码。

I am facing an issue with a memory leak when using the ngOnChanges hook to access the changes to an input property in my child component.
I am sending a large amount of data (an array with 27000 items) from my parent component and the data within the array is changing at a frequency of at least 30hz.
I think that the garbage collector is not removing the references of the previousValue in the changes: SimpleChanges object at the same frequency as mentioned above. Is there a manual way I can dispose of the data from the object or is there a more efficient way of accessing the changes on the input property other than the hook? Please correct me if that is not the root cause of the memory leak? I can share my code if required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2025-01-30 10:20:59

如果ngonchanges正在导致内存泄漏,则无法从您的问题的细节中了解 - 如果这是Angular的最新版本,我会想象它不太可能是

将其排除的一种方式,您可以使用@Input而不是ngonchanges

private _data: any[];

@Input() set data(value: any[]) {
  this._data = value;
  this.runUpdates();
}
get data(): any[] {
  // other logic
  return this._data;
}

在使用上述方法时,请确保通过输入绑定IE在新数组中传递IE,IE设置器不会通过阵列突变

There is no way of knowing from the details of your question if ngOnChanges is causing a memory leak - if it's a recent version of Angular I would imagine it's pretty unlikely

As a way of ruling it out, you could use property accessors with @Input instead of ngOnChanges

private _data: any[];

@Input() set data(value: any[]) {
  this._data = value;
  this.runUpdates();
}
get data(): any[] {
  // other logic
  return this._data;
}

When using the above approach, make sure to pass in a new array via the input binding i.e. the setter won't be triggered via array mutations

转瞬即逝 2025-01-30 10:20:59

在处理旧数据后

I was able to fix the issue by disposing of the old array references immeaditaley after I had processed the old data

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文