1 /*
   2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.util.logging.*;
  25 
  26 public class LoggerWeakRefLeak {
  27     // AnonLoggerWeakRefLeak checks for one weak reference leak.
  28     // LoggerWeakRefLeak checks for two weak reference leaks so
  29     // this test runs twice as long, by default.
  30     public static int DEFAULT_LOOP_TIME = 120;  // time is in seconds
  31 
  32     public static void main(String[] args) {
  33         int loop_time = 0;
  34         int max_loop_time = DEFAULT_LOOP_TIME;
  35 
  36         if (args.length == 0) {
  37             System.out.println("INFO: using default time of "
  38                 + max_loop_time + " seconds.");
  39         } else {
  40             try {
  41                 max_loop_time = Integer.parseInt(args[0]);
  42             } catch (NumberFormatException nfe) {
  43                 System.err.println("Error: '" + args[0]
  44                     + "': is not a valid seconds value.");
  45                 System.err.println("Usage: LoggerWeakRefLeak [seconds]");
  46                 System.exit(1);
  47             }
  48         }
  49 
  50         long count = 0;
  51         int  loggerCount = 0;
  52         long now = 0;
  53         long startTime = System.currentTimeMillis();
  54 
  55         while (now < (startTime + (max_loop_time * 1000))) {
  56             if ((count % 1000) == 0) {
  57                 // Print initial call count to let caller know that
  58                 // we're up and running and then periodically
  59                 System.out.println("INFO: call count = " + count);
  60             }
  61 
  62             for (int i = 0; i < 100; i++) {
  63                 // This Logger call is leaking two different WeakReferences:
  64                 // - one in LogManager.LogNode
  65                 // - one in Logger.kids
  66                 java.util.logging.Logger.getLogger("logger-" + loggerCount);
  67                 count++;
  68                 if (++loggerCount >= 25000) {
  69                     // Limit the Logger namespace used by the test so
  70                     // the weak refs in LogManager.loggers that are
  71                     // being properly managed don't skew the counts
  72                     // by too much.
  73                     loggerCount = 0;
  74                 }
  75             }
  76 
  77             try {
  78                 // delay for 1/10 of a second to avoid CPU saturation
  79                 Thread.sleep(100);
  80             } catch (InterruptedException ie) {
  81                 // ignore any exceptions
  82             }
  83 
  84             now = System.currentTimeMillis();
  85         }
  86 
  87         System.out.println("INFO: final loop count = " + count);
  88     }
  89 }