Memory leaks in Java n Not exactly memory

  • Slides: 7
Download presentation
Memory leaks in Java? n Not exactly memory leak u but reduced performance due

Memory leaks in Java? n Not exactly memory leak u but reduced performance due to « « 12/12/2021 increased garbage collection activities and increased memory footprint Good Java Programming 1

Stack implementation in Java public class My. Stack { private Object[] stack. Elements; private

Stack implementation in Java public class My. Stack { private Object[] stack. Elements; private int current. Size = 0; private static int INIT_CAPACITY = 100; public My. Stack() { stack. Elements = new Object[INIT_CAPACITY]; } public void push(Object obj. Add) { ensure. Capacity(); current. Size++; stack. Elements[current. Size] = obj. Add; } // code continued in next slide } 12/12/2021 Good Java Programming 2

Stack implementation in Java (contd) public Object pop() { if (0 == current. Size)

Stack implementation in Java (contd) public Object pop() { if (0 == current. Size) throw new Empty. Stack. Exception(); current. Size--; return stack. Elements[current. Size]; } private void ensure. Capacity() { if (stack. Elements. length == current. Size) { // double the array + 1 // copy. Of method: http: //download. oracle. com/javase/6/docs/api/java/util/Arrays. html stack. Elements = Arrays. copy. Of(stack. Elements, 2 * current. Size + 1); } } } // end class My. Stack 12/12/2021 Good Java Programming 3

Memory leaks in Java? n As the stack grows and shrinks objects that were

Memory leaks in Java? n As the stack grows and shrinks objects that were popped off will not be garbage collected u even if the driver/client code, which is using the Stack, does not have those references n Why? u Stack maintains obsolete references to these objects u Obsolete reference: a reference that will never be dereferenced again n This is called Unintentional Object Retention n Note that such objects may contain other references and so. Good on. Java Programming 12/12/2021 4

Unintentional Object Retention n Note that such objects may contain other references and so

Unintentional Object Retention n Note that such objects may contain other references and so on n Guideline u Always null the references u Added benefit « accidental use of that reference (array index in this case) will result in a Null. Pointer. Exception u Do NOT null every reference « « 12/12/2021 overkill, cluttered code Define each variable in the narrowest possible scope and let it fall out of scope (we will discuss this later) Good Java Programming 5

Eliminate obsolete references public Object pop() { if (0 == current. Size) throw new

Eliminate obsolete references public Object pop() { if (0 == current. Size) throw new Empty. Stack. Exception(); Object result. Obj = stack. Elements[current. Size]; // elimnate the obsolete reference by setting it to null stack. Elements[current. Size] = null; current. Size--; return result. Obj; } 12/12/2021 Good Java Programming 6

Design Guideline Eliminate obsolete object references 12/12/2021 Good Java Programming 7

Design Guideline Eliminate obsolete object references 12/12/2021 Good Java Programming 7