gradle-doctor icon indicating copy to clipboard operation
gradle-doctor copied to clipboard

Reducing high memory consumption

Open eugene-krivobokov opened this issue 1 year ago • 3 comments

Hi!

There was a report about a potential memory leak: https://github.com/runningcode/gradle-doctor/issues/247 It turned out that this is expected and the plugin stores a quite detailed information about build operations to make needed calculation.

The thing is that memory consumption increases so big that it makes using of the plugin problematic on big projects. In my environment this is the top-1 memory consumer.

Therefore, I'd like to revive this topic to clarify potential improvements, if possible.

  • Would it be possible to store this information only for checks that consume it?
  • Are there any ideas to reduce the amount of data or calculate and discard on the fly?

With plugin: Screenshot from 2024-05-27 19-40-58

with_plugin

Without plugin: without_plugin

eugene-krivobokov avatar May 27 '24 19:05 eugene-krivobokov

Hi @eugene-krivobokov thanks for bringing this up. I'm very open to ideas on how to improve this.

I'm wondering, is the memory still retained after a single build completes? At least in the way it is built, it is intentional that these objects are retained during a single build, but not from one build to the next.

runningcode avatar Jul 08 '24 14:07 runningcode

Hi! The memory is released after the build completes. The issue is with high memory consumption while the build is running. It's a limiting factor on laptops, where we can't increase memory easily.

In general, I know two approaches.

  1. Collect this information only if features that actually use it are enabled. In that case, we can use at least some functionality instead of disabling the whole plugin.
  2. Aggregate metrics on the fly with each new piece of tracked operations. Not sure, if this is feasible.

eugene-krivobokov avatar Aug 17 '24 13:08 eugene-krivobokov

Found a workaround to disable BuildOperations collection

build.gradle.kts

// Disable task monitoring by default providing an option to opt-in.
// See 'doctor.enableTaskMonitoring' in gradle.properties for details.
val enableTasksMonitoring = properties.getOrDefault("doctor.enableTaskMonitoring", "false").toString().toBoolean()

if (!enableTasksMonitoring) {
    logger.info("Gradle Doctor task monitoring is disabled.")
    gradle.sharedServices.unregister("listener-service")
}

fun BuildServiceRegistry.unregister(name: String) {
    val registration = registrations.getByName(name)
    registrations.remove(registration)
    (registration.service as RegisteredBuildServiceProvider<*, *>).maybeStop()
}

gradle.properties

# Gradle Doctor might increase memory consumption when task monitoring is enabled, so it is disabled by default.
# Some features can't work without task monitoring:
#  doctor-negative-savings, doctor-slow-build-cache-connection, doctor-slow-maven-connection
# Issue: https://github.com/runningcode/gradle-doctor/issues/348
doctor.enableTaskMonitoring=false

osipxd avatar Nov 21 '24 15:11 osipxd