@@ -53,6 +53,8 @@ public class ReferencesGC extends ThreadedGCTest {
53
53
54
54
static int RANGE = 256 ;
55
55
static float RATIO = (float ) 1.0 ;
56
+ static int REMOVE ; // Initialized in parseArgs.
57
+ static int RETAIN ; // Initialized in parseArgs.
56
58
57
59
public static void main (String [] args ) {
58
60
parseArgs (args );
@@ -67,6 +69,8 @@ public static void parseArgs(String[] args) {
67
69
RATIO = new Float (args [++i ]).floatValue ();
68
70
}
69
71
}
72
+ REMOVE = (int ) (RANGE * RATIO );
73
+ RETAIN = RANGE - REMOVE ;
70
74
}
71
75
72
76
private class Worker implements Runnable {
@@ -76,13 +80,13 @@ private class Worker implements Runnable {
76
80
static final int PHANTOM = 2 ;
77
81
private ExecutionController stresser ;
78
82
int finalizationMaxTime = 1000 * 60 * runParams .getNumberOfThreads ();
79
- int [] alive = new int [3 ];
80
- int [] enqued = new int [3 ];
83
+ ReferenceQueue refq = null ; // Reinitialized each time through loop
84
+ int [] alive = null ; // Reinitialized each time through loop
85
+ int [] wrong = null ; // Reinitialized each time through loop
81
86
CircularLinkedList holder [] = new CircularLinkedList [RANGE ];
82
87
WeakReference wr [] = new WeakReference [RANGE ];
83
88
SoftReference sr [] = new SoftReference [RANGE ];
84
89
PhantomReference phr [] = new PhantomReference [RANGE ];
85
- ReferenceQueue refq = new ReferenceQueue ();
86
90
GarbageProducer gp = GarbageUtils .getArrayProducers ().get (0 );
87
91
int iter = 0 ;
88
92
@@ -93,11 +97,11 @@ public void run() {
93
97
}
94
98
95
99
while (stresser .continueExecution ()) {
96
- int totalQ = 0 ;
100
+ int totalLive = 0 ;
97
101
try {
98
102
refq = new ReferenceQueue ();
99
103
alive = new int [3 ];
100
- enqued = new int [3 ];
104
+ wrong = new int [3 ];
101
105
for (int j = 0 ; j < RANGE ; j ++) {
102
106
holder [j ] = new CircularLinkedList ();
103
107
holder [j ].addNelements (300 );
@@ -112,21 +116,21 @@ public void run() {
112
116
}
113
117
114
118
for (int i = 0 ; i < RANGE ; i ++) {
115
- if (wr [i ].isEnqueued ( )) {
116
- ++totalQ ;
119
+ if (wr [i ].refersTo ( holder [ i ] )) {
120
+ ++totalLive ;
117
121
}
118
- if (sr [i ].isEnqueued ( )) {
119
- ++totalQ ;
122
+ if (sr [i ].refersTo ( holder [ i ] )) {
123
+ ++totalLive ;
120
124
}
121
- if (phr [i ].isEnqueued ( )) {
122
- ++totalQ ;
125
+ if (phr [i ].refersTo ( holder [ i ] )) {
126
+ ++totalLive ;
123
127
}
124
128
}
125
- if (totalQ != 0 ) {
126
- throw new TestFailure ("There are " + totalQ + " references in the queue instead 0 before null-assigment." );
129
+ if (totalLive != 3 * RANGE ) {
130
+ throw new TestFailure ("There are " + ( 3 * RANGE - totalLive ) + " references cleared before null-assigment." );
127
131
}
128
132
129
- for (int i = 0 ; i < ( int ) ( RANGE * RATIO ) ; i ++) {
133
+ for (int i = 0 ; i < REMOVE ; i ++) {
130
134
holder [i ] = null ;
131
135
}
132
136
@@ -137,69 +141,57 @@ public void run() {
137
141
// At this point OOME was thrown and accordingly to spec
138
142
// all weak refs should be processed
139
143
140
- alive = new int [3 ];
141
- enqued = new int [3 ];
142
- for (int i = 0 ; i < RANGE ; i ++) {
143
- if (wr [i ].get () != null ) {
144
- ++alive [WEAK ];
145
- }
146
- if (wr [i ].isEnqueued ()) {
147
- ++enqued [WEAK ];
148
- }
149
- if (sr [i ].get () != null ) {
150
- ++alive [SOFT ];
151
- }
152
- if (sr [i ].isEnqueued ()) {
153
- ++enqued [SOFT ];
154
- }
155
- if (phr [i ].isEnqueued ()) {
156
- ++enqued [PHANTOM ];
157
- }
158
- }
159
-
160
144
long waitTime = System .currentTimeMillis () + finalizationMaxTime ;
161
- while (totalQ < (RANGE * RATIO * 3 * 0.9 ) && (System .currentTimeMillis () < waitTime )) {
162
- alive = new int [3 ];
163
- enqued = new int [3 ];
145
+ int totalQ = 0 ;
146
+ while ((totalQ < (3 * REMOVE )) && (System .currentTimeMillis () < waitTime )) {
147
+ alive [WEAK ] = alive [SOFT ] = alive [PHANTOM ] = 0 ;
148
+ wrong [WEAK ] = wrong [SOFT ] = wrong [PHANTOM ] = 0 ;
164
149
for (int i = 0 ; i < RANGE ; i ++) {
165
- if (wr [i ].get () != null ) {
150
+ if (!wr [i ].refersTo (holder [i ])) {
151
+ ++wrong [WEAK ];
152
+ } else if (holder [i ] != null ) {
166
153
++alive [WEAK ];
167
154
}
168
- if ( wr [ i ]. isEnqueued ()) {
169
- ++ enqued [ WEAK ];
170
- }
171
- if (sr [i ]. get () != null ) {
155
+
156
+ if (! sr [ i ]. refersTo ( holder [ i ])) {
157
+ ++ wrong [ SOFT ];
158
+ } else if (holder [i ] != null ) {
172
159
++alive [SOFT ];
173
160
}
174
- if (sr [i ].isEnqueued ()) {
175
- ++enqued [SOFT ];
161
+
162
+ if (!phr [i ].refersTo (holder [i ])) {
163
+ ++wrong [PHANTOM ];
164
+ } else if (holder [i ] != null ) {
165
+ ++alive [PHANTOM ];
176
166
}
177
- if (phr [i ].isEnqueued ()) {
178
- ++enqued [PHANTOM ];
167
+ }
168
+
169
+ try {
170
+ while (refq .remove (100 ) != null ) {
171
+ ++totalQ ;
179
172
}
173
+ } catch (InterruptedException ie ) {
180
174
}
181
- totalQ = (enqued [WEAK ] + enqued [SOFT ] + enqued [PHANTOM ]);
182
- if (totalQ < (int ) (3 * RANGE * RATIO * 0.9 )) {
183
- log .debug ("After null-assignment to " + (int ) (RANGE * RATIO ) +
184
- //" elements from " + lower + " to " + (upper - 1) +
185
- " and provoking gc found:\n \t " +
186
- enqued [WEAK ] + " weak\n \t " +
187
- enqued [SOFT ] + " soft\n \t " +
188
- enqued [PHANTOM ] + " phantom " +
189
- " queuened refs and \n \t " +
190
- alive [WEAK ] + " weak\n \t " +
191
- alive [SOFT ] + " soft\n \t " +
192
- "alive refs." );
175
+ if (totalQ < (3 * REMOVE )) {
176
+ log .debug ("After null-assignment to " + REMOVE +
177
+ " referent values and provoking gc found:\n \t " +
178
+ totalQ + " queued refs." );
193
179
try {
194
- log .debug ("sleeping to give gc one more chance ... ..." );
180
+ log .debug ("sleeping to give reference processing more time ..." );
195
181
Thread .sleep (1000 );
196
182
} catch (InterruptedException ie ) {
197
183
}
198
184
}
199
185
}
200
186
log .debug ("iteration.... " + iter ++);
201
- if (totalQ < (int ) (3 * RANGE * RATIO * 0.9 ) || totalQ > (int ) (3 * RANGE * RATIO )) {
202
- throw new TestFailure ("Test failed" );
187
+ if (wrong [WEAK ] != 0 ) {
188
+ throw new TestFailure ("Expected " + RETAIN + " weak references still alive: " + alive [WEAK ]);
189
+ } else if (wrong [SOFT ] != 0 ) {
190
+ throw new TestFailure ("Expected " + RETAIN + " soft references still alive: " + alive [SOFT ]);
191
+ } else if (wrong [PHANTOM ] != 0 ) {
192
+ throw new TestFailure ("Expected " + RETAIN + " phantom references still alive: " + alive [PHANTOM ]);
193
+ } else if (totalQ != (3 * REMOVE )) {
194
+ throw new TestFailure ("Expected " + (3 * REMOVE ) + " references enqueued: " + totalQ );
203
195
}
204
196
}
205
197
}
0 commit comments