Skip to content

Commit

Permalink
HHH-19047 Prevent GC nepotism from affecting StandardStack
Browse files Browse the repository at this point in the history
  • Loading branch information
mbladel committed Jan 17, 2025
1 parent 88d689e commit d71a2e5
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @author Sanne Grinovero
* @author Marco Belladelli
*/
@SuppressWarnings("unchecked")
public final class StandardStack<T> implements Stack<T> {

private Object[] elements;
Expand Down Expand Up @@ -107,8 +108,8 @@ public boolean isEmpty() {

@Override
public void clear() {
for ( int i = 0; i < top; i++ ) {
elements[i] = null;
if ( elements != null ) {
Arrays.fill( elements, 0, top, null );
}
top = 0;
}
Expand Down Expand Up @@ -145,7 +146,10 @@ public <X, Y> X findCurrentFirstWithParameter(Y parameter, BiFunction<T, Y, X> b
private void grow() {
final int oldCapacity = elements.length;
final int jump = ( oldCapacity < 64 ) ? ( oldCapacity + 2 ) : ( oldCapacity >> 1 );
elements = Arrays.copyOf( elements, oldCapacity + jump );
final Object[] newElements = Arrays.copyOf( elements, oldCapacity + jump );
// Prevents GC nepotism on the old array elements, see HHH-19047
Arrays.fill( elements, null );
elements = newElements;
}

}

0 comments on commit d71a2e5

Please sign in to comment.