Java
Coding Tips
Project Management
Software Development
Code Cleanup

How to find unused and dead code in Java projects

Codemia Knowledge Hub ·

Combine a static analyzer such as PMD or SpotBugs, a coverage tool such as JaCoCo, and your IDE's inspections (IntelliJ IDEA's Inspect Code or Eclipse's compiler warnings). Each catches a different kind of unused code, and manual review covers what they miss. Removing dead code reduces complexity, maintenance overhead, and the room for bugs.

Static analysis tools

Static analyzers examine code without running it and flag code that is never called or executed.

1. PMD

PMD is a popular static analyzer for Java and other languages. It detects unused local variables, parameters, and private methods, and its built-in rules can be extended or customized for a project.

Example:

bash
pmd check -d /path/to/codebase -R category/java/bestpractices.xml -f html -r report.html

This runs PMD on the codebase with the bestpractices category, which includes UnusedPrivateField, UnusedPrivateMethod, UnusedLocalVariable and UnusedFormalParameter, and writes an HTML report.

2. FindBugs/SpotBugs

SpotBugs is the successor to FindBugs and focuses on finding bugs in Java code. It includes detectors for dead code such as unused variables, methods, and parameters.

Example:

bash
spotbugs -textui -output report.txt -effort:max /path/to/codebase

This analyzes the codebase at the maximum effort level and saves the report to a text file.

Code coverage tools

Coverage tools find dead code indirectly, by showing code that no test ever executes.

3. JaCoCo

JaCoCo is a widely used coverage library. Add it to the build and its reports highlight code that never ran during the tests.

Maven integration:

xml
1<plugin>
2  <groupId>org.jacoco</groupId>
3  <artifactId>jacoco-maven-plugin</artifactId>
4  <version>0.8.16</version>
5  <executions>
6    <execution>
7      <goals>
8        <goal>prepare-agent</goal>
9      </goals>
10    </execution>
11    <execution>
12      <id>report</id>
13      <phase>test</phase>
14      <goals>
15        <goal>report</goal>
16      </goals>
17    </execution>
18  </executions>
19</plugin>

Review the coverage reports to spot code that may be dead.

IDE inspections

IntelliJ IDEA and Eclipse both have built-in tools and plugins for finding unused code.

4. IntelliJ IDEA

Use Analyze -> Inspect Code to find unused code across the whole project or a single module.

5. Eclipse

The Eclipse Java compiler (ECJ) warns about unused private members, local variables, and more. Configure it under Window -> Preferences -> Java -> Compiler -> Errors/Warnings.

Manual review

Periodic code reviews and refactoring complement the tools. Checking import statements and public API usages by hand catches subtle cases that automated tools overlook.

Approaches compared

TechniqueTool/ApproachStrengthsWeaknesses
Static AnalysisPMD, SpotBugsIdentifies clear-cut unused codeMay not detect indirectly used code
Code CoverageJaCoCoHighlights unexecuted code during testsDepends on the quality of test cases
IDE ToolsIntelliJ, EclipseIntegrates easily into development flowMay vary in effectiveness by settings
Manual ReviewCode ReviewsCaptures nuances and context-specific casesTime-consuming, depends on reviewer

Run these checks regularly as part of the development lifecycle rather than as a one-off cleanup.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms