Garbage Collection What is garbage and how can we deal with it?

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Garbage Collection Introduction What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy.
Garbage Collection CS Introduction to Operating Systems.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
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.
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Garbage Collection. Memory Management So Far ● Some items are preallocated and persist throughout the program: ● What are they? Some are allocated on.
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
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
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 61C L07 More Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
1 An Efficient On-the-Fly Cycle Collection Harel Paz, Erez Petrank - Technion, Israel David F. Bacon, V. T. Rajan - IBM T.J. Watson Research Center Elliot.
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 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Java Garbage Collection, Byte Code James Atlas August 7, 2008.
1 Lecture 22 Garbage Collection Mark and Sweep, Stop and Copy, Reference Counting Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 1.
Memory Management -Memory allocation -Garbage collection.
Runtime The optimized program is ready to run … What sorts of facilities are available at runtime.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
Spring, 2011 –– Computational Thinking – Dennis Kafura – CS 2984 Data Structures Mapping complex structures to linear memory.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
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 Student: Jack Chang. Introduction Manual memory management Memory bugs Automatic memory management We know... A program can only use.
Memory Management.
CSC 533: Programming Languages Spring 2016
Object Lifetime and Pointers
Core Java Garbage Collection LEVEL – PRACTITIONER.
CSC 533: Programming Languages Spring 2015
Storage 18-May-18.
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
Memory Management 6/20/ :27 PM
Storage Management.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Automatic Memory Management
Storage.
Simulated Pointers.
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Simulated Pointers.
Strategies for automatic memory management
Chapter 12 Memory Management
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
Lecturer PSOE Dan Garcia
Garbage collection Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 – More Memory Management Lecturer PSOE Dan Garcia
CSC 533: Programming Languages Spring 2019
Reference Counting.
CMPE 152: Compiler Design May 2 Class Meeting
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Garbage Collection What is garbage and how can we deal with it? Garbage collection schemes Reference Counting Mark and Sweep Stop and Copy

How Objects are Created in Java An object is created in Java by invoking the new() operator. Calling the new() operator, the JVM will do the following: allocate memory; assign fields their default values; run the constructor; a reference is returned.

How Java Reclaims Objects Memory Java does not provide the programmer any means to destroy objects explicitly The advantages are No dangling reference problem in Java Easier programming No memory leak problem

What is Garbage? Garbage: unreferenced objects Ali Object Garbage: unreferenced objects Student ali= new Student(); Student khalid= new Student(); ali=khalid; Now ali Object becomes a garbage, It is unreferenced Object ail khalid Khalid Object

What is Garbage Collection? Finding garbage and reclaiming memory allocated to it. Why Garbage Collection? the heap space occupied by an un-referenced object can be recycled and made available for subsequent new objects When is the Garbage Collection process invoked? When the total memory allocated to a Java program exceeds some threshold. Is a running program affected by garbage collection? Yes, the program suspends during garbage collection.

Advantages of Garbage Collection GC eliminates the need for the programmer to deallocate memory blocks explicitly Garbage collection helps ensure program integrity. Garbage collection can also dramatically simplify programs.

Disadvantages of Garbage Collection Garbage collection adds an overhead that can affect program performance. GC requires extra memory. Programmers have less control over the scheduling of CPU time.

Helping the Garbage Collector Reuse objects instead of generating new ones. This program generates one million objects and prints them out, generating a lot of garbage for (int i=0;i<1000000; ++i) { SomeClass obj= new SomeClass(i); System.out.println(obj); } Using only one object and implementing the setInt() method, we dramatically reduce the garbage generated. SomeClass obj= new SomeClass(); for (int i=0;i< 1000000; ++i) { obj.setInt(i); System.out.println(onj); Eliminate all references to objects that are no longer needed This can be done by assigning null to every variable that refers to an object that is no longer needed

Common Garbage Collection Schemes Three main methods of garbage collection: Reference counting Mark-and-sweep Stop-and-copy garbage collection.

Reference Counting Garbage Collection Main Idea: Add a reference count field for every object. This Field is updated when the number of references to an object changes. Example Object p= new Integer(57); Object q = p; p 57 refCount = 2 q

Reference Counting (cont'd) The update of reference field when we have a reference assignment ( i.e p=q) can be implemented as follows if (p!=q) { if (p!=null) --p.refCount; p=q; ++p.refCount; } Example: Object p = new Integer(57); Object q= new Integer(99); p=q p 57 refCount = 0 q 99 refCount = 2

Reference Counting (cont'd) Reference counting will fail whenever the data structure contains a cycle of references and the cycle is not reachable from a global or local reference ListElements ListElements ListElements head next next next refCount = 1 refCount = 1 refCount = 1

Reference Counting (cont'd) Advantages Conceptually simple: Garbage is easily identified It is easy to implement. Immediate reclamation of storage Objects are not moved in memory during garbage collection. Disadvantages Reference counting does not detect garbage with cyclic references. The overhead of incrementing and decrementing the reference count each time. Extra space: A count field is needed in each object. It may increase heap fragmentation.

Mark-and-Sweep Garbage Collection The mark-and-sweep algorithm is divided into two phases: Mark phase: the garbage collector traverses the graph of references from the root nodes and marks each heap object it encounters. Each object has an extra bit: the mark bit – initially the mark bit is 0. It is set to 1 for the reachable objects in the mark phase. Sweep phase: the GC scans the heap looking for objects with mark bit 0 – these objects have not been visited in the mark phase – they are garbage. Any such object is added to the free list of objects that can be reallocated. The objects with a mark bit 1 have their mark bit reset to 0.

Mark and Sweep (cont'd) Advantages Disadvantages It is able to reclaim garbage that contains cyclic references. There is no overhead in storing and manipulating reference count fields. Objects are not moved during GC – no need to update the references to objects. Disadvantages It may increase heap fragmentation. It does work proportional to the size of the entire heap. The program must be halted while garbage collection is being performed.

Stop-and-Copy Garbage Collection The heap is divided into two regions: Active and Inactive. Objects are allocated from the active region only. When all the space in the active region has been exhausted, program execution is stopped and the heap is traversed. Live objects are copied to the other region as they are encountered by the traversal. The role of the two regions is reversed, i.e., swap (active, inactive). …

Stop-and-Copy Garbage Collection (cont'd) A graphical depiction of a garbage-collected heap that uses a stop and copy algorithm. This figure shows nine snapshots of the heap over time:

Stop-and-Copy Garbage Collection (cont'd) Advantages Only one pass through the data is required. It de-fragments the heap. It does work proportional to the amount of live objects and not to the memory size. It is able to reclaim garbage that contains cyclic references. There is no overhead in storing and manipulating reference count fields.

Stop-and-Copy Garbage Collection (cont'd) Disadvantages Twice as much memory is needed for a given amount of heap space. Objects are moved in memory during garbage collection (i.e., references need to be updated) The program must be halted while garbage collection is being performed.