Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,

Similar presentations


Presentation on theme: "OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,"— Presentation transcript:

1 OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dk Advanced Computer Studies Academy of Higher Professional Education of Northern Jutlandfen@noea.dk Applies to C# as well

2 OOPLs /FEN March 2004 Object-Oriented Languages2 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Outline: Dynamic Method Lookup Garbage Collection Problems and Solutions

3 OOPLs /FEN March 2004 Object-Oriented Languages3 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

4 OOPLs /FEN March 2004 Object-Oriented Languages4 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

5 OOPLs /FEN March 2004 Object-Oriented Languages5 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

6 OOPLs /FEN March 2004 Object-Oriented Languages6 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

7 OOPLs /FEN March 2004 Object-Oriented Languages7 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

8 OOPLs /FEN March 2004 Object-Oriented Languages8 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 )

9 OOPLs /FEN March 2004 Object-Oriented Languages9 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

10 OOPLs /FEN March 2004 Object-Oriented Languages10 Mark is actually a graph traversal An object reference in the program

11 OOPLs /FEN March 2004 Object-Oriented Languages11 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); }

12 OOPLs /FEN March 2004 Object-Oriented Languages12 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 G 0, G 1, …. G 0 is youngest –G.C. is only performed in G 0 or in G 0 and G 1 or in G 0, G 1 and G 2 … –In G 0 often only about 10% of the objects are alive, i.d. G.C. pays a lot Generational G.C. is s very good idea!!!

13 OOPLs /FEN March 2004 Object-Oriented Languages13 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!!!

14 OOPLs /FEN March 2004 Object-Oriented Languages14 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!!!)

15 OOPLs /FEN March 2004 Object-Oriented Languages15 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


Download ppt "OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,"

Similar presentations


Ads by Google