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 storedHeap? Address?CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)2Heap = “Fresh Memory”While a program is running, some memory is used to store variablesTerminology: stackWe 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 MemoryStack 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 CreationNew space allocated in heap to store instance variablesReference (= address) to this space is returnedScanner sc = new (…);AllocatedHeapHeapStackFreeHeapscCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)5Strings Are ObjectsWhere is new inString name = “Narita”; ?Java provides it!String is special because it is used so oftenJava automatically “fills in” newYou can too:String name = new String(“Narita”);CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)6In Java, 9 Sorts of Variables8 primitively typedTypes are the 8 built-ins (int, byte, double, etc.)Storage allocated on stack based on typeValue stored in stacke.g. int xReference typedTypes are classesStorage allocated on stack to hold one memory address (typically, one word)What is stored in stack is reference to heap, where actual data is storede.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 IssuesWhat 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 stackCrashes become a problem!HeapStacks“cat” “dog” “cow”CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)9Garbage CollectionThis “heap management” or “memory management” issue is central in CSJava copes by invoking garbage collector to reclaim unused but still-allocated heap spaceGarbage collector reclaims memory in allocated heap and returns it to free heapIn 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 valuesIf x, y are reference variables, then they contain addressesTwo addresses are equal if they point to exactly the same thingIn 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 printedThis 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 objectMethods like str.equals() check if two different objects have the same “content”Other classes will have an equals method
View Full Document