0
0
Jenkinsdevops~5 mins

Disk space management in Jenkins - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Disk space management
O(n)
Understanding Time Complexity

When managing disk space in Jenkins, it is important to understand how the time to clean or check disk usage grows as the amount of data increases.

We want to know how the time needed changes when there are more files or builds to handle.

Scenario Under Consideration

Analyze the time complexity of the following Jenkins pipeline script that deletes old build artifacts to free disk space.


pipeline {
  agent any
  stages {
    stage('Clean Disk') {
      steps {
        script {
          def builds = currentBuild.rawBuild.project.builds
          builds.each { build ->
            if (build.getTimeInMillis() < System.currentTimeMillis() - 30L*24*60*60*1000) {
              build.delete()
            }
          }
        }
      }
    }
  }
}
    

This script loops through all builds and deletes those older than 30 days to manage disk space.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Looping through all builds in the project.
  • How many times: Once for each build stored in Jenkins.
How Execution Grows With Input

The time to check and delete builds grows as the number of builds increases.

Input Size (n)Approx. Operations
1010 checks and possible deletions
100100 checks and possible deletions
10001000 checks and possible deletions

Pattern observation: The work grows directly with the number of builds; doubling builds doubles the work.

Final Time Complexity

Time Complexity: O(n)

This means the time needed grows in a straight line with the number of builds to check.

Common Mistake

[X] Wrong: "Deleting old builds takes the same time no matter how many builds there are."

[OK] Correct: Each build must be checked, so more builds mean more work and more time.

Interview Connect

Understanding how disk cleanup time grows helps you design Jenkins jobs that scale well and keep the system healthy.

Self-Check

"What if we only checked the last 50 builds instead of all builds? How would the time complexity change?"