# HG changeset patch # User shade # Date 1534752240 -7200 # Node ID cdffba164671a2620efc3e2eeccc4135a70c71b8 # Parent 703576fdd268138047de557f47af4ba08d4a1bae 8209573: [TESTBUG] gc/epsilon/TestMemoryMXBeans should retry on failure Reviewed-by: kvn, pliden diff -r 703576fdd268 -r cdffba164671 test/hotspot/jtreg/gc/epsilon/TestMemoryMXBeans.java --- a/test/hotspot/jtreg/gc/epsilon/TestMemoryMXBeans.java Sun Aug 19 22:41:49 2018 -0700 +++ b/test/hotspot/jtreg/gc/epsilon/TestMemoryMXBeans.java Mon Aug 20 10:04:00 2018 +0200 @@ -67,28 +67,37 @@ } } - public static void testAllocs() { + public static void testAllocs() throws Exception { MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); - // Do lazy inits first: - long heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed(); - sink = new int[1024*1024]; - long heapUsed2 = memoryMXBean.getHeapMemoryUsage().getUsed(); + // Try multiple times, to capture either APIs we call allocate lazily, or the background threads allocating + int maxTries = 10; + int tries = 0; - // Compute how much we waste during the calls themselves: - heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed(); - heapUsed2 = memoryMXBean.getHeapMemoryUsage().getUsed(); - long adj = heapUsed2 - heapUsed1; + while (true) { + // Compute how much we waste during the calls themselves: + long heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed(); + long heapUsed2 = memoryMXBean.getHeapMemoryUsage().getUsed(); + long adj = heapUsed2 - heapUsed1; + + heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed(); + sink = new int[1024*1024]; + heapUsed2 = memoryMXBean.getHeapMemoryUsage().getUsed(); - heapUsed1 = memoryMXBean.getHeapMemoryUsage().getUsed(); - sink = new int[1024*1024]; - heapUsed2 = memoryMXBean.getHeapMemoryUsage().getUsed(); + long diff = heapUsed2 - heapUsed1 - adj; + long min = 8 + 4*1024*1024; + long max = 16 + 4*1024*1024; + if ((min <= diff && diff <= max)) { + // Success + return; + } - long diff = heapUsed2 - heapUsed1 - adj; - long min = 8 + 4*1024*1024; - long max = 16 + 4*1024*1024; - if (!(min <= diff && diff <= max)) { - throw new IllegalStateException("Allocation did not change used space right: " + diff + " should be in [" + min + ", " + max + "]"); + if (tries++ > maxTries) { + throw new IllegalStateException("Allocation did not change used space right: " + diff + " should be in [" + min + ", " + max + "]"); + } + + // Wait and try again + Thread.sleep(1000); } }