Runtime System CS 153: Compilers. Runtime System Runtime system: all the stuff that the language implicitly assumes and that is not described in the program.

Slides:



Advertisements
Similar presentations
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Advertisements

Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Lecture 21 Dynamic Memory Allocation:- *Allocation of objects in program ’ s heap _e.g. C ’ s malloc/free or Pascal ’ s new/dispose _e.g. C ’ s malloc/free.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Data & Memory Management CS153: Compilers Greg Morrisett.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Memory Management Tom Roeder CS fa. Motivation Recall unmanaged code eg C: { double* A = malloc(sizeof(double)*M*N); for(int i = 0; i < M*N; i++)
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CPSC 388 – Compiler Design and Construction
CS 536 Spring Automatic Memory Management Lecture 24.
CSC321: Programming Languages 11-1 Programming Languages Tucker and Noonan Chapter 11: Memory Management 11.1 The Heap 11.2 Implementation of Dynamic Arrays.
An Efficient Machine-Independent Procedure for Garbage Collection in Various List Structures, Schorr and Waite CACM August 1967, pp Curtis Dunham.
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.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
MOSTLY PARALLEL GARBAGE COLLECTION Authors : Hans J. Boehm Alan J. Demers Scott Shenker XEROX PARC Presented by:REVITAL SHABTAI.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
An Adaptive, Region-based Allocator for Java Feng Qian & Laurie Hendren 2002.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Garbage collection (& Midterm Topics) David Walker COS 320.
CS61C Midterm Review on C & Memory Management Fall 2006 Aaron Staley Some material taken from slides by: Michael Le Navtej Sadhal.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Reference Counters Associate a counter with each heap item Whenever a heap item is created, such as by a new or malloc instruction, initialize the counter.
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
Intro to Java The Java Virtual Machine. What is the JVM  a software emulation of a hypothetical computing machine that runs Java bytecodes (Java compiler.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Lecture 10 : Introduction to Java Virtual Machine
CSC 253 Lecture 2. Some differences between Java and C  Compiled C code is machine specific, whereas Java compiles for a virt. machine.  Virtual machines.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
Data & Memory Management CS153: Compilers. Records in C: struct Point { int x; int y; }; struct Rect { struct Point ll,lr,ul,ur; }; struct Rect mkSquare(struct.
CS Software Studio Java Lab 1 Meng-Ting Wang PLLAB, Computer Science Department, National Tsing-Hua University.
Parallel Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
Lecture 5 Page 1 CS 111 Online Processes CS 111 On-Line MS Program Operating Systems Peter Reiher.
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
GARBAGE COLLECTION IN AN UNCOOPERATIVE ENVIRONMENT Hans-Juergen Boehm Computer Science Dept. Rice University, Houston Mark Wieser Xerox Corporation, Palo.
Memory Management -Memory allocation -Garbage collection.
Programming Languages and Paradigms Activation Records in Java.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
20-Feb-16 javap. Bytecode Java is compiled into bytecode, which is intermediate between Java and a “real” assembly language To implement Java, it is only.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
RealTimeSystems Lab Jong-Koo, Lim
Memory Management in Java Mr. Gerb Computer Science 4.
Object Lifetime and Pointers
Dynamic Compilation Vijay Janapa Reddi
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
Dynamic Memory Allocation
CS 153: Concepts of Compiler Design November 28 Class Meeting
Introduction to Algorithm Design
Strategies for automatic memory management
Memory Allocation CS 217.
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Presentation transcript:

Runtime System CS 153: Compilers

Runtime System Runtime system: all the stuff that the language implicitly assumes and that is not described in the program – Handling of POSIX signals – Automated memory management (garbage collection) – Automated core management (work stealing) – Virtual machine execution (just-in-time compilation) – Class loading – …

Garbage Collection: Starting from stack, registers, & globals (roots), determine which objects in the heap are reachable following pointers. Reclaim any object that isn't reachable. Requires being able to determine pointer values from other values (e.g., ints). – OCaml uses the low bit: 1 it's a scalar, 0 it's a pointer. – In Java, we use put the tag bits in the meta-data. – For Boehm collector, we use heuristics: (e.g., the value doesn't point into an allocated object.)

Mark and Sweep Collector Reserve a mark-bit for each object. Starting from roots, mark all accessible objects. Stick accessible objects into a queue or stack. – queue: breadth-first traversal – stack: depth-first traversal Loop until queue/stack is empty: – remove marked object (say x). – if x points to an (unmarked) object y, then mark y and put it in the queue. Run through all objects: – If they haven't been marked, put them on the free list. – If they have been marked, clear the mark bit.

Stop and Copy Collector: Split the heap into two pieces. Allocate in 1st piece until it fills up. Copy the reachable data into the 2nd area, compressing out the holes corresponding to garbage objects.

Reality: Techniques such as generational or incremental collection can greatly reduce latency. – A few millisecond pause times. Large objects (e.g., arrays) can be copied in a "virtual" fashion without doing a physical copy. Some systems use a mix of copying collection and mark/sweep with support for compaction. A real challenge is scaling this to server-scale systems with terabytes of memory… Interactions with OS matter a lot: cheaper to do GC than it is to start paging…

Conservative Collectors: Work without help from the compiler. – e.g., legacy C/C++ code. – e.g., your compiler :-) Cannot accurately determine which values are pointers. – But can rule out some values (e.g., if they don't point into the data segment.) – So they must conservatively treat anything that looks like a pointer as such. – Two bad things result: leaks, can't move.

The Boehm Collector Based on mark/sweep. – performs sweep lazily Organizes free lists as we saw earlier. – different lists for different sized objects. – relatively fast (single-threaded) allocation. Most of the cleverness is in finding roots: – global variables, stack, registers, etc. And determining values aren't pointers: – blacklisting, etc.

Work-Stealing Garbage collection takes care of managing an important resource: memory Work-stealing takes care of managing cores/processors Each core runs a thread associated with a work dequeue A thread can push and pop work from one end of its dequeue When out of work, a thread can steal work from the other end of another thread dequeue

Work-Stealing: example class Fibonacci extends RecursiveTask { final int n; Fibonacci(int n) { this.n = n; } Integer compute() { if (n <= 1) return n; Fibonacci f1 = new Fibonacci(n - 1); f1.fork(); Fibonacci f2 = new Fibonacci(n - 2); return f2.compute() + f1.join(); } NB: This is a dumb Fibonacci to illustrate work-stealing

Virtual Machines Some languages are neither interpreted nor compiled to native code Instead the compiler generates code in a virtual assembly language (called bytecode) At runtime, the bytecode is interpreted by a virtual machine Sometimes, the runtime can compile important code further to native code on the fly. This is called Just-In-Time compilation

Example: Java public class Hi { public static void main(String[] args) { System.out.println("Hi"); } } Javac Hi.java generates Hi.class

Example: Java javap -c Hi Compiled from "Hi.java" public class Hi { public Hi(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object." ":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #3 // String Hi 5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return } Java Hi Hi