From 72d12283342de4f09858e9094611d930c1fa3b89 Mon Sep 17 00:00:00 2001 From: floriankirmaier Date: Tue, 5 May 2020 22:38:31 +0200 Subject: [PATCH] JDK-8244297 Added some javadoc to the testutility Some methods/fields are now private --- .../java/test/util/memory/JMemoryBuddy.java | 60 +++++++++++++++---- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/modules/javafx.base/src/test/java/test/util/memory/JMemoryBuddy.java b/modules/javafx.base/src/test/java/test/util/memory/JMemoryBuddy.java index bd436ce5676..01fdf819c28 100644 --- a/modules/javafx.base/src/test/java/test/util/memory/JMemoryBuddy.java +++ b/modules/javafx.base/src/test/java/test/util/memory/JMemoryBuddy.java @@ -39,14 +39,13 @@ public class JMemoryBuddy { - static int steps = 10; - static int overallTime = 1000; - static int sleepTime = overallTime / steps; - static boolean createHeapdump = false; - static int garbageAmount = 999999; + private static int steps = 10; + private static int overallTime = 1000; + private static int sleepTime = overallTime / steps; + private static boolean createHeapdump = false; + private static int garbageAmount = 999999; private static String MX_BEAN_PROXY_TYPE = "com.sun.management:type=HotSpotDiagnostic"; - - static String outputFolderString = "."; + private static String outputFolderString = "."; static { outputFolderString = System.getProperty("jmemorybuddy.output","."); @@ -56,7 +55,7 @@ public class JMemoryBuddy { garbageAmount = Integer.parseInt(System.getProperty("jmemorybuddy.garbageAmount", "10")); } - public static void createGarbage() { + private static void createGarbage() { LinkedList list = new LinkedList(); int counter = 0; while(counter < garbageAmount) { @@ -65,6 +64,11 @@ public static void createGarbage() { } } + /** + * Checks whethr the content of the WeakReference can be collected. + * @param weakReference + * @return It throws an excpetion when the weakReference was not collectable. + */ public static void assertCollectable(WeakReference weakReference) { if(!checkCollectable(weakReference)) { AssertCollectable assertCollectable = new AssertCollectable(weakReference); @@ -73,6 +77,11 @@ public static void assertCollectable(WeakReference weakReference) { } } + /** + * Checks whethr the content of the WeakReference can be collected. + * @param weakReference + * @return Returns true, when the provided WeakReference can be collected. + */ public static boolean checkCollectable(WeakReference weakReference) { return checkCollectable(steps, weakReference) > 0; } @@ -104,17 +113,33 @@ private static int checkCollectable(int stepsLeft, WeakReference weakReference) return counter; } + /** + * Checks whethr the content of the WeakReference can not be collected. + * @param weakReference + * @return It throws an excpetion when the weakReference was collectable. + */ public static void assertNotCollectable(WeakReference weakReference) { if(!checkNotCollectable(weakReference)) { throw new AssertionError("Content of WeakReference was collected!"); } } + + /** + * Checks whethr the content of the WeakReference can not be collected. + * @param weakReference + * @return Returns true, when the provided WeakReference can be collected. + */ public static boolean checkNotCollectable(WeakReference weakReference) { createGarbage(); System.gc(); return weakReference.get() != null; } + /** + * A standard method to define a test which checks code for specific memory semantic. + * The parameter of the lambda provides an API to define the required memory semantic. + * @param f + */ public static void memoryTest(Consumer f) { LinkedList toBeCollected = new LinkedList(); LinkedList toBeNotCollected = new LinkedList(); @@ -176,7 +201,7 @@ public void setAsReferenced(Object ref) { } - public static void createHeapDump() { + private static void createHeapDump() { if(createHeapdump) { try { String dateString = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()); @@ -206,8 +231,21 @@ private static HotSpotDiagnosticMXBean getHotspotMBean() throws IOException { } public static interface MemoryTestAPI { + /** + * After executing the lambda, the provided ref must be collectable. Otherwise an Exception is thrown. + * @param ref + */ public void assertCollectable(Object ref); + /** + * After executing the lambda, the provided ref must be not collectable. Otherwise an Exception is thrown. + * @param ref + */ public void assertNotCollectable(Object ref); + + /** + * The provided reference won't be collected, until memoryTest finishes all it's tests. + * @param ref + */ public void setAsReferenced(Object ref); } @@ -229,7 +267,7 @@ public String toString() { } } - static class AssertNotCollectable { + private static class AssertNotCollectable { WeakReference assertNotCollectable; String originalResultOfToString; @@ -248,7 +286,7 @@ public String toString() { } } - static class SetAsReferenced { + private static class SetAsReferenced { Object setAsReferenced; SetAsReferenced(Object ref) {