kezhuw commented on a change in pull request #15039: URL: https://github.com/apache/flink/pull/15039#discussion_r587562315
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/metrics/util/MetricUtilsTest.java ########## @@ -175,24 +183,20 @@ public MetricGroup addGroup(String name) { @SuppressWarnings("unchecked") final Gauge<Long> used = (Gauge<Long>) metaspaceMetrics.get(MetricNames.MEMORY_USED); - final long usedMetaspaceInitially = used.getValue(); + runUntilMetricChanged("Metaspace", 10, () -> redefineAsNewClass(MetricUtils.class), used); + } - // check memory usage difference multiple times since other tests may affect memory usage as - // well - for (int x = 0; x < 10; x++) { - List<Runnable> consumerList = new ArrayList<>(); - for (int i = 0; i < 10; i++) { - consumerList.add(() -> {}); - } + @Test + public void testNonHeapMetricUsageNotStatic() throws Exception { + final InterceptingOperatorMetricGroup nonHeapMetrics = + new InterceptingOperatorMetricGroup(); - final long usedMetaspaceAfterAllocation = used.getValue(); + MetricUtils.instantiateNonHeapMemoryMetrics(nonHeapMetrics); - if (usedMetaspaceInitially != usedMetaspaceAfterAllocation) { - return; - } - Thread.sleep(50); - } - Assert.fail("Metaspace usage metric never changed it's value."); + @SuppressWarnings("unchecked") + final Gauge<Long> used = (Gauge<Long>) nonHeapMetrics.get(MetricNames.MEMORY_USED); + + runUntilMetricChanged("Non-heap", 10, () -> redefineAsNewClass(MetricUtils.class), used); Review comment: Hi @zentol, I runs following paths: * With `referencedObjects` and new anonymous class, it does not last 2 runs in my IDEA. * Without `referencedObjects` and new anonymous class, same as above. * With `referencedObjects` and `testNonHeapMetricUsageNotStatic`, 100K runs without failure. * Without `referencedObjects` and `testNonHeapMetricUsageNotStatic`, 100K runs without failure. `referencedObjects` does not contribute to result for both approaches. I keep it mainly for mental health that the created object should live before checking. Before `referencedObjects` approach, I actually use a simple assertion statement for this as an replacement of Java 9 `Reference.reachabilityFence`. Here is code snippet for anonymous class: ```java runUntilMetricChanged( "Non-heap", 10, () -> new Runnable() { @Override public void run() { System.out.println(used); } }, used); ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org