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

Memory leak when using daemon #25

Open
Dave562CZ opened this issue May 29, 2019 · 7 comments
Open

Memory leak when using daemon #25

Dave562CZ opened this issue May 29, 2019 · 7 comments

Comments

@Dave562CZ
Copy link

When using gradle daemon registered shutdown hooks are not run because task is run inside daemon which keeps running after build is done. And also stays in memory so new shutdown hooks are added with subsequent builds and filling heap until it runs out of heap space.

@woldie
Copy link

woldie commented Jan 14, 2020

We've got Gradle daemon disabled to avoid this shutdown hook memory snowball issue.

For anyone coming from google, if you see the following message a bunch of times on the console along with a sick/stuck Gradle process:

Expiring Daemon because JVM heap space is exhausted

Then, try adding the following to your top-level gradle.properties file to disable Gradle daemon until there's a fix:

# Turns out psxpaul's gradle-exec-plugin has a memory leak caused by a snowballing shutdown hook
# that never fires in the case of gradle running as a daemon.  Until that bug is fixed, I am
# disabling the gradle daemon to avoid the leak.
# See https://github.com/psxpaul/gradle-execfork-plugin/issues/25
org.gradle.daemon=false

Hey @psxpaul, could org.gradle.api.invocation.Gradle#buildFinished be a possible substitute for shutdown hooks in your plugin?

@psxpaul
Copy link
Owner

psxpaul commented Jan 15, 2020

Thanks for the feedback @woldie. I believe I used a shutdown hook because the stop must be run when the user presses ctrl-c to kill the process.

I suspect calling Runtime.removeShutdownHook() would help, but I'm not able to reproduce this locally. Can you provide more information on how to reproduce this? Or perhaps try adding that call to AbstractExecFork.stop() and seeing if that fixes it for you?

@Dave562CZ
Copy link
Author

Hello to replicate this issue it should be enough to run some execfork task several times with using daemon and then do heapdump of that daemon process and there should be multiple threads saved as shutdown hooks even after build is done.

@zielezin
Copy link

Hi @psxpaul,

We faced this issue on our CI infrastructure and running without a daemon is not an option.

After digging into the source code I've found that ForkTaskTerminationService implemented in #45 is already tracking the processes and cleans them up post build - this works nicely with daemon and the wrapper.

I think you could remove these lines and leave cleaning up for the service.

The only use cases it would leave started processes is:

  • running the build in no daemon mode and ctrl-c the build - this is not Gradle default behavior and should be used in very rare cases.
  • killing a daemon in the middle of the build started with gradlew (assuming it's not done with kill -9) - I see no reason to do this ;)

If you would really want to leave the hook in there you could register it, track it in the list and unregister in the service in the same way as tasks are tracked right now.

@psxpaul
Copy link
Owner

psxpaul commented Dec 1, 2022

I've added 8328794 to try to address this. Please try 0.2.2 to see if your issues are fixed.

@zielezin
Copy link

zielezin commented Jan 27, 2023

Hi @psxpaul ,
I've used the 0.2.2 version for some time and it behaves better. The number of OOMs decreased but they still happen from time to time.
I'm trying to figure out what path leads to the thread being not registered - most probably a negative case of some sort.

@onkarjoshi
Copy link

onkarjoshi commented Apr 30, 2023

Saw this issue being easily reproducible locally with v0.2.0. Upgraded to v0.2.2. That's made the problem go away for my use case.

Thanks.

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

No branches or pull requests

5 participants