Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added disposing/release mechanism to the system #131

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

chrfalch
Copy link
Collaborator

To make sure we free any underlying values due to the dreaded hermes GC issue where internally allocated memory is not calcualated when considering garbage collection we need to implement this ourselves.

A shared value is now disposed and all resources allocated should be released. This is done through wrappers.

When deleting argument wrappers we also release any resources.

Fixes #129

To make sure we free any underlying values due to the dreaded hermes GC issue where internally allocated memory is not calcualated when considering garbage collection we need to implement this ourselves.

A shared value is now disposed and all resources allocated should be released. This is done through wrappers.

When deleting argument wrappers we also release any resources.

Fixes #129
@chrfalch chrfalch requested a review from mrousavy October 28, 2023 10:15
- Added calling dispose from useSharedValue
- Ran clang / cpp lint
@mrousavy
Copy link
Member

Wait, how does this work? Do we need to manually call close/dispose now when we no longer need a value?
How do we know when we no longer need a value, is there now a ref counting mechanism here?

@chrfalch
Copy link
Collaborator Author

Wait, how does this work? Do we need to manually call close/dispose now when we no longer need a value? How do we know when we no longer need a value, is there now a ref counting mechanism here?

No need for users to call dispose - but f.ex. in the useSharedValue hook we can do a dispose when we unmount - freeing up untracked (for Hermes) memory. Also after passing arguments through worklets we now dispose these JSI values that are no longer needed so that the memory pressure that Hermes uses for calculating gc is more correct.

@mrousavy
Copy link
Member

Also after passing arguments through worklets we now dispose these JSI values that are no longer needed so that the memory pressure that Hermes uses for calculating gc is more correct

How does that happen?

@mskalra
Copy link

mskalra commented Dec 14, 2023

Hey @chrfalch / @mrousavy any updates on this fix?

I believe I'm dealing with a similar memory leak issue as mrousavy/react-native-vision-camera#1953 / #129.

Much appreciated, thanks!

@mskalra
Copy link

mskalra commented Dec 18, 2023

Hey @chrfalch -- quick FYI I monkey patched these changes into our build and it worked great. Memory leak is no longer an issue on iOS (have not tested Android).

We are officially unblocked with this patch, but would love to get this fix into main for others. Lmk how I can help, thanks!

@bglgwyng
Copy link
Contributor

How is this PR related to facebook/hermes@aae2c42?
Is this PR going to be close in favor of the usage of the new method introduced there?

@chrfalch
Copy link
Collaborator Author

Yes it is definitely related. If it will replace it or be used in addition is not yet clear.

Comment on lines 14 to 17
// Free on unmount
useEffect(() => {
return ref.current?.dispose();
}, []);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will immediately free the shared value, I think we only want to run this in the cleanup function of the useEffect hook, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely - this looks like the exact reason for having great people doing code reviews! Thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed now.

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

Successfully merging this pull request may close these issues.

Memory leak when calling JS functions
5 participants