autorun() without disposal doesn't seem to memory-leak? #3185
Answered
by
mweststrate
brundonsmith
asked this question in
Q&A
Replies: 2 comments 2 replies
-
Both junk and autorun run out of scope, so they can be gc-ed together. The
problem arises when a reference is kept to one of those. Typically a single
observable that is used by the autoruns is enough for that.
…On Sat, 13 Nov 2021, 02:09 Brandon Smith, ***@***.***> wrote:
I'm trying to get a deeper understanding of how MobX works, particularly
around reactions and disposal of reactions. To test things out, I wrote the
following code:
import * as mobx from ***@***.***"
function junk() {
let arr = []
for (let i = 0; i < 10000000; i++) {
arr.push(Math.random())
}
return arr}
console.log('running...')for (let i = 0; i < 10; i++) {
const j = mobx.observable.box(junk(), { deep: false });
mobx.autorun(() => {
console.log(j.get().length)
})}console.log('done')
The junk() function generates a large array of noise data, so that its
allocation and freeing will be clearly visible in the profiler. The theory,
in this code, is that the junk() result created in each iteration of this
loop will not be garbage-collected because the autorun() is referencing
it (and is not disposed). But here's what I see in the profiler:
[image: image]
<https://user-images.githubusercontent.com/3674979/141601928-bbf045df-277a-41bc-b8db-a9125a15d446.png>
It very much looks like the arrays are getting collected. Is there a
problem with my test code, or a gap in my mental model?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#3185>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBDFA3LG3TUNRWYDMQ3ULXCHDANCNFSM5H6B6TSA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
brundonsmith
-
That was indeed what I had in mind. Since it can be very subtle, just
always dispose anything that doesnt span the entire lifecycle if the app.
…On Sat, 13 Nov 2021, 04:23 Brandon Smith, ***@***.***> wrote:
One more angle (thank you for taking the time to answer these questions!):
my end goal here is to figure out the circumstances under which it's safe
to *not* dispose reactions. If there's a simple way to define that set of
cases, I would be very grateful/excited to learn it!
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3185 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN4NBCJVISJH4WOSO3OX5DULXR5ZANCNFSM5H6B6TSA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to get a deeper understanding of how MobX works, particularly around reactions and disposal of reactions. To test things out, I wrote the following code:
The
junk()
function generates a large array of noise data, so that its allocation and freeing will be clearly visible in the profiler. The theory, in this code, is that thejunk()
result created in each iteration of this loop will not be garbage-collected because theautorun()
is referencing it (and is not disposed). But here's what I see in the profiler (the blue line is heap memory):It very much looks like the arrays are getting collected. Is there a problem with my test code, or a gap in my mental model?
Edit: Follow-up question; the original thing I was trying to understand was for reaction(), whether disposals are necessary to free values referenced in the data function, or the effect function, or both. But the above case makes that question moot, until it's resolved. But any further insight would be appreciated!
One possibility: does MobX have a way of knowing when a reaction is "no longer being used" in some cases (similar to what it does for @computed), allowing it to automatically dispose them in certain situations?
Beta Was this translation helpful? Give feedback.
All reactions