How JaCoCo Can Reveal and Eliminate Zombie Code to Boost Backend Efficiency
By instrumenting Java services with JaCoCo’s agent and analyzing runtime coverage data, teams can identify unused (“zombie”) code, safely remove or deactivate it, and consequently reduce maintenance costs, shorten delivery cycles, and improve overall system performance and developer productivity.
1. Situation & Problem
Rapid requirement iteration causes business code to grow quickly. While developers may be proud of high code output, they often lack visibility into how much of that code is actually used by online customers, leading to hidden "zombie" code that inflates maintenance cost.
2. Why Zombie Code Appears
Initial product analysis missed certain business scenarios.
During development, functional direction deviated from the original plan.
After launch, external factors reduced customer traffic.
Other unspecified reasons.
3. Measures
Use JaCoCo (Java Code Coverage) to probe runtime execution, identify which classes and methods are never invoked, and then safely deactivate or delete the corresponding dead code.
4. Implementation Steps
4.1 Add JaCoCo Ant Dependency
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.ant</artifactId>
<version>0.8.3</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.9</version>
</dependency>4.2 Expose a REST Endpoint to Dump Coverage
@RestController
@RequestMapping("/coverage")
public class CoverageController {
@PostMapping("dump")
@NoCheckMenuPermission
public Result<Boolean> dumpCoverageFile() {
DumpTask dumpTask = new DumpTask();
// dump file location
dumpTask.setDestfile(new File("/export/Data/coverage/code-cover.exec"));
// append on multiple dumps
dumpTask.setAppend(true);
// choose an idle port
dumpTask.setPort(8840);
// bind to localhost
dumpTask.setAddress("127.0.0.1");
dumpTask.execute();
return Result.succeed(true);
}
}4.3 Embed JaCoCo Agent
Decompress the JaCoCo agent JAR to avoid involving operations personnel.
# Decompress dependency to obtain jacocoagent.jar
jar -xvf $BASEDIR/lib/org.jacoco.agent-0.8.3.jar -javaagent:$BASEDIR/bin/jacocoagent.jar=includes=com.jdwl.*,output=tcpserver,port=8840,address=127.0.0.1 -Xverify:none4.4 Reserve JDOS Resource
Configure a custom /export directory for coverage files and add a cleanup script.
#!/bin/bash
ls -lh /export | awk 'NR >1 {print}' | awk '{if ($9 != "Data") print $9}' | xargs -i /bin/rm -rf /export/{} > /dev/null 2>&1
ls -lh /export/Data | awk 'NR >1 {print}' | awk '{if ($9 != "jdos.jd.com" && $9 != "coverage") print $9}' | xargs -i /bin/rm -rf /export/Data/{} > /dev/null 2>&14.5 Download Coverage File
The generated file is located at /export/Data/coverage/code-cover.exec.
4.6 Analyze Coverage
Open IntelliJ IDEA → Run → Show Coverage Data, select the .exec file, and view green (covered) versus red (uncovered) lines.
5. Efficiency Gains
5.1 Faster Delivery
After removing dead code, the average delivery cycle dropped from about 15 days to 12 days (January 2023).
5.2 Reduced Development Phase Duration
Development stage time shortened from 4.54 days to 3.11 days, a ~31 % reduction.
5.3 Human Efficiency
Removing zombie code lowers cyclomatic complexity and duplicate blocks (from ~31 to ~27), reducing cognitive load and maintenance cost.
Per‑person demand throughput increased from ~1.5 to ~2.5 after January 2023.
5.4 Process Quality
Automation bugs fell from 11/month to 5/month, and unit test coverage rose from 51.33 % to 52.28 %.
6. Brief Summary
Continuous requirement iteration inevitably expands codebases, and without runtime visibility, dead code accumulates, raising operational costs. Leveraging JaCoCo probes to measure actual code execution enables targeted deprecation, shrinking the codebase, lowering developers’ cognitive load, and improving delivery speed, human efficiency, and overall software quality.
References
JaCoCo Documentation – https://www.jacoco.org/jacoco/trunk/doc/index.html
javaagent Guide – https://www.cnblogs.com/rickiyang/p/11368932.html
Practical JaCoCo Coverage on Services – https://www.cnblogs.com/ccoder/p/15369719.html#4946710
Bytecode Manipulation with ASM & Javassist – https://newrelic.com/blog/best-practices/java-performance-monitoring
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JD Retail Technology
Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
