# abhilash1401/leakwatch architecture

> Architecture diagram of the GitHub repository abhilash1401/leakwatch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abhilash1401/leakwatch
- Repository: https://github.com/abhilash1401/leakwatch

## Overview

#### Purpose and entry point
LeakWatch is a CLI-oriented Java diagnostic tool that compiles and launches a user-selected Java source program, monitors its external process memory and GC signals, applies a heuristic for possible leaks, optionally terminates the target, and prints a final report. The application entry point and workflow coordinator are `Main.java`; presentation begins with `Header.java`.

#### 1. Program selection boundary
`FileSelector.java` provides the user-facing source-file selection step, using Swing file selection for `.java` targets. Its output is the selected source path/name consumed by the execution pipeline. This separates LeakWatch’s own JVM from the application being diagnosed.

#### 2. Target compilation and process lifecycle
`JavaRunner.java` owns target compilation and execution through Java process APIs/`ProcessBuilder`. It creates the monitored target Java process and supplies its process identity/lifecycle state to monitoring. This is the critical isolation boundary: LeakWatch observes and can stop a separate OS process rather than introspecting its own heap.

#### 3. Cross-platform memory acquisition
`MemoryMonitor.java` periodically samples the target process’s memory usage. It relies on platform commands—PowerShell on Windows and `ps` on Linux/macOS—rather than JVM heap APIs, so measurements represent externally observable process memory. `MemorySample.java` is the time-series record passed downstream; `MemoryUtils.java` formats byte quantities for console output.

#### 4. GC signal observation
`GCMonitor.java` observes garbage-collection activity from the running target, likely via process output/GC logging configured at launch. GC observations are used as evidence that memory should have had an opportunity to recover; they are not object-level or heap-dump analysis.

#### 5. Heuristic leak analysis
`LeakAnalyzer.java` consumes collected `MemorySample` history plus GC observations to evaluate the defining pattern: significant growth, continuous upward trend, GC activity, and no meaningful recovery. It produces `AnalysisResult.java`, the decision boundary between raw monitoring data and the final classification of normal behavior versus a potential leak.

#### 6. Control decision and termination
`Main.java` coordinates sampling, GC observation, and repeated analysis while the target remains active. When the analyzer identifies a suspicious pattern, the orchestration path instructs the process owner in `JavaRunner.java` to stop the target; otherwise monitoring ends when the program completes. This makes detection actionable but intentionally heuristic-driven.

#### 7. Final reporting
`Report.java` renders the terminal summary from `AnalysisResult.java`, collected samples, GC-event count, and process outcome. Reported values include initial/final/peak memory, trend, recovery behavior, leak status, and whether LeakWatch terminated the application.

#### Runtime and operational dependencies
The tool requires JDK 11+ for compiling/running both LeakWatch and selected sources. It uses Java/Swing locally, OS process execution via `ProcessBuilder`, PowerShell on Windows, and `ps` on Linux/macOS. No server, database, persistent storage, or cloud service is indicated.

Main entry points: [`Main.java`](https://github.com/abhilash1401/leakwatch/blob/main/Main.java) (Main), [`Report.java`](https://github.com/abhilash1401/leakwatch/blob/main/Report.java) (Report), [`JavaRunner.java`](https://github.com/abhilash1401/leakwatch/blob/main/JavaRunner.java) (JavaRunner), [`GCMonitor.java`](https://github.com/abhilash1401/leakwatch/blob/main/GCMonitor.java) (GCMonitor)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Selection &amp; presentation"]
  node_main["Main<br/>workflow coordinator<br/>[Main.java]"]
  node_header["Header<br/>console presentation<br/>[Header.java]"]
  node_file_selector["FileSelector<br/>source picker<br/>[FileSelector.java]"]
  node_report["Report<br/>[Report.java]"]
  node_memory_utils["MemoryUtils<br/>byte formatter<br/>[MemoryUtils.java]"]
end

subgraph group_target["Target process lifecycle"]
  node_java_runner{{"JavaRunner<br/>process owner<br/>[JavaRunner.java]"}}
  node_target_process["Selected Java program<br/>external JVM process"]
end

subgraph group_monitoring["External monitoring"]
  node_memory_monitor["MemoryMonitor<br/>external memory sampler<br/>[MemoryMonitor.java]"]
  node_os_memory_commands{{"PowerShell / ps<br/>platform process metrics"}}
  node_memory_sample["MemorySample<br/>time-series record<br/>[MemorySample.java]"]
  node_gc_monitor["GCMonitor<br/>GC signal observer<br/>[GCMonitor.java]"]
end

subgraph group_analysis["Heuristic analysis"]
  node_leak_analyzer["LeakAnalyzer<br/>heuristic engine<br/>[LeakAnalyzer.java]"]
  node_analysis_result["AnalysisResult<br/>classification result"]
end

node_main -->|"starts presentation"| node_header
node_main -->|"requests source"| node_file_selector
node_file_selector -->|"selected source path"| node_java_runner
node_java_runner -->|"compiles and launches"| node_target_process
node_main -->|"coordinates sampling"| node_memory_monitor
node_memory_monitor -->|"queries memory"| node_os_memory_commands
node_os_memory_commands -.->|"reads process metrics"| node_target_process
node_memory_monitor -->|"records samples"| node_memory_sample
node_main -->|"coordinates observation"| node_gc_monitor
node_target_process -.->|"GC output or logs"| node_gc_monitor
node_memory_sample -->|"memory history"| node_leak_analyzer
node_gc_monitor -->|"GC observations"| node_leak_analyzer
node_leak_analyzer -->|"classification"| node_analysis_result
node_analysis_result -->|"suspicion decision"| node_main
node_main -->|"stop on suspected leak"| node_java_runner
node_java_runner -->|"terminates when instructed"| node_target_process
node_main -->|"final outcome and data"| node_report
node_analysis_result -->|"leak classification"| node_report
node_memory_sample -->|"memory summary"| node_report
node_gc_monitor -->|"GC event count"| node_report
node_memory_utils -->|"formats byte values"| node_report

click node_main "https://github.com/abhilash1401/leakwatch/blob/main/Main.java"
click node_header "https://github.com/abhilash1401/leakwatch/blob/main/Header.java"
click node_file_selector "https://github.com/abhilash1401/leakwatch/blob/main/FileSelector.java"
click node_java_runner "https://github.com/abhilash1401/leakwatch/blob/main/JavaRunner.java"
click node_memory_monitor "https://github.com/abhilash1401/leakwatch/blob/main/MemoryMonitor.java"
click node_memory_sample "https://github.com/abhilash1401/leakwatch/blob/main/MemorySample.java"
click node_gc_monitor "https://github.com/abhilash1401/leakwatch/blob/main/GCMonitor.java"
click node_leak_analyzer "https://github.com/abhilash1401/leakwatch/blob/main/LeakAnalyzer.java"
click node_analysis_result "https://github.com/abhilash1401/leakwatch/blob/main/AnalysisResult.java"
click node_report "https://github.com/abhilash1401/leakwatch/blob/main/Report.java"
click node_memory_utils "https://github.com/abhilash1401/leakwatch/blob/main/MemoryUtils.java"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_header,node_file_selector,node_report,node_memory_utils toneBlue
class node_java_runner,node_target_process toneAmber
class node_memory_monitor,node_os_memory_commands,node_memory_sample,node_gc_monitor toneMint
class node_leak_analyzer,node_analysis_result toneRose
```

## Components

### Selection & presentation

- [Main](https://github.com/abhilash1401/leakwatch/blob/main/Main.java): workflow coordinator · CLI entry point coordinating the diagnostic lifecycle. · `Main.java`
- [Header](https://github.com/abhilash1401/leakwatch/blob/main/Header.java): console presentation · `Header.java`
- [FileSelector](https://github.com/abhilash1401/leakwatch/blob/main/FileSelector.java): source picker · Swing selection boundary for .java targets. · `FileSelector.java`
- [Report](https://github.com/abhilash1401/leakwatch/blob/main/Report.java): terminal report renderer · Summarizes memory, trend, recovery, status, and outcome. · `Report.java`
- [MemoryUtils](https://github.com/abhilash1401/leakwatch/blob/main/MemoryUtils.java): byte formatter · `MemoryUtils.java`

### Target process lifecycle

- [JavaRunner](https://github.com/abhilash1401/leakwatch/blob/main/JavaRunner.java): process owner · Compiles, launches, and can stop the separate target JVM. · `JavaRunner.java`
- **Selected Java program**: external JVM process · User-selected source running as an isolated OS process.

### External monitoring

- [MemoryMonitor](https://github.com/abhilash1401/leakwatch/blob/main/MemoryMonitor.java): external memory sampler · Periodically reads target memory through OS commands. · `MemoryMonitor.java`
- **PowerShell / ps**: platform process metrics · Windows PowerShell or Linux/macOS ps provides external memory data.
- [MemorySample](https://github.com/abhilash1401/leakwatch/blob/main/MemorySample.java): time-series record · `MemorySample.java`
- [GCMonitor](https://github.com/abhilash1401/leakwatch/blob/main/GCMonitor.java): GC signal observer · Observes target GC activity via output or GC logging. · `GCMonitor.java`

### Heuristic analysis

- [LeakAnalyzer](https://github.com/abhilash1401/leakwatch/blob/main/LeakAnalyzer.java): heuristic engine · Evaluates growth, trend, GC activity, and recovery. · `LeakAnalyzer.java`
- [AnalysisResult](https://github.com/abhilash1401/leakwatch/blob/main/AnalysisResult.java): classification result · `AnalysisResult.java`

## Connections

- Main → Header: starts presentation
- Main → FileSelector: requests source
- FileSelector → JavaRunner: selected source path
- JavaRunner → Selected Java program: compiles and launches
- Main → MemoryMonitor: coordinates sampling
- MemoryMonitor → PowerShell / ps: queries memory
- PowerShell / ps → Selected Java program: reads process metrics
- MemoryMonitor → MemorySample: records samples
- Main → GCMonitor: coordinates observation
- Selected Java program → GCMonitor: GC output or logs
- MemorySample → LeakAnalyzer: memory history
- GCMonitor → LeakAnalyzer: GC observations
- LeakAnalyzer → AnalysisResult: classification
- AnalysisResult → Main: suspicion decision
- Main → JavaRunner: stop on suspected leak
- JavaRunner → Selected Java program: terminates when instructed
- Main → Report: final outcome and data
- AnalysisResult → Report: leak classification
- MemorySample → Report: memory summary
- GCMonitor → Report: GC event count
- MemoryUtils → Report: formats byte values
