oopls /fen march 2004 object-oriented languages1 object-oriented languages - design and...

15
OOPLs /FEN March 2004 Object-Oriented Languages 1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, [email protected] Advanced Computer Studies Academy of Higher Professional Education of Northern Jutland Applies to C# as well

Upload: victor-bryant

Post on 17-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 1

Object-Oriented Languages - Design and Implementation

Java: Behind the Scenes

Finn E. Nordbjerg, [email protected] Computer Studies

Academy of Higher Professional Education of Northern Jutland

Applies to C# as well

Page 2: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 2

Object-Oriented Languages - Design and Implementation

Java: Behind the Scenes

Outline:Dynamic Method Lookup

Garbage CollectionProblems and Solutions

Page 3: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 3

Java: Behind the Scenes:Object-Oriented Languages – Design and

Implementation

•Object-orientation:– encapsulation and methods– dynamic allocation and de-allocation of objects – inheritance– polymorphism/dynamic (late) binding

Interesting problems: What goes actually on during program

execution?

Among many other things:

Dynamic Method Lookup

Garbage Collection

Page 4: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 4

Dynamic Method Lookup

• an object must always know it type (class)–Objects carry a reference to a class-descriptor. The class-descriptor is generated by the compiler–The class-descriptor contains (among other things):

• a collection of references to methods (VMT)• a reference to the class-descriptor of its direct ancestor (superclass) (in the case of multiple inheritance: a collection of references to superclasses).

VMT: Virtual Method Table

Page 5: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 5

Dynamic Method Lookup•when a method is invoked on an object:

– the reference to the class-descriptor of the object is followed:•first the VMT of the object it self is search:

–If the method is not inherited, the method will be found here. The reference to the code segment is followed and the method is called as an ordinary procedure or function

–If the method is not found, the the ancestor-reference is followed and the ancestor’s VMT is searched.

•This search continues recursively up through the tree of ancestor classes

Please note how this scheme supports

redefinition of methods in subclasses

In the case of multiple inheritance it’s actually a

directed graph of ancestors that must be traversed

Page 6: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 6

A word on costs associated with Dynamic Binding

• Consider the method call:x.f(a,b) where is f defined?

in the class (type)of x? Or in a predecessor?

• If multiple inheritance is supported then the entire predecessor graph must be searched:– This costs a large overhead in dynamic typed languages like

Smalltalk (normally these languages don’t support multiple inheritance)

– In static typed languages like Java, Eiffel, C++, C# the compiler is able to analyse the class-hierarchy (or more precisely: the graph) for x and create a VTM containing addresses for all methods of an object (including inherited methods)

– According to Meyer the overhead of this compared to static binding is at most 30%, and overhead decreases with complexity of the method

B. Meyer: Object-Oriented Software Construction, 2ed

Ed. Prentice-Hall

Trading space for

time

Page 7: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 7

Garbage Collection (GC)

•A running program is complex graph of objects referencing each other which makes memory management a complex task•Garbage collection is one (and the most common) solution:

–A technique to lift the burden of memory management off the shoulders of the programmer

–Decreases the costs of coding with approximately 30%

–Standard in most modern OOPLs

–Run-time overhead to be paid

–Is nondeterministic which makes G.C. problematic (unusable?) in systems with reel time requirements

Page 8: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 8

Dynamically allocated memory

• Programs ask for and get memory allocated on arbitrary point during execution

• This is done via call to the new-operator• When this memory is no longer used by the

program it must be freed again• The heap-manager maintains a (doubly

linked) list (freeSpaceList) of free blocks of memory, from which allocation is done when new is called

Memory-model (just to refresh your memories)

Page 9: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 9

Mark-and-Sweep Garbage Collection

• Classic GC-algorithm - still widely used• Two phases:

– firstly all active objects are marked (mark)– secondly all not marked objects are returned the

freeSpaceList of the heap (sweep)

• Problems:– the program must be suspended during garbage

collection– the heap will be fragmented

Page 10: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 10

Mark is actually a

graph traversal

An object reference in the

program

Page 11: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 11

Mark-and-Sweep Garbage Collection

void markAndSweepGC(Program p){for(alle referencer r fra p)

mark(r);sweep();

}void mark(Object o){

if(!o.marked){o.marked= true;for(alle referencer r fra o)//DFS

mark(r);}

}void sweep(){

for(alle Object o i Heap h)if(o.marked)

o.marked= false;else

h.release(o);}

Page 12: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 12

Generational G.C.

• Experience shows that the youngest objects tend to die first. This means, that old objects tend to become very old.

• Generational G.C. concentrates on new objects:– The heap is partitioned into generations G0, G1, …. G0 is

youngest

– G.C. is only performed in G0 or in G0 and G1 or in G0, G1 and G2…

– In G0 often only about 10% of the objects are alive, i.d. G.C. pays a lot

• Generational G.C. is s very good idea!!!

Page 13: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 13

Incremental (parallel) G.C.

• Avoid halting the application• G.C. and the application execute in separate

threads, this means that G.C. executes in small steps (”incrementally”) interleaved with the execution of the application

• This means that the graph of live objects are modified during marking, which complicates the algorithm some what

• This is left for an exercise!!!

Page 14: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 14

Other issues

• Compiler-support for G.C.:– The compiler generates code for allocating objects,

it must also generate code to support G.C.:• The G.C. algorithm must be able to recognise root-

pointers when scanning stack-frames• Object-descriptors and data-layout on the heap• Providing pointer maps• etc.

• In systems with distributed objects the problem of garbage collection is rather more complicated. (This is left as an exercise!!!)

Page 15: OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk

OOPLs /FEN March 2004

Object-Oriented Languages 15

Summary:

• Dynamic method lookup is a prerequisite for polymorphism

• Garbage Collection increases speed of development

• Both features are essential to object-oriented programming and should be language supported

• But there is a cost in performance to be paid – although extensive research is going on to lower the costs

• A skilled programmer must understand these mechanisms in

order to use them as efficiently as possible