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

Call System.gc between work requests #558

Merged

Conversation

Bencodes
Copy link
Collaborator

@Bencodes Bencodes commented Jul 20, 2021

These long running Kotlin workers are consuming lots of memory, eventually causing the Linux out-of-memory killer to step in and kill a random java process resulting in this error message:

Example crash output (the out file is empty):

--
  | Server terminated abruptly (error code: 14, error message: 'Socket closed', log file: '/root/.cache/bazel/_bazel_root/7b7747ec045ae606eb720a1222f56098/server/jvm.out')
  |  

Calling System.gc between these Kotlin work requests has improved stability for us, and it seems to be the standard in Bazel core (seen here and here).

This implementation is intended to closely mirror what Bazel is doing inside it's worker implementation seen here.

@@ -73,6 +73,8 @@ class PersistentWorker(
.forEach { request ->
launch {
compileWork(request, io, writeChannel, execute)
//Be a friendly worker by performing a GC between compilation requests
System.gc()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mirroring what's being done in the generic worker implementation inside of Bazel might actually be a better approach here that doesn't incur too much gc overhead. I can make those changes if folks think this is the right direction.

https://github.com/Bencodes/bazel/blob/3835d9b21ad524d06873dfbf465ffd2dfb635ba8/src/main/java/com/google/devtools/build/lib/worker/WorkRequestHandler.java#L431-L474

Copy link
Contributor

Choose a reason for hiding this comment

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

I've found in other systems calling System.gc frequently only hurts instead of helping. Calling periodically would certainly be better, but is there other JVM GC settings that can help here instead of manually invoking GC?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am curious why bazel has decided to call System.gc as well - id think tuning the GC a bit more worthwhile. That said the approach you linked @Bencodes seems like a decent compromise.

import java.time.Duration
import java.util.concurrent.atomic.AtomicReference

class CpuTimeBasedGcScheduler(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

It maybe worthwhile for future readers to inline the link to bazel source and part of the PR description in a class comment :)

@restingbull restingbull merged commit ece04e8 into bazelbuild:master Jul 21, 2021
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.

4 participants