Mobx: Do reactions automatically get cleaned up when an observable is not longer present?

Created on 29 Jun 2018  Â·  4Comments  Â·  Source: mobxjs/mobx

I have a...

  • [x] Question
  • [x] Did you check this issue wasn't filed before?

Hi!

I am wondering if mobx / GC automatically dispose reactions and observers on observables, that have been dereferenced, or if I have to make sure that their dispose methods get called and thereby release unused memory by myself.

I tried to illustrate the case with the following example:
https://codesandbox.io/s/94zmr52l9w

How could I check, if the reaction is still registered within mobx's tracking cycle?

Thanks!

Most helpful comment

it will be alive as long as this.isHungry is alive. In this case it is
easy to oversee the impact, bug in general I recommend to be a good citizen
and clean up any subscriptions you make. Like you would do in Rx for example

Op vr 29 jun. 2018 om 16:40 schreef ManuSevenval notifications@github.com:

thanks for the quick response!
just out of curiosity:

Otherwise, the engine can only GC a

reaction if all things on which the reaction depends can be GC-ed as
well.

in my particular example the reaction looks like this:

reaction(
() => this.isHungry,
() => document.getElementById('root').innerHTML = "FEED WALTER!!",
)

would the GC keep the reaction alive because #root is still present? Or
would it be clever enough to notice, that the class instance was killed and
collect the reaction anyways?

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx/issues/1617#issuecomment-401374767, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ABvGhMYyyajjxdCvDfb8VOXKAW1aIWUTks5uBjxLgaJpZM4U8205
.

All 4 comments

Reactions returns a disposer function that can be used to stop the
observation and thus enable GC. Otherwise, the engine can only GC a
reaction if all things on which the reaction depends can be GC-ed as
well. Something I wouldn't rely on too much :)

Op vr 29 jun. 2018 om 13:15 schreef ManuSevenval notifications@github.com:

I have a...

  • Question
  • Did you check this issue wasn't filed before?

Hi!

I am wondering if mobx / GC automatically dispose reactions and observers
on observables, that have been dereferenced, or if I have to make sure that
their dispose methods get called and thereby release unused memory by
myself.

I tried to illustrate the case with the following example:
https://codesandbox.io/s/94zmr52l9w

How could I check, if the reaction is still registered within mobx's
tracking cycle?

Thanks!

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx/issues/1617, or mute the thread
https://github.com/notifications/unsubscribe-auth/ABvGhCEDm-zWjjLrJO8wenehnb847Q8Xks5uBgxjgaJpZM4U8205
.

thanks for the quick response!
just out of curiosity:

Otherwise, the engine can only GC a
reaction if all things on which the reaction depends can be GC-ed as
well.

in my particular example the reaction looks like this:

reaction(
      () => this.isHungry,
      () => document.getElementById('root').innerHTML = "FEED WALTER!!",
    )

would the GC keep the reaction alive because #root is still present? Or would it be clever enough to notice, that the class instance was killed and collect the reaction anyways?

it will be alive as long as this.isHungry is alive. In this case it is
easy to oversee the impact, bug in general I recommend to be a good citizen
and clean up any subscriptions you make. Like you would do in Rx for example

Op vr 29 jun. 2018 om 16:40 schreef ManuSevenval notifications@github.com:

thanks for the quick response!
just out of curiosity:

Otherwise, the engine can only GC a

reaction if all things on which the reaction depends can be GC-ed as
well.

in my particular example the reaction looks like this:

reaction(
() => this.isHungry,
() => document.getElementById('root').innerHTML = "FEED WALTER!!",
)

would the GC keep the reaction alive because #root is still present? Or
would it be clever enough to notice, that the class instance was killed and
collect the reaction anyways?

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx/issues/1617#issuecomment-401374767, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ABvGhMYyyajjxdCvDfb8VOXKAW1aIWUTks5uBjxLgaJpZM4U8205
.

alright, thanks! :+1: :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nopik picture Nopik  Â·  33Comments

bySabi picture bySabi  Â·  95Comments

fibric picture fibric  Â·  37Comments

winterbe picture winterbe  Â·  34Comments

mweststrate picture mweststrate  Â·  37Comments