DOC PREVIEW
UMD CMSC 131 - Lecture 8: Introduction to the Heap and Garbage Collection

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 8 Introduction to the Heap and Garbage Collection Last time 1 Software lifecycle 2 Pseudo code from last lecture 3 Objects and classes Today 1 Heaps 2 Garbage Collection CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr How Are Objects Created In Java using new Recall Scanner sc new Scanner System in Invoking new creates fresh copies of instance variables in the heap returns the address where the fresh variables are stored Heap Address CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 1 Heap Fresh Memory While a program is running some memory is used to store variables Terminology stack We have been representing stack as table e g Variable x Value 3 y 4 5 Rest of memory is called heap and can be used for other purposes including storing new objects CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 2 Main Memory Stack grows shrinks during program execution why So does allocated heap part of heap in use Unallocated part of heap is called free Heap Allocated Heap Free Heap Stack CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 3 Object Creation New space allocated in heap to store instance variables Reference address to this space is returned Scanner sc new Heap Allocated Heap Free Heap Stack sc CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 4 Strings Are Objects Where is new in String name Narita Java provides it String is special because it is used so often Java automatically fills in new You can too String name new String Narita CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 5 In Java 9 Sorts of Variables 8 primitively typed Types are the 8 built ins int byte double etc Storage allocated on stack based on type Value stored in stack e g int x Reference typed Types are classes Storage allocated on stack to hold one memory address typically one word What is stored in stack is reference to heap where actual data is stored e g Scanner sc new Scanner System in CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 6 Example int x 7 float y 3 3 String f cat Stack x Heap 7 y 3 3 f cat CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 7 Heap Issues What happens if new is called and there is no free heap Crash What happens if following are executed String s s new String cat s new String dog s new String cow Wasted heap cat dog no longer referenced by stack Crashes become a problem Stack Heap s cat dog cow CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 8 Garbage Collection This heap management or memory management issue is central in CS Java copes by invoking garbage collector to reclaim unused but still allocated heap space Garbage collector reclaims memory in allocated heap and returns it to free heap In previous example cat and dog would be reclaimed CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 9 Equality and Objects can be used to compare objects but result is not always what you expect What is output of following String a new String abc String b new String abc if a b println Equal else println Not equal Not equal CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 10 for Reference Values compares for identical values If x y are reference variables then they contain addresses Two addresses are equal if they point to exactly the same thing In previous example a and b are assigned different addresses because new is called twice CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 11 Example String a new String abc String b new String abc if a b println Equal else println Not equal Stack Heap a b Not equal is printed abc abc CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 12 Contrasting Example String a new String abc String b a if a b println Equal else println Not equal Equal is printed This is called ALIASING Two variables refer to same object Can be DANGEROUS What if we really want to make a copy Stack Heap a b String a abc String b new String a CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr abc 13 equals checks if two reference variables refer to the same object Methods like str equals check if two different objects have the same content Other classes will have an equals method also CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 14


View Full Document

UMD CMSC 131 - Lecture 8: Introduction to the Heap and Garbage Collection

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 8: Introduction to the Heap and Garbage Collection
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 8: Introduction to the Heap and Garbage Collection and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 8: Introduction to the Heap and Garbage Collection and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?