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
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
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
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
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
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
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:

CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)Lecture 8:Introduction to the Heap and Garbage CollectionLast time:1.Software lifecycle2.Pseudo-code (from last lecture)3.Objects and classesToday:1.Heaps2.Garbage CollectionCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)1How Are Objects Created?In Java: using newRecall: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 2007Jan Plane (adapted from Bonnie Dorr)2Heap = “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.Rest of memory is called heap and can be used for other purposes, including storing new objects4.5y3xValueVariableCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)3AllocatedHeapMain MemoryStack grows, shrinks during program execution (why?)So does “allocated heap” (part of heap in use)Unallocated part of heap is called “free”HeapStackFreeHeapCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)4Object CreationNew space allocated in heap to store instance variablesReference (= address) to this space is returnedScanner sc = new (…);AllocatedHeapHeapStackFreeHeapscCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)5Strings Are ObjectsWhere is new inString 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 2007Jan Plane (adapted from Bonnie Dorr)6In 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 2007Jan Plane (adapted from Bonnie Dorr)7int x = 7; float y = 3.3;String f = “cat”;ExampleHeapStackx 7y 3.3“cat”fCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)8Heap 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!HeapStacks“cat” “dog” “cow”CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)9Garbage 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 reclaimedCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)10Equality and Objects== can be used to compare objects, but result is not always whatyou 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 2007Jan Plane (adapted from Bonnie Dorr)11== 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 2007Jan Plane (adapted from Bonnie Dorr)12StackabExampleString a = new String (“abc”);String b = new String (“abc”);if (a == b) {println (“Equal”);} else {println (“Not equal”);} Not equal is printedHeap“abc” “abc”CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)13HeapStackabContrasting ExampleString 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?String a = "abc"String b = new String(a);“abc”CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)14“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


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 2 2 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?