Question from the Java test

Find the memory leak in the following code.

Expert

Can you spot the memory leak in the following code ?

public class Test {
    private Object[] elements;
    private int size = 0;
    public static final int DEFAULT_INITIAL_CAPACITY = 16;

    public Test() {
        elements = new Object[DEFAULT_INITIAL_CAPACITY];
    }

    public void push(Object e) {
        ensureCapacity();
        elements[size++] = e;
    }

    public Object pop() {
        if (size == 0) {
            throw new EmptyStackException();
        }
        return elements[size--];
    }

    private void ensureCapacity() {
        if (elements.length == size) {
            elements = Arrays.copyOf(elements, 2 * size + 1);
        }
    }
}
Author: Joffrey BonifayStatus: PublishedQuestion passed 67 times
Edit
0
Community EvaluationsNo one has reviewed this question yet, be the first!