1 / 15

Object-Oriented Languages - Design and Implementation

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

moana
Download Presentation

Object-Oriented Languages - Design and Implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Languages - Design and Implementation Applies to C# as well Java: Behind the Scenes Finn E. Nordbjerg, fen@noea.dkAdvanced Computer StudiesAcademy of Higher Professional Education of Northern Jutland Object-Oriented Languages

  2. Object-Oriented Languages - Design and ImplementationJava: Behind the Scenes Outline:Dynamic Method LookupGarbage CollectionProblems and Solutions Object-Oriented Languages

  3. Java: Behind the Scenes:Object-Oriented Languages – Design and Implementation Interesting problems: What goes actually on during program execution? Object-orientation: encapsulation and methods dynamic allocation and de-allocation of objects inheritance polymorphism/dynamic (late) binding Among many other things: Dynamic Method Lookup Garbage Collection 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 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 In the case of multiple inheritance it’s actually a directed graph of ancestors that must be traversed Please note how this scheme supports redefinition of methods in subclasses 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 Trading space for time B. Meyer: Object-Oriented Software Construction, 2ed Ed. Prentice-Hall 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 Object-Oriented Languages

  8. Memory-model (just to refresh your memories) 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 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 Object-Oriented Languages

  10. An object reference in the program Mark is actually a graph traversal 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); } 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!!! 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!!! 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!!!) 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 Object-Oriented Languages

More Related