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

RewriteClassLoader is never freed, effectively a memory leak #132

Closed
sambsnyd opened this issue Oct 14, 2022 · 1 comment
Closed

RewriteClassLoader is never freed, effectively a memory leak #132

sambsnyd opened this issue Oct 14, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@sambsnyd
Copy link
Member

sambsnyd commented Oct 14, 2022

When multiple builds are run in the same Gradle daemon if the rewrite classpath changes we'll create a new RewriteClassLoader, but the old one and all the classes it ever loaded never get garbage collected.

We load JGit within RewriteClassLoader to keep it isolated from the rest of the plugin classpath. But JGit registers a shutdown hook in a static initializer. So that holds a reference to the classloader until the end of the JVM.

This becomes a problem only when you do multiple builds on the same Gradle daemon with different classpaths.

image

This shutdown hook is registered in a static initializer in org.eclipse.jgit.util.FS:
image

We can't load this class from the parent classloader because it's shaded into rewrite-core.
To fix this we have to do something like:

  • Run in a separate process
  • Fork JGit / use a modified class file for that one class that does not register the shutdown hook
  • Bytecode hackery
@sambsnyd sambsnyd added the bug Something isn't working label Oct 14, 2022
@sambsnyd
Copy link
Member Author

This is the branch I was working off of, whenever I come back to try and fix this for real:
https://github.com/openrewrite/rewrite-gradle-plugin/tree/classloader-memory-leak

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

No branches or pull requests

2 participants