From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for...

16
From Theory to Practice 2 OOP 2006

Transcript of From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for...

Page 1: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

From Theory to Practice 2

OOP 2006

Page 2: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Overview• Performance issues:

– Preparing classes for inheritance– Memory management and release of obsolete

object references

• The sources for this lecture:– Effective Java™ by Joshua Bloch

• The book contains many rules of thumbfor writing a code that is clear, correct,robust and reuseable

• Most code samples are taken from http://java.sun.com/docs/books/effective/

– About garbage collection:http://java.sun.com/developer/technicalArticles/ALT/RefObj/

Page 3: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Preparing Classes For Inheritance

• Design and document for inheritance or else prohibit it.

What does it mean?• Document precisely the effect of overriding

any method • Choose protected methods judiciously• Constructors and clone must not involve

overridable methods• Special measures should be taken when

implementing Serializable objects

Page 4: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Documentationremove

public boolean remove(Object o)

Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an element e such that (o==null ? e==null : o.equals(e)), if the collection contains one or more such elements. Returns true if the collection contained the specified element (or equivalently, if the collection changed as a result of the call).This implementation iterates over the collection looking for the specified element. If it finds the element, it removes the element from the collection using the iterator's remove method.

Note that this implementation throws an UnsupportedOperationException if the iterator returned by this collection's iterator method does not implement the remove method and this collection contains the specified object.

A description of the effect of overriding a method inAbstractCollection

Page 5: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Choosing Protected Methods

• Consider the API of abstractList

• The following method is called by clear

• Allows efficient implementation of clear in inherited class

List subList(int fromIndex, int toIndex) Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive.

void clear() Removes all of the elements from this collection (optional operation).

protected void removeRange(int fromIndex, int toIndex)

Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.

Page 6: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Constructors

• What happens if the constructor invokes overridable method?

public class Super {

// Broken - constructor invokes overridable method

public Super() {

m();

}

public void m() {

}

}

Page 7: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Constructors

final class Sub extends Super {

// Blank final, set by constructor

private final Date date;

Sub() {

date = new Date();

}

// Overrides Super.m, invoked by the constructor Super()

public void m() {

System.out.println(date);

}

public static void main(String[] args) {

Sub s = new Sub();

s.m();

}

}

The program prints null

Page 8: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Memory Management in Java

• The memory in java is handled in two main structures:– Program execution stack: contains the memory

required by method invocation– The memory heap: contains the memory required

by objects in the program

• The new command allocates memory for an object and activates its constructor

Page 9: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Releasing Unused Objects

• Memory release of unreferenced objects is handled using garbage collection

• The garbage collector: – Identify objects that are no-longer in use– Recycle memory occupied by such objects

• An object is in use if it can be accessed or reached by the program in its current state

• In order to define a reachable object we first define the root set of references

Page 10: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

The Root Set and Reachable Objects

• The root set of references includes:– References to objects in active methods of the

program:• Method arguments• Local variables

– Static reference variables– References reiterated through the Java Native

Interface (JNI)

• An object is reachable if– It is referenced by a reference from the root set– It is referenced by another reachable object

Page 11: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

The Root Set and Reachable Objects

Root set

of references

Unreachable objects (garbage)

Reachable objects

Java Heap

Page 12: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Advantage of Garbage collected Heap

• In programming languages such as C and C++, the programmer manages memory allocation and release

• In Java, the programmer does not have to deal with memory allocation

• But special care should be taken to avoid memory leaks – where not-used objects still occupy memory

Page 13: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Example – Stack Implementationpublic class Stack {

private Object[] elements;

private int size = 0;

public Stack(int initialCapacity) {

this.elements = new Object[initialCapacity];

}

public void push(Object e) {

ensureCapacity();

elements[size++] = e;

}

private void ensureCapacity() {

if (elements.length == size) {

Object[] oldElements = elements;

elements = new Object[2 * elements.length + 1];

System.arraycopy(oldElements, 0, elements, 0, size);

}

}

Page 14: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Stack Implementation - continue // Can you spot the memory leak?

public Object pop() {

if (size==0)

throw new EmptyStackException();

Object result = elements[--size];

return result;

}

• If the stack shrinks, objects that were popped of the stack will not be garbage collected

• Memory leak potential consequences– Reduced performance due to paging or increased

garbage collection activity– OutOfMemoryError

Page 15: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Solution

public Object pop() {

if (size==0)

throw new EmptyStackException();

Object result = elements[--size];

// Eliminate obsolete reference

elements[size] = null;

return result;

}

Page 16: From Theory to Practice 2 OOP 2006. Overview Performance issues: –Preparing classes for inheritance –Memory management and release of obsolete object.

Correct Memory handling

• Memory leaks are hard to detect and resolved (there are specialized tools for that)

• Null out object references in classes that manage their own memory

• Special care should also be taken for caches• Do not null out every object finishing its role:

– References of local variables fall out of scope– Variable reuse releases the previous referenced

objects

• Define each variable in the narrowest possible score (There are other good reasons to do it, what are they?)