Presentation is loading. Please wait.

Presentation is loading. Please wait.

Storage.

Similar presentations


Presentation on theme: "Storage."— Presentation transcript:

1 Storage

2 Where do programs get memory?
Three types Static Automatic Dynamic

3 Static Memory Each corresponds to a single variable in the program
Not all variables, only Globals In C, local variables declared with the static keyword Lifetime Created/initialized on program start Deallocated on program termination

4 Static Memory Advantages No runtime cost for memory management
Disadvantages Fixed amount of memory can not grow or shrink

5 Static Memory int x = 42; int a[3] = {1, 2, 3}; char *msg = "Hello";
void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0;

6 Static Memory int x = 42; int a[3] = {1, 2, 3}; char *msg = "Hello";
void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0;

7 Static Memory int x = 42; int a[3] = {1, 2, 3}; char *msg = "Hello";
void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0;

8 Static Memory Output: 10 20 int x = 42; int a[3] = {1, 2, 3};
char *msg = "Hello"; void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0; Output: 10 20

9 Static Memory Output: 10 20 int x = 42; int a[3] = {1, 2, 3};
char *msg = "Hello"; void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0; Output: 10 20

10 Static Memory Output: 10 20 int x = 42; int a[3] = {1, 2, 3};
char *msg = "Hello"; void f() { static int z = 10; printf("%s\n",z); z = 20; } int main() { f(); return 0; Output: 10 20

11 Automatic Variables Local variables of functions
Reside in the call stack Each call gets its own instance Even recursive calls Lifetime Allocated when the function is called Deallocated when the function returns

12 Automatic Variables void g() { int k[3]; int m; } void f(int i) {
int j; (i>0) ? f(i-1) : g(); int main() { int x; f(1); return 0;

13 Automatic Variables void g() { int k[3]; int m; } void f(int i) {
int j; (i>0) ? f(i-1) : g(); int main() { int x; f(1); return 0;

14 Automatic Variables void g() { int k[3]; int m; } void f(int i) {
int j; (i>0) ? f(i-1) : g(); int main() { int x; f(1); return 0;

15 Automatic Variables void g() { int k[3]; int m; } void f(int i) {
int j; (i>0) ? f(i-1) : g(); int main() { int x; f(1); return 0;

16 Dynamic Memory Allocated in response to an instruction
Explicit: new operator or malloc call Implicit: appending to lists in Python Reside on the heap Lifecycle Allocated on demand Freed on demand

17 Dynamic Memory Dynamic memory is basically required for non-trivial programs Responding to external events User input, etc. Reading files of arbitrary size

18 Dynamic Memory But it has a dark side
The programmer can forget to free the memory "Memory Leak" Or worse, memory can be accessed after it is freed "Dangling Pointers" These bugs can take a long time to track down because they don't necessarily fail right away.

19 Solution ! Take the responsibility for managing dynamic memory out of the hands of the developer Automatically free memory when it is no longer accessible to the program Memory can't be leaked (permanently, at least) Dangling pointers can't exist (by definition)

20 Two approaches Reference Counting Garbage Collection

21 Reference Counting Conceptually simple
Every object is augmented with a count of the number of references pointing to it. Whenever a new reference is added, the count increases Whenever a reference is lost, the count decreases If the count reaches zero, the object is freed

22 Reference Counting Major Flaw Self-referential objects

23 An example in Python x = SomeClass()

24 An example in Python x = SomeClass() x.ref = x

25 An example in Python x = SomeClass() x.ref = x x=y

26 The object will never be freed!
x = SomeClass() x.ref = x x=y

27 Garbage Collection Wait until memory is exhausted and then clean up any memory that can't be reached by any variables in the program "Mark-and-sweep" Initially mark all objects as "garbage" Walk the graph of references from the program variables, through anything reachable from those variables, and so on, marking anything reachable as "not garbage" Free anything still marked as "garbage"

28 Advantage The memory from the Python example will be freed

29 Disadvantage Waiting until all memory is exhausted causes very noticeable pauses in the program execution Whereas reference-counting interleaves object destruction with the normal execution

30 Workaround Observation: there are many short-lived objects and relatively few long-lived ones Generational garbage collection creates new objects in a "nursery" that is a smaller pool of memory for initial object allocation. Once it is exhausted, it is garbage collected as before, but because it is smaller, the effect is less noticeable.

31 Workaround Combining reference counting with garbage collection can provide the best of both approaches This is actually how Python works. It periodically checks for isolated cycles any container that can produce a reference cycle must support cycle detection


Download ppt "Storage."

Similar presentations


Ads by Google