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

Don't use mono_gc_is_moving for incremental gc #1228

Merged
merged 3 commits into from
Sep 26, 2019

Conversation

jechter
Copy link

@jechter jechter commented Sep 25, 2019

When I added incremental bdwgc support to mono, I used the existing mono_gc_is_moving function from the mono sgen implementation to make sure that mono would generate write barriers everywhere it is needed. But returning true from mono_gc_is_moving does more than just enabling write barriers. It would also in several situations use indirect handles for GC objects, as those might be moved. This is not needed for incremental bdwgc, so it is an unnecessary cost. But, more importantly, it turned out that some memory referred to by such handles would not be freed after domain reloads, causing massive memory leaks in the unity editor.

I believe the specific case where such objects would leak is when generating code to refer to static readonly objects. In the non-moving case, mono would just write the pointer value into the emitted code (since it would never change), in the moving case, it would create an indirect handle. It appeared that this handle would not be released, and such objects would thus leak. I don't know if this is a bug in sgen as well (possible, because I guess not many users of mono other then Unity rely on domain reloads) or if that is specific to using this code in bdwgc.

But anyways, the fact is that this is not needed for incremental bdwgc. So, now we no longer return true from mono_gc_is_moving in the incremental bdwgc case, and instead also check for mono_gc_is_incremental in any case where this check is used to implement write barriers. This fixes the memory leaks in the Unity editor.

@jechter
Copy link
Author

jechter commented Sep 25, 2019

This will need backports to 19.3 and 19.2

Copy link
Member

@joncham joncham left a comment

Choose a reason for hiding this comment

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

Looks good. Longer term it would be nice to have a specific function for needed check, like mono_gc_needs_write_barriers().

@jechter
Copy link
Author

jechter commented Sep 26, 2019

Looks good. Longer term it would be nice to have a specific function for needed check, like mono_gc_needs_write_barriers().

That's a good point, that will make it safer in the future - will change it.

@jechter jechter merged commit 17790f1 into unity-master Sep 26, 2019
@jechter jechter deleted the gc-incremental-moving-split branch September 26, 2019 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants