Pacific-Design.com

    
Home Index

1. Algorithms

2. Stack

Algorithms / Stack /

Java Memory Leak - Eliminate Absolete References

Stack.java

Stack.java

package memoryleak;
import java.util.Arrays;
import java.util.EmptyStackException;

/*
    Stack class maintains obsolete references to these objects. 
    An obsolete reference is simply a reference that will never be dereferenced again. 
    In this case, any references outside of the "active portion" 
    of the element array are obsolete.
    The active portion consists of the elements whose index is less than size.
*/
public class Stack {
    
    private Object[] elements;
    private int size = 0;
    private static final int CAPACITY = 2;
    
    public Stack() {
        elements = new Object[CAPACITY];        
    }
    
    public void push(Object e) {
        capacity();
        elements[size++] = e;        
    }
    
    public Object pop() {
        if(size==0) throw new EmptyStackException();
        //return elements[--size];  // Creates a Memory Leaks
        
        Object result = elements[--size];
        
        // Null out reference once they become absolete
        elements[size] = null;      // Eliminate Absolete Reference
        return result;
    }
    
    private void capacity() {
        if(elements.length == size) {
            elements = Arrays.copyOf(elements, 2 * size + 1);            
        }
        System.out.println("Size " + elements.length);
    }    
}

Elements.java

package memoryleak;

public class Elements {

    private String name;
    private String address;

    public Elements(String name, String address) {
        this.name = name;
        this.address = address;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

Main.java

package memoryleak;

public class Main {


    public static void main(String[] args) {

        System.out.println("Java Memory Leak ...");
               
        Stack stack = new Stack();
        
        stack.push(new Elements("Kevin Duraj", "Los Angeles, CA"));
        stack.push(new Elements("Kevin Thomas", "Santa Monica, CA"));
        stack.push(new Elements("Steve Jobs", "Palo Alto, CA"));
        stack.push(new Elements("Mark Zuckerberg", "Palo Alto, CA"));
        stack.push(new Elements("Steve Wozniak", "San Jose, CA"));
        
        System.out.println("-----------------------------------------");
        Elements person1 = (Elements)stack.pop();
        System.out.println(person1.getName());
        System.out.println(person1.getAddress());        

        stack.push(new Elements("Tim Cook", "San Francisco, CA"));
        
        System.out.println("-----------------------------------------");
        Elements person2 = (Elements)stack.pop();
        System.out.println(person2.getName());
        System.out.println(person2.getAddress());             
        
    }    
}