This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-jbang-examples.git


The following commit(s) were added to refs/heads/main by this push:
     new fdab85e  Add memory-leak example for JFR diagnostics
fdab85e is described below

commit fdab85e46caa7e89a045726f8517930aea936468
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Jul 2 09:02:45 2026 +0200

    Add memory-leak example for JFR diagnostics
    
    Bundled example that simulates a memory leak (growing HashMap cache
    and ArrayList buffers) for testing the JFR Old Object Sample tool.
    
    Co-Authored-By: Claude <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 camel-jbang-example-catalog.json | 19 +++++++++++++++++++
 memory-leak/README.md            | 24 ++++++++++++++++++++++++
 memory-leak/memory-leak.java     | 40 ++++++++++++++++++++++++++++++++++++++++
 memory-leak/metadata.json        | 12 ++++++++++++
 4 files changed, 95 insertions(+)

diff --git a/camel-jbang-example-catalog.json b/camel-jbang-example-catalog.json
index 33129c7..8ac7469 100644
--- a/camel-jbang-example-catalog.json
+++ b/camel-jbang-example-catalog.json
@@ -269,6 +269,25 @@
             "keycloak"
         ]
     },
+    {
+        "name": "memory-leak",
+        "title": "Memory Leak",
+        "description": "Simulates a memory leak for testing JFR Old Object 
Sample diagnostics",
+        "level": "beginner",
+        "tags": [
+            "observability",
+            "jfr",
+            "diagnostics",
+            "tui"
+        ],
+        "bundled": true,
+        "requiresDocker": false,
+        "hasCitrusTests": false,
+        "files": [
+            "README.md",
+            "memory-leak.java"
+        ]
+    },
     {
         "name": "message-size",
         "title": "Message Size",
diff --git a/memory-leak/README.md b/memory-leak/README.md
new file mode 100644
index 0000000..1391a2d
--- /dev/null
+++ b/memory-leak/README.md
@@ -0,0 +1,24 @@
+## Memory Leak
+
+This example simulates a memory leak for testing the JFR Old Object Sample 
diagnostic tool.
+
+It runs three routes:
+- **cache-leak** — adds a 64 KB entry to a HashMap every 200ms (never evicts)
+- **buffer-leak** — appends a 32 KB byte array to a List every 300ms (never 
shrinks)
+- **healthy** — a normal route with no leak, for comparison
+
+### How to run
+
+    camel run memory-leak.java
+
+### Diagnose with TUI
+
+    camel tui
+
+Navigate to the **JFR Old Objects** tab and press **R** to start a dual 
recording.
+After both runs complete, the comparison table will flag `byte[]` and 
`HashMap$Node`
+as `growing` with high growth ratios, confirming the leak.
+
+### Diagnose with CLI
+
+    camel cmd jfr-old-objects
diff --git a/memory-leak/memory-leak.java b/memory-leak/memory-leak.java
new file mode 100644
index 0000000..9e8b890
--- /dev/null
+++ b/memory-leak/memory-leak.java
@@ -0,0 +1,40 @@
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class memory_leak extends RouteBuilder {
+
+    // these collections grow forever — simulating a memory leak
+    private final Map<String, byte[]> cache = new HashMap<>();
+    private final List<byte[]> buffers = new ArrayList<>();
+
+    @Override
+    public void configure() {
+        // leak 1: growing cache — adds a 64 KB entry every 200ms, never evicts
+        from("timer:cache-leak?period=200")
+            .process(e -> {
+                String key = "entry-" + cache.size();
+                cache.put(key, new byte[65536]);
+                e.getMessage().setBody("Cache size: " + cache.size()
+                    + " (~" + (cache.size() * 64) + " KB)");
+            })
+            .to("log:cache-leak?level=INFO");
+
+        // leak 2: growing buffer list — adds a 32 KB buffer every 300ms
+        from("timer:buffer-leak?period=300")
+            .process(e -> {
+                buffers.add(new byte[32768]);
+                e.getMessage().setBody("Buffers: " + buffers.size()
+                    + " (~" + (buffers.size() * 32) + " KB)");
+            })
+            .to("log:buffer-leak?level=INFO");
+
+        // normal route for comparison — no leak
+        from("timer:healthy?period=5000")
+            .setBody(constant("I am healthy"))
+            .to("log:healthy?level=INFO");
+    }
+}
diff --git a/memory-leak/metadata.json b/memory-leak/metadata.json
new file mode 100644
index 0000000..f44f241
--- /dev/null
+++ b/memory-leak/metadata.json
@@ -0,0 +1,12 @@
+{
+    "title": "Memory Leak",
+    "description": "Simulates a memory leak for testing JFR Old Object Sample 
diagnostics",
+    "tags": [
+        "observability",
+        "jfr",
+        "diagnostics",
+        "tui"
+    ],
+    "bundled": true,
+    "level": "beginner"
+}

Reply via email to