Skip to content

Commit d71a2e5

Browse files
committed
HHH-19047 Prevent GC nepotism from affecting StandardStack
1 parent 88d689e commit d71a2e5

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

hibernate-core/src/main/java/org/hibernate/internal/util/collections/StandardStack.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @author Sanne Grinovero
2020
* @author Marco Belladelli
2121
*/
22+
@SuppressWarnings("unchecked")
2223
public final class StandardStack<T> implements Stack<T> {
2324

2425
private Object[] elements;
@@ -107,8 +108,8 @@ public boolean isEmpty() {
107108

108109
@Override
109110
public void clear() {
110-
for ( int i = 0; i < top; i++ ) {
111-
elements[i] = null;
111+
if ( elements != null ) {
112+
Arrays.fill( elements, 0, top, null );
112113
}
113114
top = 0;
114115
}
@@ -145,7 +146,10 @@ public <X, Y> X findCurrentFirstWithParameter(Y parameter, BiFunction<T, Y, X> b
145146
private void grow() {
146147
final int oldCapacity = elements.length;
147148
final int jump = ( oldCapacity < 64 ) ? ( oldCapacity + 2 ) : ( oldCapacity >> 1 );
148-
elements = Arrays.copyOf( elements, oldCapacity + jump );
149+
final Object[] newElements = Arrays.copyOf( elements, oldCapacity + jump );
150+
// Prevents GC nepotism on the old array elements, see HHH-19047
151+
Arrays.fill( elements, null );
152+
elements = newElements;
149153
}
150154

151155
}

0 commit comments

Comments
 (0)