How FixrLeak Uses Generative AI to Automatically Fix Java Resource Leaks

FixrLeak combines AST analysis with generative AI to detect and automatically repair Java resource leaks, dramatically reducing manual effort, improving code quality, and demonstrating the transformative potential of AI‑driven solutions in large‑scale software engineering.

FunTester
FunTester
FunTester
How FixrLeak Uses Generative AI to Automatically Fix Java Resource Leaks

Introduction

Resource leaks—such as unreleased files, database connections, or streams—have long plagued Java applications, causing performance degradation and system failures. While tools like SonarQube can identify these leaks, fixing them remains manual, time‑consuming, and error‑prone.

FixrLeak is a generative‑AI‑based framework that automatically detects and repairs resource leaks. By integrating abstract syntax tree (AST) analysis with large language models, it generates language‑idiomatic fixes that follow Java best practices like try‑with‑resources.

Background

In Java, failing to close resources (e.g., BufferedReader) can exhaust file descriptors and destabilize the system. Traditional approaches rely on try/catch/finally blocks, but missing or incorrect finally clauses often lead to leaks. Modern best practice recommends try‑with‑resources, which automatically closes resources even when exceptions occur.

FixrLeak was deployed on Uber’s massive Java codebase, significantly reducing manual work, boosting developer productivity, and improving code quality.

Architecture

FixrLeak’s workflow (see Figure 3) begins by scanning SonarQube reports for resource‑leak warnings, extracting file names and line numbers. It computes deterministic hashes based on file and function names to track leaks across code changes. The tool then uses the Tree‑sitter library to parse code and extract relevant functions for analysis.

AST‑level analysis ensures that only safe, function‑scoped leaks are fixed, avoiding scenarios where a resource is passed, returned, or stored in a field—situations that could introduce use‑after‑close errors.

For each eligible leak, FixrLeak crafts a custom prompt and sends it to a generative AI model (e.g., OpenAI’s ChatGPT‑4O). The model returns a suggested fix, which FixrLeak applies to produce a leak‑free version of the function. Finally, FixrLeak creates a pull request, runs build verification, executes all tests, and re‑runs SonarQube to confirm the leak is resolved before developers review and merge.

Uber Use Case

FixrLeak was evaluated on 124 resource‑leak instances reported by SonarQube in Uber’s Java codebase. After discarding 12 leaks in deprecated code, 112 remained for AST analysis. Of the 102 cases that met the function‑scope criteria, FixrLeak automatically repaired 93 leaks.

The tool’s focused, high‑accuracy approach demonstrates how generative AI can be safely integrated into large‑scale software engineering pipelines.

Future Directions

Support cross‑function repairs : Extend the tool to handle leaks that span multiple methods.

GenAI‑driven leak detection : Apply generative AI to identify leaks in languages lacking dedicated detectors, such as Go.

Advanced source‑code analysis : Improve detection accuracy for user‑defined resource types.

Conclusion

Resource leaks remain a persistent challenge in large systems, but generative AI offers a powerful new remedy. FixrLeak bridges detection and remediation by coupling static analysis with AI‑generated fixes, delivering accurate, efficient repairs while boosting developer productivity. Organizations facing similar challenges can adopt comparable AI‑assisted code‑repair pipelines to enhance code quality and reliability at scale.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaASTstatic analysisGenerative AIresource leakcode repair
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.