rxjs icon indicating copy to clipboard operation
rxjs copied to clipboard

animationFrames() has memory leak.

Open C-zhui opened this issue 3 years ago • 3 comments

Describe the bug

animationFrames will add subscriptions again again before unsubscribe.

Expected behavior

Here I describe the problem. I can't find the logic of removing sub subscription inside animationFrames. So I add this to source code in Subscription class. image Add this to project code. image You will see this. image Shouldn't they be removed from array:this._finalizers over time???

Reproduction code

As shown in the prev section.

Reproduction URL

No response

Version

7.5.5

Environment

No response

Additional context

No response

C-zhui avatar Apr 24 '22 17:04 C-zhui

I can confirm that it accumulates a new Subscription for each animation frame. The Subscriptions returned from animationFrameProvider.schedule are never closed/unsubscribed, therefore they are not removed from the list of finalizers.

I tried fixing this, but I don't know which is the preferred solution:

  • don't collect all subscriptions in a parent subscription in animationFramesFactory, instead save the latest subscription in a local variable and return a finalizer function that unsubscribes from that subscription
  • in animationFrameProvider.schedule, unsubscribe from the returned subscription after calling the callback
  • don't use animationFrameProvider.schedule at all, because creating a Subscription for every invocation seems unnecessary (wasteful and complex). instead just use animationFrameProvider.requestAnimationFrame

ajafff avatar Apr 24 '22 18:04 ajafff

In my opinion:

  • in animationFrameProvider.schedule, unsubscribe from the returned subscription after calling the callback

DavidWeiss2 avatar Nov 25 '22 21:11 DavidWeiss2