-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
feat: Trigger Java GC on reload #43813
Conversation
Base commit: ce811a0 |
Seems reasonable, and should only affect development. I'm not 100% confident that the existing runtime will have been torn down by the time this method is called. Perhaps Can you expand the comment in-code with some of the information from this PR? |
@javache what do you think of putting it inside |
Or I guess at the end of |
Okay I moved it to let me know if that makes sense EDIT: hmm actually I don't think this makes sense - for bridge mode this doesn't get called afaik. I'll try to find a better place |
Yes, this will only get called in bridge mode, you'll need an equivalent point in ReactHostImpl for this. |
I just moved it into For me this always hits after refreshing in debug. In release, this isn't used afaik |
sorry for the bump here @javache - have you had a chance to look at this? |
This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
This PR was closed because it has been stalled for 7 days with no activity. |
not stale |
The semantics of this in the new architecture are still not great - we call this when the ReactContext is destroyed, not the ReactInstance - that's outside the scope of this diff though. |
@javache has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Keep in mind that in production we have |
That's good tho, no? We only want this in DEV |
Not sure if I understand - is there anything I can change here? |
This pull request was successfully merged by @mrousavy in de3c1ee When will my fix make it into a release? | How to file a pick request? |
No, outside the scope of this PR. Just means that the GC here may be less effective in the new architecture as the full instance hasn't been destroyed yet. |
Summary:
While we (Margelo) were developing a new C++ 3D library for react-native, we noticed that Java often keeps a lot of dead instances in memory, making it hard to debug memory allocations (or actually de-allocations), especially since we use
jsi::HostObject
andjni::HybridClass
in conjunction. Having two garbage-collected languages retain an object is a bit tricky, and making sure that we aren't doing anything wrong with our allocations and references was not easy - but manually callingSystem.gc()
on app reloads helped us see that much better.Before this, we needed to wait multiple minutes until some Java objects are actually freed from the GC. Our use-case was a
facebook::jni::HybridClass
, which was held strong in afacebook::jsi::HostObject
(so again, two GC'd languages).There should be no change in behaviour with this PR, just two things to note:
System.gc()
only suggests garbage collection, it does not force it. But when it runs, it might impact performance, although we haven't noticed any impact of that at all. The garbage collector runs anyways - better during a reload than later when exceuting the app normally.Changelog:
[ANDROID] [ADDED] - Trigger Java GC on app reload
Test Plan:
Open an app, create a Java module that holds a few objects, add
finalize()
methods to those objects and log their deletion.Reload the app to see the logs, compare before vs after.