Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 213 – Large Scale Programming. Explicit Memory Management  Traditional form of memory management  Used a lot, but fallen out of favor  malloc /

Similar presentations


Presentation on theme: "CSC 213 – Large Scale Programming. Explicit Memory Management  Traditional form of memory management  Used a lot, but fallen out of favor  malloc /"— Presentation transcript:

1 CSC 213 – Large Scale Programming

2 Explicit Memory Management  Traditional form of memory management  Used a lot, but fallen out of favor  malloc / new  Commands used to allocate space for an object  free / delete  Return memory to system using these command  Simple to use

3 Explicit Memory Management  Traditional form of memory management  Used a lot, but fallen out of favor  malloc / new  Commands used to allocate space for an object  free / delete  Return memory to system using these command  Simple to use, but tricky to get right memory leak  Forget to free  memory leak dangling pointer  free too soon  dangling pointer

4 Dangling Pointers Node x = new Node(“happy”);

5 Dangling Pointers Node x = new Node(“happy”); Node ptr = x; Node x = new Node(“happy”); Node ptr = x;

6 Dangling Pointers Node x = new Node(“happy”); Node ptr = x; delete x; // But I’m not dead yet! Node x = new Node(“happy”); Node ptr = x; delete x; // But I’m not dead yet!

7 Dangling Pointers Node x = new Node(“happy”); Node ptr = x; delete x; // But I’m not dead yet! Node y = new Node(“sad”); Node x = new Node(“happy”); Node ptr = x; delete x; // But I’m not dead yet! Node y = new Node(“sad”);

8 Dangling Pointers Node x = new Node(“happy”); Node ptr = x; delete x; // But I’m not dead yet! Node y = new Node(“sad”); cout data << endl; // sad  Node x = new Node(“happy”); Node ptr = x; delete x; // But I’m not dead yet! Node y = new Node(“sad”); cout data << endl; // sad 

9 Dangling Pointers Node x = new Node(“happy”); Node ptr = x; delete x; // But I’m not dead yet! Node y = new Node(“sad”); cout data << endl; // sad  Node x = new Node(“happy”); Node ptr = x; delete x; // But I’m not dead yet! Node y = new Node(“sad”); cout data << endl; // sad 

10 Solution: Garbage Collection  Allocate objects into program’s heap  No relation to other heap used with priority queues  This heap is simply a “pile of memory”  Garbage collector scans objects on heap  Starts with references in local & static variables  Finds reachable objects from those program roots  Calls the unreachable objects are garbage  Cannot be used again, so safe to remove from heap  Need to include free command is eliminated

11 No More Dangling Pointers Node x = new Node(“happy”); Node ptr = x; // x reachable through ptr so cannot reclaim! Node y = new Node(“sad”); cout data << endl; // happy! Node x = new Node(“happy”); Node ptr = x; // x reachable through ptr so cannot reclaim! Node y = new Node(“sad”); cout data << endl; // happy!

12  Static & local references are the roots  Must compute objects in their transitive closure Garbage Collection HEAP

13 Garbage Collection HEAP  Remove unmarked objects from the heap  New objects allocated into empty spaces

14 Why Not Always Use GC?  Garbage collection has obvious benefits  Eliminates some errors that often occurs  Makes programming easier  Several drawbacks created by GC, too couldwill  Reachable objects could, not will, be used again  More memory needed to hold the extra objects  It takes time to compute the reachable objects

15 Why Not Always Use GC?  GC also has several drawbacks  More memory needed to run program than before couldwill  Reachable objects could, not will, be used again  Time needed to find the reachable objects unreachable livedead reachable can be garbage collected free(obj) obj = new Object(); can be explicitly freed free(obj) free(??)

16 Cost of Accessing Memory  How long memory access takes is also important  Will make a major difference in time program takes  Imaginary scenario used to consider this effect:

17 Cost of Accessing Memory  How long memory access takes is also important  Will make a major difference in time program takes  Imaginary scenario used to consider this effect: I want a beer

18 Registers and Caches  Inside the CPU, find first levels of memory  At the lowest level, are processor’s registers

19 Registers and Caches  Inside the CPU, find first levels of memory  At the lowest level, are processor’s registers

20 Registers and Caches  Inside the CPU, find first levels of memory  At the lowest level, are processor’s registers  Very, very fast but…  … number of beers held is limited

21 Registers and Caches  Inside the CPU, find first levels of memory  At the lowest level, are processor’s registers  Use caches at next level for dearest memory

22 Registers and Caches  Inside the CPU, find first levels of memory  At the lowest level, are processor’s registers  Use caches at next level for dearest memory

23 Registers and Caches  Inside the CPU, find first levels of memory  At the lowest level, are processor’s registers  Use caches at next level for dearest memory  More space than registers, but…  … not as fast (must walk across room)  May need more beer if party is good

24 Horrors!  Processor does its best to keep memory local  Caches organized to hold memory needed soon  Makes guesses, since this requires predicting future  Eventually, I will drink all beer in house

25 Horrors!  Processor does its best to keep memory local  Caches organized to hold memory needed soon  Makes guesses, since this requires predicting future  Eventually, I will drink all beer in house

26 Horrors!  Processor does its best to keep memory local  Caches organized to hold memory needed soon  Makes guesses, since this requires predicting future  Eventually, I will drink all beer in house  8MB is largest cache size at the moment  Many programs need more than this  What do we do?

27 No Beer & No TV Make Homer Go Mad  What do you normally do when all beer gone?  Must go to store to get more…  … but do not want a DUI so we must walk to store  Processor uses RAM to store data that cannot fit  RAM sizes are much, much larger than caches  100 x slower to access, however

28 When Store Is Out Of Beer...

29

30 Ein Glass Bier, Bitte  Get passports ready for WALK to Germany  Should find enough beer to handle any situation  But buzz destroyed by the very long weight per glass  If Germany runs out, you're drinking too much

31 Non-Beer Example of Hierarchy but only 3 can fit in RAM Program takes 4 pages, but only 3 can fit in RAM RAM Hard Disk

32 GC follows references from object to object Non-Beer Example of Hierarchy RAM Hard Disk

33 Bringing memory into RAM when references require it Non-Beer Example of Hierarchy RAM Hard Disk

34 But we don't have space, Non-Beer Example of Hierarchy RAM Hard Disk

35 But we don't have space, so another page is evicted Non-Beer Example of Hierarchy RAM Hard Disk

36 But we don't have space, so another page is evicted Non-Beer Example of Hierarchy RAM Hard Disk

37 Now GC can continue Non-Beer Example of Hierarchy RAM Hard Disk

38 Which requires walking to Germany again Non-Beer Example of Hierarchy RAM Hard Disk

39 Which requires walking to Germany again Non-Beer Example of Hierarchy RAM Hard Disk

40 Before GC continues to find the reachable objects Non-Beer Example of Hierarchy RAM Hard Disk

41 And we find that we again walk to Germany Non-Beer Example of Hierarchy RAM Hard Disk

42 Walking To Germany Is Slow… 23.17x 62.73x

43 What Does This Mean?  Large data sets require more thought & care  Start with, but do not end at, big-Oh notation  Consider memory costs and how to limit them  Most data structures do not grow this large  Stack, Queue, Sequence rarely get above 1GB  Using very, very large Graphs is not typical  Databases are largest data sets anywhere  Which data structures & implementations affected?

44 For Next Lecture  Weekly assignment is available on web page  Due at regular time next Thursday  For Monday, must finish program #3  Larger data file available on assignment  Reading on (a,b) trees for Monday


Download ppt "CSC 213 – Large Scale Programming. Explicit Memory Management  Traditional form of memory management  Used a lot, but fallen out of favor  malloc /"

Similar presentations


Ads by Google