0

I read some material about Java memory leak. It implements FIFO Queue with array to semantically leak memory. But I don't understand why it will cause memory leak. Is it because it didn't nullify the unused slot in the 'pop' operation? Anyone can explain to me?

queue[head] = null

The FIFO Queue implementation is as follows:

public class FIFOQueue {     
    private Object[] queue;     
    private int size = 0, head = 0, tail = 0;
    private static final int INITIAL_CAPACITY = 16;     

    public FIFOQueue() {         
        queue = new Object[INITIAL_CAPACITY];
    }

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

    public Object pop() throws EmptyStackException {         
        if (size == 0)             
            throw new EmptyStackException();         
        size–;
        Object returnValue = queue[head];
        head = increment(head);
        return returnValue;
    }  

    /**  doubling the capacity each time the array needs to grow.      */     
    private void ensureCapacity() {        
        if (queue.length == size)             
            queue = Arrays.copyOf(queue, 2 * size + 1);     
    }

    /** make sure the pointers are wrapped around at the end of the array */
    private int increment( int x ) {
        if( ++x == queue.length )
            x = 0;
            return x;
        }
    }
Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
GarudaReiga
  • 1,057
  • 2
  • 9
  • 12
  • Out of curiosity, are you required to manually implement a queue rather than using one of the existing [`Queue`](http://docs.oracle.com/javase/8/docs/api/java/util/Queue.html) implementations? For instance, [`ArrayDeque`](http://docs.oracle.com/javase/8/docs/api/java/util/ArrayDeque.html) sounds like it's the same kind of thing you're trying to do here (except it's a [double-ended queue](http://docs.oracle.com/javase/8/docs/api/java/util/Deque.html)) – Powerlord Oct 28 '14 at 05:42
  • This appears to be essentially the same question asked here: http://stackoverflow.com/questions/2843799/why-does-this-code-sample-produce-a-memory-leak?rq=1 But what I don't understand is how the ensureCapacity() is correct in the case when the queue wraps around the end of the array (which is the common case), simply resizing will not make `queue[head]` available. – hcs Oct 28 '14 at 05:59

1 Answers1

0

You answered your own Question:)

Since you don't clean queue reference Garbage Collector, can't clean it from memory, because you have valid reference to this Object in your FIFOQueue. That way you pollute your memory with unused Objects, reducing memory effectively available to your program.

Also as pointed in comments, your ensureCapasity function will only work, when the tail is at the end of an array, otherwise you'll be loosing elements in your queue on push, this is more critical then queue[head] reference problem.

mavarazy
  • 7,562
  • 1
  • 34
  • 60
  • Thanks. I also find this link http://cs.lmu.edu/~ray/notes/queues/ which also mentioned memory leak by not nullifying the object reference. – GarudaReiga Oct 28 '14 at 06:07
  • If you are using your Queue often you'll eventually replace the old reference with the new one, effectively releasing it for GC. – mavarazy Oct 28 '14 at 06:14