author | jlahoda |
Wed, 12 Dec 2012 20:26:56 +0100 | |
changeset 14803 | 88347e495d34 |
parent 6120 | 4979c5d548f8 |
permissions | -rw-r--r-- |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
1 |
/* |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
2 |
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
4 |
* |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
8 |
* |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
13 |
* accompanied this code). |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
14 |
* |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
18 |
* |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
21 |
* questions. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
22 |
*/ |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
23 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
24 |
import java.util.logging.*; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
25 |
|
6120
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
26 |
public class LoggerWeakRefLeak extends SimpleApplication { |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
27 |
// The test driver script will allow this program to run until we |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
28 |
// reach DEFAULT_LOOP_TIME or a decrease in instance counts is |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
29 |
// observed. For these particular WeakReference leaks, the count |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
30 |
// was always observed to be increasing so if we get a decreasing |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
31 |
// count, then the leaks are fixed in the bits being tested. |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
32 |
// Two minutes has been enough time to observe a decrease in |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
33 |
// fixed bits on overloaded systems, but the test will likely |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
34 |
// finish more quickly. |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
35 |
public static int DEFAULT_LOOP_TIME = 120; // time is in seconds |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
36 |
|
6120
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
37 |
// execute the LoggerWeakRefLeak app work |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
38 |
public void doMyAppWork(String[] args) throws Exception { |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
39 |
int loop_time = 0; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
40 |
int max_loop_time = DEFAULT_LOOP_TIME; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
41 |
|
6120
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
42 |
// args[0] is the port-file |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
43 |
if (args.length < 2) { |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
44 |
System.out.println("INFO: using default time of " |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
45 |
+ max_loop_time + " seconds."); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
46 |
} else { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
47 |
try { |
6120
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
48 |
max_loop_time = Integer.parseInt(args[1]); |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
49 |
} catch (NumberFormatException nfe) { |
6120
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
50 |
throw new RuntimeException("Error: '" + args[1] |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
51 |
+ "': is not a valid seconds value."); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
52 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
53 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
54 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
55 |
long count = 0; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
56 |
int loggerCount = 0; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
57 |
long now = 0; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
58 |
long startTime = System.currentTimeMillis(); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
59 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
60 |
while (now < (startTime + (max_loop_time * 1000))) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
61 |
if ((count % 1000) == 0) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
62 |
// Print initial call count to let caller know that |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
63 |
// we're up and running and then periodically |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
64 |
System.out.println("INFO: call count = " + count); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
65 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
66 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
67 |
for (int i = 0; i < 100; i++) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
68 |
// This Logger call is leaking two different WeakReferences: |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
69 |
// - one in LogManager.LogNode |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
70 |
// - one in Logger.kids |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
71 |
java.util.logging.Logger.getLogger("logger-" + loggerCount); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
72 |
count++; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
73 |
if (++loggerCount >= 25000) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
74 |
// Limit the Logger namespace used by the test so |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
75 |
// the weak refs in LogManager.loggers that are |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
76 |
// being properly managed don't skew the counts |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
77 |
// by too much. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
78 |
loggerCount = 0; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
79 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
80 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
81 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
82 |
try { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
83 |
// delay for 1/10 of a second to avoid CPU saturation |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
84 |
Thread.sleep(100); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
85 |
} catch (InterruptedException ie) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
86 |
// ignore any exceptions |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
87 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
88 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
89 |
now = System.currentTimeMillis(); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
90 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
91 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
92 |
System.out.println("INFO: final loop count = " + count); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
93 |
} |
6120
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
94 |
|
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
95 |
public static void main(String[] args) throws Exception { |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
96 |
AnonLoggerWeakRefLeak myApp = new AnonLoggerWeakRefLeak(); |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
97 |
|
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
98 |
SimpleApplication.setMyApp(myApp); |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
99 |
|
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
100 |
SimpleApplication.main(args); |
4979c5d548f8
6964018: 3/4 AnonLoggerWeakRefLeak and LoggerWeakRefLeak can fail in JPRT
dcubed
parents:
5964
diff
changeset
|
101 |
} |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
diff
changeset
|
102 |
} |