From 76c554a0f8fac92e00e4a887808201eb48c5c905 Mon Sep 17 00:00:00 2001 From: Vladimir Plizga Date: Mon, 29 Aug 2022 05:47:55 +0000 Subject: [PATCH] 7903262: JOL: GraphStatsWalker counts array elements incorrectly Reviewed-by: shade --- .../org/openjdk/jol/info/GraphStatsWalker.java | 1 + .../org/openjdk/jol/info/GraphStatsTest.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/jol-core/src/main/java/org/openjdk/jol/info/GraphStatsWalker.java b/jol-core/src/main/java/org/openjdk/jol/info/GraphStatsWalker.java index 90c3d41c..3a91767d 100644 --- a/jol-core/src/main/java/org/openjdk/jol/info/GraphStatsWalker.java +++ b/jol-core/src/main/java/org/openjdk/jol/info/GraphStatsWalker.java @@ -67,6 +67,7 @@ public GraphStats walk(Object... roots) { for (Object e : (Object[]) o) { if (e != null && visited.add(e)) { + data.addRecord(vm.sizeOf(e)); s.push(e); } } diff --git a/jol-core/src/test/java/org/openjdk/jol/info/GraphStatsTest.java b/jol-core/src/test/java/org/openjdk/jol/info/GraphStatsTest.java index c87e5ff8..5de4cc72 100644 --- a/jol-core/src/test/java/org/openjdk/jol/info/GraphStatsTest.java +++ b/jol-core/src/test/java/org/openjdk/jol/info/GraphStatsTest.java @@ -23,6 +23,14 @@ static class D { public D(D d) { this.d = d; } } + static class E { + private final A[] as; + + E() { + as = new A[] { new A() }; + } + } + @Test public void basicCounts() { A a = new A(); @@ -85,4 +93,14 @@ public void compoundSizes() { } } + @Test + public void layoutAndStatsMatch() { + E e = new E(); + + GraphLayout layout = GraphLayout.parseInstance(e); + GraphStats stats = GraphStats.parseInstance(e); + + Assert.assertEquals(layout.totalSize(), stats.totalSize()); + Assert.assertEquals(layout.totalCount(), stats.totalCount()); + } }