test/java/util/logging/AnonLoggerWeakRefLeak.java

Print this page




   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 AnonLoggerWeakRefLeak {
  27     public static int DEFAULT_LOOP_TIME = 60;  // time is in seconds








  28 
  29     public static void main(String[] args) {

  30         int loop_time = 0;
  31         int max_loop_time = DEFAULT_LOOP_TIME;
  32 
  33         if (args.length == 0) {

  34             System.out.println("INFO: using default time of "
  35                 + max_loop_time + " seconds.");
  36         } else {
  37             try {
  38                 max_loop_time = Integer.parseInt(args[0]);
  39             } catch (NumberFormatException nfe) {
  40                 System.err.println("Error: '" + args[0]
  41                     + "': is not a valid seconds value.");
  42                 System.err.println("Usage: AnonLoggerWeakRefLeak [seconds]");
  43                 System.exit(1);
  44             }
  45         }
  46 
  47         long count = 0;
  48         long now = 0;
  49         long startTime = System.currentTimeMillis();
  50 
  51         while (now < (startTime + (max_loop_time * 1000))) {
  52             if ((count % 1000) == 0) {
  53                 // Print initial call count to let caller know that
  54                 // we're up and running and then periodically
  55                 System.out.println("INFO: call count = " + count);
  56             }
  57 
  58             for (int i = 0; i < 100; i++) {
  59                 // this Logger call is leaking a WeakReference in Logger.kids
  60                 java.util.logging.Logger.getAnonymousLogger();
  61                 count++;
  62             }
  63 
  64             try {
  65                 // delay for 1/10 of a second to avoid CPU saturation
  66                 Thread.sleep(100);
  67             } catch (InterruptedException ie) {
  68                 // ignore any exceptions
  69             }
  70 
  71             now = System.currentTimeMillis();
  72         }
  73 
  74         System.out.println("INFO: final loop count = " + count);
  75     }








  76 }


   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 AnonLoggerWeakRefLeak extends SimpleApplication {
  27     // The test driver script will allow this program to run until we
  28     // reach DEFAULT_LOOP_TIME or a decrease in instance counts is
  29     // observed. For this particular WeakReference leak, the count
  30     // was always observed to be increasing so if we get a decreasing
  31     // count, then the leak is fixed in the bits being tested.
  32     // Two minutes has been enough time to observe a decrease in
  33     // fixed bits on overloaded systems, but the test will likely
  34     // finish more quickly.
  35     public static int DEFAULT_LOOP_TIME = 120;  // time is in seconds
  36 
  37     // execute the AnonLoggerWeakRefLeak app work
  38     public void doMyAppWork(String[] args) throws Exception {
  39         int loop_time = 0;
  40         int max_loop_time = DEFAULT_LOOP_TIME;
  41 
  42         // args[0] is the port-file
  43         if (args.length < 2) {
  44             System.out.println("INFO: using default time of "
  45                 + max_loop_time + " seconds.");
  46         } else {
  47             try {
  48                 max_loop_time = Integer.parseInt(args[1]);
  49             } catch (NumberFormatException nfe) {
  50                 throw new RuntimeException("Error: '" + args[1]
  51                     + "': is not a valid seconds value.");


  52             }
  53         }
  54 
  55         long count = 0;
  56         long now = 0;
  57         long startTime = System.currentTimeMillis();
  58 
  59         while (now < (startTime + (max_loop_time * 1000))) {
  60             if ((count % 1000) == 0) {
  61                 // Print initial call count to let caller know that
  62                 // we're up and running and then periodically
  63                 System.out.println("INFO: call count = " + count);
  64             }
  65 
  66             for (int i = 0; i < 100; i++) {
  67                 // this Logger call is leaking a WeakReference in Logger.kids
  68                 java.util.logging.Logger.getAnonymousLogger();
  69                 count++;
  70             }
  71 
  72             try {
  73                 // delay for 1/10 of a second to avoid CPU saturation
  74                 Thread.sleep(100);
  75             } catch (InterruptedException ie) {
  76                 // ignore any exceptions
  77             }
  78 
  79             now = System.currentTimeMillis();
  80         }
  81 
  82         System.out.println("INFO: final loop count = " + count);
  83     }
  84 
  85     public static void main(String[] args) throws Exception {
  86         AnonLoggerWeakRefLeak myApp = new AnonLoggerWeakRefLeak();
  87 
  88         SimpleApplication.setMyApp(myApp);
  89 
  90         SimpleApplication.main(args);
  91     }
  92 }