< prev index next >

test/hotspot/jtreg/testlibrary/ctw/src/sun/hotspot/tools/ctw/Compiler.java

Print this page
*** 39,13 ***
   * Provide method to compile whole class.
   * Also contains compiled methods and classes counters.
   */
  public class Compiler {
  
      private static final Unsafe UNSAFE = Unsafe.getUnsafe();
      private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
!     private static final AtomicLong METHOD_COUNT = new AtomicLong(0L);
  
      private Compiler() { }
  
      /**
       * @return count of processed methods
--- 39,19 ---
   * Provide method to compile whole class.
   * Also contains compiled methods and classes counters.
   */
  public class Compiler {
  
+     // Call GC after compiling as many methods. This would remove the stale methods.
+     // This threshold should balance the GC overhead and the cost of keeping lots
+     // of stale methods around.
+     private static final long GC_METHOD_THRESHOLD = Long.getLong("gcMethodThreshold", 100);
+ 
      private static final Unsafe UNSAFE = Unsafe.getUnsafe();
      private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
!     private static final AtomicLong METHOD_COUNT = new AtomicLong();
+     private static final AtomicLong METHODS_SINCE_LAST_GC = new AtomicLong();
  
      private Compiler() { }
  
      /**
       * @return count of processed methods

*** 81,10 ***
--- 87,25 ---
          for (Executable e : aClass.getDeclaredMethods()) {
              ++methodCount;
              executor.execute(new CompileMethodCommand(id, e));
          }
          METHOD_COUNT.addAndGet(methodCount);
+ 
+         // See if we need to schedule a GC
+         while (true) {
+             long current = METHODS_SINCE_LAST_GC.get();
+             long update = current + methodCount;
+             if (update >= GC_METHOD_THRESHOLD) {
+                 update = 0;
+             }
+             if (METHODS_SINCE_LAST_GC.compareAndSet(current, update)) {
+                 if (update == 0) {
+                     executor.execute(() -> System.gc());
+                 }
+                 break;
+             }
+         }
      }
  
      private static void preloadClasses(String className, long id,
              ConstantPool constantPool) {
          try {
< prev index next >