Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.

Similar presentations


Presentation on theme: "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management."— Presentation transcript:

1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management

2 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Memory management Memory management is the process of binding values to memory, taking into account both the static and dynamic characteristics of these values.

3 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Static Memory is allocated to values whose storage requirements are known before run time and remain constant throughout the life of the running program. 1 The run-time stack is the center of control for dispatching active methods (procedures and functions), their local variables and their parameter-argument linkage. 1 The heap contains all other values that are dynamically allocated during the run-time life of the program. The heap is the least structured of the three memory categories.

4 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-4 The Structure of Run-Time Memory The invariant 0 <= a <=h <n avoids stack overflow

5 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-5 How to represent the run-time memory? 1 The locations (addresses) are taken from {0,...,n}. They define the address space.  The environment of an active method m (gamma m,  m ) is a set of pairs that unite those variables which are accessible to m with specific memory addresses.  The memory (mu,  ) is the set of pairs that relate locations with values.

6 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-6  The state (sigma m,  m ) of an active method m is the product  m x . So  m (x) =  m (x)).  The address of a variable v is  m (v).  The value of a variable v is  m (v). 1 This framework permits to define the memory and operations on the memory (declaration, assignment, allocation, deallocation...).

7 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-7 Structure of a Called Method’s Stack Frame Figure 5.2

8 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-8 Static and dynamic links 1 The static link points to the static area. 1 The dynamic link points to the stack frame of the method that called the considered method.

9 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-9 Scope of a variable 1 The scope of a variable determines where a variable can be used. 1 Static scoping means that the scope of each variable is determined by the program's static structure (using the static links). 1 Dynamic scoping means that the scope of each variable is determined by the sequence of method's calls that occurs at run time (using the dynamic links).

10 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-10 Example Program with Methods and Parameters Figure 5.3

11 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-11 Run-Time Stack with Stack Frames for Method Invocations Figure 5.4

12 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-12 By value or by reference 1 Passing an argument by value means that the value of the argument is obtained at the time of the call and placed in a memory location for the corresponding parameter. 1 Passing an argument by reference means that the address of the argument is associated with the corresponding parameter. 1 Example: The argument of parameter w is passed by reference inside the method B.

13 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-13 Passing an Argument by Reference Example Figure 5.5

14 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-14 Arrays and records 1 Arrays are ordered sequences of values that share the same types. 1 Records are finite collection of values that have different types.

15 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-15 Dynamic Memory Allocation for the One-Dimensional Array A Figure 5.7 int[] A = new int[10];  (A[i]) =  (A[0])+e*i 0 <= i < n

16 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-16 Memory Allocation for Two-Dimensional Array C Figure 5.8 char[][] C = new char[10];  (C[i][j]) =  (C[0][0])+e*(ni+j) 0 <= i < m and 0 <=j < n

17 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-17 Sample Structure Representing an Individual Employee Figure 5.9 In C language: typedef struct { int id; char[25] name; int age; float salary; char dept; } employee Type; employeeType employee;

18 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-18 Memory leaks and garbage collection 1 Garbage collection is the strategy to reclaim those blocks of memory that contain "no longer needed values", so that they can be reused by other objects that require heap space. 1 Example: class node {int value, node next}; node p, q; p = new node(); q = new node(); q = p;

19 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-19 Creating Widows and Orphans: A Simple Example Figure 5.13

20 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-20 Garbage collector algorithms 1 Reference counting 1 Mark-Sweep 1 Copy collection 1 In JAVA: The garbage collector runs as a low-priority process called a thread. It is automatic but it can be explicitly called by: System.gc() (regardless of the state of the heap at the time of the call).

21 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-21 Node Structure and Example Heap for Reference Counting Figure 5.14 The initial heap is a continuous chain of nodes called the free_list. Each node has an additional integer field that contains a count of the number of pointers referencing that node (initially 0).

22 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-22 Reference counting algorithm - limitations p->next = null; It fails to return to the free_list any garbage that occurs in the form of isolated circular chain.

23 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-23 Node Structure and Example for Mark-Sweep Algorithm Figure 5.15

24 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-24 Heap after Pass I of Mark-Sweep Figure 5.16

25 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-25 Heap after Pass II of Mark-Sweep Figure 5.17

26 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-26 Initial Heap Organization for Copy Collection Figure 5.18

27 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 5-27 Result of a Copy Collection Activation Figure 5.19


Download ppt "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management."

Similar presentations


Ads by Google