|
39 | 39 | import java.nio.file.Path;
|
40 | 40 | import java.nio.file.Paths;
|
41 | 41 | import java.util.ArrayList;
|
| 42 | +import java.util.HashSet; |
| 43 | +import java.util.List; |
| 44 | +import java.util.Set; |
42 | 45 | import java.util.stream.Stream;
|
43 | 46 |
|
44 | 47 | public class ClassUnloadCommon {
|
@@ -71,6 +74,41 @@ public static void triggerUnloading() {
|
71 | 74 | wb.fullGC(); // will do class unloading
|
72 | 75 | }
|
73 | 76 |
|
| 77 | + /** |
| 78 | + * Calls triggerUnloading() in a retry loop for 2 seconds or until WhiteBox.isClassAlive |
| 79 | + * determines that no classes named in classNames are alive. |
| 80 | + * |
| 81 | + * This variant of triggerUnloading() accommodates the inherent raciness |
| 82 | + * of class unloading. For example, it's possible for a JIT compilation to hold |
| 83 | + * strong roots to types (e.g. in virtual call or instanceof profiles) that |
| 84 | + * are not released or converted to weak roots until the compilation completes. |
| 85 | + * |
| 86 | + * @param classNames the set of classes that are expected to be unloaded |
| 87 | + * @return the set of classes that have not been unloaded after exiting the retry loop |
| 88 | + */ |
| 89 | + public static Set<String> triggerUnloading(List<String> classNames) { |
| 90 | + WhiteBox wb = WhiteBox.getWhiteBox(); |
| 91 | + Set<String> aliveClasses = new HashSet<>(classNames); |
| 92 | + int attempt = 0; |
| 93 | + while (!aliveClasses.isEmpty() && attempt < 20) { |
| 94 | + ClassUnloadCommon.triggerUnloading(); |
| 95 | + for (String className : classNames) { |
| 96 | + if (aliveClasses.contains(className)) { |
| 97 | + if (wb.isClassAlive(className)) { |
| 98 | + try { |
| 99 | + Thread.sleep(100); |
| 100 | + } catch (InterruptedException ex) { |
| 101 | + } |
| 102 | + } else { |
| 103 | + aliveClasses.remove(className); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + attempt++; |
| 108 | + } |
| 109 | + return aliveClasses; |
| 110 | + } |
| 111 | + |
74 | 112 | /**
|
75 | 113 | * Creates a class loader that loads classes from {@code ${test.class.path}}
|
76 | 114 | * before delegating to the system class loader.
|
|
0 commit comments