How to Identify and Remove Unused Java Code with SA and JACG
This article explains how rapid business growth leads to dead Java code, defines unused code at the method level, compares runtime and full‑method collection techniques, describes the algorithm for deriving not‑executed methods, and outlines an architecture that combines ServiceAbility Agent and java‑all‑call‑graph for systematic code slimming.
Background
As the company's business expands rapidly, the amount of dead and redundant code grows, causing longer onboarding, longer compile and deployment times.
Design Thought
Define "code to delete" as redundant code that does not affect business logic. "Unused code" is code that has not been executed in production for a long time. The smallest granularity for deletion is at the method level.
Technical Selection
Runtime Method Collection
Two solutions were compared: a profiler‑based approach and the ServiceAbility Agent (SA). SA scores higher on collection efficiency, sampling rate, and fault tolerance, while its development difficulty can be mitigated through research. Therefore SA was chosen.
Full Method Collection
Three solutions were evaluated: IDE built‑in scanning, SonarQube, and the open‑source java‑all‑call‑graph (JACG). JACG offers good integration, supports web services and has lower development effort, so it was selected.
Algorithm for Not‑Executed Methods (NEM)
Not‑executed methods are derived by subtracting the set of runtime methods from the set of all methods. A method is uniquely identified by its package‑class name, method name and parameter list. The process includes:
Collect runtime methods via SA (attach to JVM, parse JNI descriptors, upload data).
Obtain all methods via JACG (upload class archives, call JACG API, filter by application, environment, and package prefixes).
Compute the difference to produce the NEM list, which drives subsequent code‑removal actions.
Architecture Design
The system consists of three modules:
Runtime Method : collected by SA and stored in method_info.
Full Method : generated by JACG and stored in method_info.
Not‑Executed Method : the diff between the above two sets.
Key components include jvm‑method‑counter (CLI tool for runtime method counting), jacg (Web service for call‑graph analysis), and code‑thinner‑api (service that orchestrates the three modules).
Typical workflow: obtain target JVM PID → attach SA agent → parse method signatures → upload to backend → JACG processes class files → diff calculation → expose NEM for cleanup.
Conclusion
SA was adopted for runtime method collection due to its efficiency and low impact, while JACG was chosen for full method extraction because of its integration ease and web‑service capability. The combined approach enables accurate identification of dead code for systematic removal.
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.
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.
