Download Stack implementation in Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Functional programming wikipedia , lookup

Go (programming language) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Smalltalk wikipedia , lookup

Java syntax wikipedia , lookup

Class (computer programming) wikipedia , lookup

Dependency injection wikipedia , lookup

Subroutine wikipedia , lookup

Name mangling wikipedia , lookup

Reactive programming wikipedia , lookup

Library (computing) wikipedia , lookup

Scala (programming language) wikipedia , lookup

Structured programming wikipedia , lookup

Resource management (computing) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Design Patterns wikipedia , lookup

C++ wikipedia , lookup

Java (programming language) wikipedia , lookup

Object-oriented programming wikipedia , lookup

Java performance wikipedia , lookup

Object lifetime wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
Memory leaks in Java?
 Not exactly memory leak

but reduced performance due to


7/7/2017
increased garbage collection activities and
increased memory footprint
Good Java Programming
1
Stack implementation in Java
public class MyStack {
private Object[] stackElements;
private int currentSize = 0;
private static int INIT_CAPACITY = 100;
public MyStack() {
stackElements = new Object[INIT_CAPACITY];
}
public void push(Object objAdd) {
ensureCapacity();
currentSize++;
stackElements[currentSize] = objAdd;
}
// code continued in next slide
}
7/7/2017
Good Java Programming
2
Stack implementation in Java (contd)
public Object pop() {
if (0 == currentSize)
throw new EmptyStackException();
currentSize--;
return stackElements[currentSize];
}
private void ensureCapacity() {
if (stackElements.length == currentSize) {
// double the array + 1
// copyOf method:
http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html
stackElements = Arrays.copyOf(stackElements, 2 * currentSize + 1);
}
}
} // end class MyStack
7/7/2017
Good Java Programming
3
Memory leaks in Java?
 As the stack grows and shrinks objects that were
popped off will not be garbage collected

even if the driver/client code, which is using the
Stack, does not have those references
 Why?
 Stack maintains obsolete references to these objects
 Obsolete reference: a reference that will never be
dereferenced again
 This is called Unintentional Object Retention
 Note that such objects may contain other
references and soGood
onJava Programming
7/7/2017
4
Unintentional Object Retention
 Note that such objects may contain other
references and so on
 Guideline
 Always null the references
 Added benefit

accidental use of that reference (array index in this case) will
result in a NullPointerException
 Do NOT null every reference


7/7/2017
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 == currentSize)
throw new EmptyStackException();
Object resultObj = stackElements[currentSize];
// elimnate the obsolete reference by setting it to null
stackElements[currentSize] = null;
currentSize--;
return resultObj;
}
7/7/2017
Good Java Programming
6
Design Guideline
Eliminate obsolete object references
7/7/2017
Good Java Programming
7