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

Slides:



Advertisements
Similar presentations
(Chapter 5) Deleting Objects
Advertisements

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.
5. Memory Management From: Chapter 5, Modern Compiler Design, by Dick Grunt et al.
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. 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.
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
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.
CSC321: Programming Languages 11-1 Programming Languages Tucker and Noonan Chapter 11: Memory Management 11.1 The Heap 11.2 Implementation of Dynamic Arrays.
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.
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Compilation 2007 Garbage Collection Michael I. Schwartzbach BRICS, University of Aarhus.
Reference Types. 2 Objectives Introduce reference types –class –array Discuss details of use –declaration –allocation –assignment –null –parameter –aggregation.
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.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
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.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Memory management (CTM 2.5) Carlos Varela RPI April 6, 2015.
Initialization & Cleanup Guaranteed initialization with the constructor The name of the constructor is the same as the name of the class.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
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.
OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,
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.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
11/26/2015IT 3271 Memory Management (Ch 14) n Dynamic memory allocation Language systems provide an important hidden player: Runtime memory manager – Activation.
1 Languages and Compilers (SProg og Oversættere) Heap allocation and Garbage Collection.
Garbage Collection and Classloading Java Garbage Collectors  Eden Space  Surviver Space  Tenured Gen  Perm Gen  Garbage Collection Notes Classloading.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 1.
Memory Management -Memory allocation -Garbage collection.
2/4/20161 GC16/3011 Functional Programming Lecture 20 Garbage Collection Techniques.
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.
CSC 533: Programming Languages Spring 2016
Garbage Collection What is garbage and how can we deal with it?
Core Java Garbage Collection LEVEL – PRACTITIONER.
CSC 533: Programming Languages Spring 2015
Storage 18-May-18.
Smalltalk Implementation: Memory Management and Garbage Collection
Storage Management.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Automatic Memory Management
Storage.
Simulated Pointers.
Simulated Pointers.
Strategies for automatic memory management
Chapter 12 Memory Management
CS5123 Software Validation and Quality Assurance
List Allocation and Garbage Collection
CSC 533: Programming Languages Spring 2019
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

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

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 Objects Memory Reclaimed 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 Student ali= new Student(); Student khalid= new Student(); ali=khalid; Now Ali Object becomes a garbage, It is unreferenced Object Ali Object ail khalid Khalid Object

What is Garbage Collection? Finding garbage and reclaiming memory allocated to it. 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. Refference to ali object Refference to khalid object Khalid Object

Reduce, Reuse, Recycle Modern Societies produce an excessive amount of waste? What is the solution? Reduce Reuse Recycle The same Applies to Java!!!

Reduce Garbage A Java program that does not create any objects does not create garbage. Objects used until the end of the program do not become garbage. Reducing the number of objects that may be used until the end of the program will reduce the amount of garbage generated.

Reuse Garbage Reuse objects instead of generating new ones. for (int i=0;i< ; ++i) { SomeClass obj= new SomeClass(i); System.out.println(obj); This program generates one million objects and prints them out. SomeClass obj= new SomeClass(); for (int i=0;i< ; ++i) { obj.setInt(i); System.out.println(onj); Using only one object and implementing the setInt() method, we dramatically reduce the garbage generated.

Recycle Garbage Don't leave unused objects for the garbage collector. Put them instead in a container to be searched when an object is needed. Advantage: reduces garbage generation. Disadvantage: puts more overhead on the programmer.

Helping the Garbage Collector Sometimes we need the garbage collector to run more frequently. How we can help the collector? 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

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; 57 refCount = 2 p q

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

Reference Counting (cont'd) Advantages and Disadvantages + Garbage is easily identified. + Garbage can be collected incrementally. - Every object should have a reference count field. - Overhead for updating reference count fields.

Reference Counting (cont'd) What in case of indirect references? We can still use reference counting, provided we consider all references to an object including references from other objects. Object p = new Association(new Integer(57), new Integer(99));

Reference Counting (cont'd) When does reference counting fail? When head is assigned to null, first object reference count becomes 1 and not zero Reference counting will fail whenever the data structure contains a cycle of references next refCount = 1 ListElements refCount = 1 ListElements next refCount = 1 ListElements next head

Mark-and-Sweep Garbage Collection It is the first garbage collection algorithm that is able to reclaim cyclic data structures. Mark and sweep algorithm consists of two phases: mark phase sweep phase for each root variable r mark(r); sweep();

Mark and Sweep (cont'd) void sweep(){ for each Object p in the heap { if (p.marked) p.marked=false; else heap.release(p); } program

Mark and Sweep (cont'd) Advantages It correctly identifies and collects garbage even in the presence of reference cycles. No overhead in manipulating references. Disadvantages The program suspends while garbage collecting. Fragmentation problem.

Stop-and-Copy Garbage Collection This algorithm collects garbage and defragments the heap. The heap is divided into two regions: active and inactive. When the memory in the active region is exhausted, the program is suspended and : Live objects are copied to the inactive region contiguously The active and inactive regions reverse their roles The Algorithm for each root variable r r=copy(r,inactiveHeap); swap (activeHeap,inactiveHeap);

Stop-and-Copy Garbage Collection (cont'd) Object copy(Object p, Heap destination) { if (p==null) return null; if (p.forward==null) { q=destination.newInstance(p.class); p.forward= q; for each field f in p { if ( f is primitive type) q.f=p.f; else q.f= copy(p.f, destination); } q.forware = null; } return p.forware; } A’ null B’ null C’ null head inactive active

Stop-and-Copy Garbage Collection (cont'd) Advantages Defragments the heap. Disadvantages All objects should be copied when the garbage collector is invoked. It requires twice as much memory as the program actually uses.