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:
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:
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:
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
| Technique | Tool/Approach | Strengths | Weaknesses |
| Static Analysis | PMD, SpotBugs | Identifies clear-cut unused code | May not detect indirectly used code |
| Code Coverage | JaCoCo | Highlights unexecuted code during tests | Depends on the quality of test cases |
| IDE Tools | IntelliJ, Eclipse | Integrates easily into development flow | May vary in effectiveness by settings |
| Manual Review | Code Reviews | Captures nuances and context-specific cases | Time-consuming, depends on reviewer |
Run these checks regularly as part of the development lifecycle rather than as a one-off cleanup.
Related reading
- How to find/remove unused dependencies in Gradle
- How to fix Consider defining a bean of type 'org.jooq.DSLContext' in your configuration. after update to jOOQ 3.15.0
- How to fix Hibernate LazyInitializationException failed to lazily initialize a collection of roles, could not initialize proxy - no Session
- How to fix java.io.NotSerializableException org.apache.kafka.clients.consumer.ConsumerRecord in Spark Streaming Kafka Consumer?
- How to fix java.lang.OutOfMemoryError Direct buffer memory in flink kafka consumer
- How to fix java.net.SocketException Broken pipe?
- How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session
- How to fix the Found Netty''s native epoll transport in the classpath, but epoll is not available. Using NIO instead warning?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.