Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Slides:



Advertisements
Similar presentations
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Advertisements

Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Chapter 6 Data Types
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
CPSC 388 – Compiler Design and Construction
Rounding Out Classes The objectives of this chapter are: To discuss issues surrounding passing parameters to methods What is "this"? To introduce class.
Road Map Introduction to object oriented programming. Classes
Chapter 10 Storage management
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
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.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Lists and More About Strings CS303E: Elements of Computers and Programming.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Multi-Threaded Programming Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
RMI – Command Line CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Object-Oriented Programming in C++
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Parallel Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
G ARBAGE C OLLECTION CSCE-531 Ankur Jain Neeraj Agrawal 1.
Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Programming Languages and Paradigms Activation Records in Java.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Thread Pools CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Producer/Consumer CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Databases and SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Garbage Collection What is garbage and how can we deal with it?
Core Java Garbage Collection LEVEL – PRACTITIONER.
Java Memory Management
Topic: Java Garbage Collection
Lecture 6 Object Oriented Programming Using Java
Java Memory Management
Unit-2 Objects and Classes
Lecture 2 Memory management.
Pointers and Dynamic Variables
CLEANING UP UNUSED OBJECTS
class PrintOnetoTen { public static void main(String args[]) {
Dynamic Memory.
Developing Java Applications with NetBeans
Developing Java Applications with NetBeans
Lecture 2 Memory management.
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/8 ▪G▪Garbage Collection

Garbage Collection ▪Java doesn’t require much memory management ›C++ has malloc, free, delete, etc. ▪Since Java does not have these key words, another mechanism was needed to clean up memory after it was used ›When a variable goes out of scope, the Garbage Collector will come around and delete that reference in memory ▪Garbage collection looks at the heap and identifies which objects are in use and which are not ›An object is a candidate for garbage collection when it no longer is referenced by any part of your program USC CSCI 201L3/8 Garbage Collection

Garbage Collection Example ▪When are the following variables candidates for garbage collection? 1 public class Garbage { 2 private int num; 3 private static int val; 4 public Garbage(int num) { 5 num++; 6 this.num = num; 7 val = 10; 8 } 9 public static void meth() { 10 Garbage g = new Garbage(number); 11 g.num = 10; 12 Garbage.val = 11; 13 } 14 public static void main(String [] args) { 15 int number = 3; 16 for (int i=0; i < number; i++) { 17 number++; 18 } 19 meth(); 20 System.out.println(number); 21 } 22 } USC CSCI 201L4/8 Garbage Collection

Garbage Collection Steps ▪Step 1 – Marking ›The garbage collector determines which pieces of memory are in use and which are not USC CSCI 201L5/8 Garbage Collection Image Source:

Garbage Collection Steps ▪Step 2 – Normal Deletion ›Garbage collector removes unreferenced objects leaving referenced objects and pointers to free space USC CSCI 201L6/8 Garbage Collection Image Source:

Garbage Collection Steps ▪Step 2a – Deletion with Compacting ›Garbage collector deletes unreferenced objects and compacts the remaining referenced objects USC CSCI 201L7/8 Garbage Collection Image Source:

Invoking Garbage Collection ▪Programmers are not able to explicitly invoke the garbage collector ▪We can make a suggestion to the JVM to run the garbage collector with System.gc(); ▪This does not mean that the garbage collector will be run immediately though ▪More garbage collection information can be found at USC CSCI 201L8/8 Garbage Collector