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

Slides:



Advertisements
Similar presentations
An Implementation of Mostly- Copying GC on Ruby VM Tomoharu Ugawa The University of Electro-Communications, Japan.
Advertisements

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Part IV: Memory Management
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Memory Management Chapter 7.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
CS 536 Spring Automatic Memory Management Lecture 24.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Memory Management Professor Yihjia Tsai Tamkang University.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Programming Language Paradigms: summary. Object-oriented programming Objects are the fundamental building blocks of a program. Interaction is structured.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Run-Time Storage Organization
Run time vs. Compile time
Memory Management (continued) CS-3013 C-term Memory Management CS-3013 Operating Systems C-term 2008 (Slides include materials from Operating System.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Garbage collection (& Midterm Topics) David Walker COS 320.
Linked lists and memory allocation Prof. Noah Snavely CS1114
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Object-oriented design CS 345 September 20,2002. Unavoidable Complexity Many software systems are very complex: –Many developers –Ongoing lifespan –Large.
Comparison of OO Programming Languages © Jason Voegele, 2003.
Programming Languages and Paradigms Object-Oriented Programming.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
 OOPLs  Help companies reduce complexity  Increase competition in open markets  Speeds up development  Improves maintenance, resusability, modifiability.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
Chapter 12 Support for Object oriented Programming.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
COMP3190: Principle of Programming Languages
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Garbage Collection What is garbage and how can we deal with it?
Section 10: Memory Allocation Topics
Topic: Java Garbage Collection
Names and Attributes Names are a key programming language feature
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Dynamic Memory Allocation
Storage Management.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Memory Management Memory Areas and their use Memory Manager Tasks:
Automatic Memory Management
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Binding Times Binding is an association between two things Examples:
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

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

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

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

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

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

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

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

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 )

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

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

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); }

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!!!

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!!!

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!!!)

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