Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

observer function leaks memory on the server #140

Closed
doctyper opened this issue Oct 25, 2016 · 6 comments
Closed

observer function leaks memory on the server #140

doctyper opened this issue Oct 25, 2016 · 6 comments

Comments

@doctyper
Copy link

Wrapping a component in observer() leaks memory when rendering universally. This is due to componentWillMount creating a reaction that is never disposed, since componentWillUnmount is not called on the server.

My current workaround involves avoiding wrapping a component in observer() when rendering on the server. This works, but obviously it's not an elegant solution:

import React from "react";
import {observer} from "mobx-react";
import {canUseDOM} from "exenv";

const Example = () => (<div>Example</div>);

export default (canUseDOM ? observer(Example) : Example)

An easy solution is to bail on the server on componentWillMount, though I'm not sure if there would be side-effects in doing so. Let me know if you'd like me to attempt a PR.

@mweststrate
Copy link
Member

@doctyper not a solution, but note that that not unmounting the observer components will only create a mem leak if the observables they use live longer then the request in which you render the components. Otherwise the used observables run out of scope as well, and the components should still be GC-ed

@doctyper
Copy link
Author

doctyper commented Nov 1, 2016

Hm, I'm not sure how accurate that statement is. We incorporated MobX into react-wildcat a few weeks ago, and within the hour we observed an enormous amount of leaked memory that was not being garbage collected.

The leak was mitigated when I added the fix to bail on the server.

@mweststrate
Copy link
Member

@doctyper what would you think about having a global mobxReact setting, that changes this behavior on the fly? This is because trying to detect it automatically might interfer with Jest / Enzyme tests etc that are run 'server side'.

For example:
mobxReact.useServerSideRendering(true) ?

@mattruby
Copy link
Member

mattruby commented Nov 2, 2016

I think that flag would work well.

On Wed, Nov 2, 2016 at 3:57 PM, Michel Weststrate notifications@github.com
wrote:

@doctyper https://github.com/doctyper what would you think about having
a global mobxReact setting, that changes this behavior on the fly? This is
because trying to detect it automatically might interfer with Jest / Enzyme
tests etc that are run 'server side'.

For example:
mobxReact.useServerSideRendering(true) ?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#140 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAIrcm3nh2uQ3_78bk6NB7dMOZwk61uuks5q6Pk4gaJpZM4KgRUS
.

-Matt Ruby-
mattruby@gmail.com

@doctyper
Copy link
Author

doctyper commented Nov 2, 2016

As long as it's opt-in, yes that would work well indeed.

@mweststrate
Copy link
Member

Released as 3.5.9, call is useStaticRendering(true), which is more descriptive I think. Let us know if that doesn't solve the issue!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants