Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8. Memory Management

Similar presentations


Presentation on theme: "Chapter 8. Memory Management"— Presentation transcript:

1 Chapter 8. Memory Management
Sung-Dong Kim Dept. of Computer Engineering, Hansung University

2 Contents Elements requiring memory
Who is responsible for memory management Steps of memory management Kinds of memory management methods (2012-1) Fundamentals of Programming Languages

3 1. Elements requiring memory (1)
Code segments User data User-defined data structures and constants Subprogram execution Activation records Subprogram return points Referencing environments (2012-1) Fundamentals of Programming Languages

4 1. Elements requiring memory (2)
Memory space needed during program execution new, delete, … Other system data Temporaries in expression evaluation Temporaries in parameter transmission Input-output buffers Reference counts Garbage collection bits (2012-1) Fundamentals of Programming Languages

5 Programmer & System-controlled MM (1)
Programmer controlled memory management malloc(), free() (2012-1) Fundamentals of Programming Languages

6 Programmer & System-controlled MM (2)
Programmer-controlled storage management Problem Risk: garbage, dangling reference Interference to system’s memory management routine Programmer can not manage all memory Temporaries Subprogram return points Other system data Advantages System can not know the time when the memory is allocated and freed Programmers knows the exact time for memory allocation and free (2012-1) Fundamentals of Programming Languages

7 Programmer & System-controlled MM (3)
System-controlled storage management Reliability Low efficiency Dilemma Strong typing and effective storage-management  low performance Storage management and execution speed  high risk Performance  Risk ? (2012-1) Fundamentals of Programming Languages

8 Steps of Memory Management
Initial allocation Recovery For reuse  free state Adjustment of stack pointer: simple Garbage collection: very complex Compaction and reuse Compaction: large empty memory block Reuse (2012-1) Fundamentals of Programming Languages

9 Kinds of Memory Management
When ? Static memory management (allocation) Dynamic storage management (allocation) Where ? Stack-based memory management Heap-based storage management (2012-1) Fundamentals of Programming Languages

10 Static Storage Management (1)
Features Allocation during translation = allocation at the time of program execution No change during execution Code segment No recovery and reuse  no run-time storage management Efficient No time, space is spent during execution  only execute the program (2012-1) Fundamentals of Programming Languages

11 Static Storage Management (2)
FORTRAN, COBOL, BASIC All memory is allocated statically C Allow dynamic memory allocation Static data: for efficiency (2012-1) Fundamentals of Programming Languages

12 Static Storage Management (3)
Advantages Simple Easy to implement Disadvantages Flexibility: no dynamic array, no recursion Memory spending: memory allocation for all sub-programs and error handling routines (2012-1) Fundamentals of Programming Languages

13 FORTRAN program 전역 변수 (COMMON ) 코드부 1 활성 레코드 단위 프로그램 2 K . . .
(주 프로그램) FORTRAN program (2012-1) Fundamentals of Programming Languages

14 Dynamic storage management (1)
Features Allocation during execution Interpreter languages: LISP, SNOBOL4, APL, … Algol-like languages: stack-based allocation  recursion (2012-1) Fundamentals of Programming Languages

15 Dynamic storage management (2)
Static-dynamic storage management ALGOL Own variable: static allocation Others: dynamic allocation  recursion PL/I STATIC: static allocation AUTOMATIC: dynamic allocation (stack-based) CONTROLED, BASED: dynamic allocation (heap-based) (2012-1) Fundamentals of Programming Languages

16 Stack-based storage management (1)
PL with dynamic allocation Compiler-based (block structure) languages: ALGOL, PASCAL, C, Java, … Interpreter-based languages: APL, LISP, SNOBOL, PROLOG, … Algol-like languages Block structure Declaration statement: new environment Limit the scope of variables (2012-1) Fundamentals of Programming Languages

17 Stack-based storage management (2)
Unit of program Block Static nested relations Subprogram Activated by call statement (2012-1) Fundamentals of Programming Languages

18 A B C D E F G 단위 프로그램 구조 정적 내포 관계 트리 Algol 유사 언어의 정적 내포 관계 예 A unit A
end A unit B unit C unit D end D end C end B unit G unit F unit E end E end G end F Algol 유사 언어의 정적 내포 관계 예 (2012-1) Fundamentals of Programming Languages

19 Stack-based storage management (3)
Unit activation Structure = code + activation record Activation record Dynamic link (dynamic chain) Static link Return address Referential environment (2012-1) Fundamentals of Programming Languages

20 Stack-based storage management (4)
Types of memory binding Static binding Binding on activation Dynamic binding (2012-1) Fundamentals of Programming Languages

21 Block’s Activation 코드부 (Code segment) Activation Record Return address
Dynamic link Static link Environment (local variables, Parameters) (2012-1) Fundamentals of Programming Languages

22 A  E  F  G  F Block A Block E Block F Block G A code … call E
E code call F F code call G G code call F A’s A.R. E’s A.R. F’s A.R. G’s A.R. F’s A.R. Return to system A  E  F  G  F (2012-1) Fundamentals of Programming Languages

23 Stack of activation records
Current F’s activation record G’s activation record Static link Dynamic link F’s activation record E’s activation record A’s activation record Stack of activation records (2012-1) Fundamentals of Programming Languages

24 Stack-based storage management (5)
Static binding Size and offset of variables Determined on translation time Size of activation record is determined statically Size of memory is determined on translation time Offset: static binding Generation of local variables Activation time of unit program Location of memory is determined (2012-1) Fundamentals of Programming Languages

25 Stack-based storage management (6)
Activation record Allocation on every call  recursion Semi-static variable Size, offset: translation time Execution time allocation: execution time binding for actual address (2012-1) Fundamentals of Programming Languages

26 Stack-based storage management (7)
Ex: activation record for Pascal and C (except pointer) a : array[0..10] of interger; (int a[11]; // C) - size, offset: static binding - location of activation record (x): execution time - effective address = dynamic binding loc(a[i]) = x + a’s offset + i * s; (2012-1) Fundamentals of Programming Languages

27 Stack-based storage management (8)
Binding on activation Generation of local variables Activation time of unit program Size of memory is determined Offset: determined on execution time Semi-dynamic variable Size, offset: activation time Execution time allocation: execution time binding for actual address Unchanged until the activated program (2012-1) Fundamentals of Programming Languages

28 Stack-based storage management (9)
Dynamic array Size of array is determined on activation time  user flexibility ALGOL, Ada, … Memory allocation of semi-dynamic variable Translation time: specification table for semi-dynamic variable  activation record Information known statically: dimension, … Fixed-size table (2012-1) Fundamentals of Programming Languages

29 Stack-based storage management (10)
Execution time Allocation for activation record Semi-dynamic variables Specification table Memory allocation for semi-dynamic variables Assign memory address of semi-dynamic variables to specification table make the offset constant (2012-1) Fundamentals of Programming Languages

30 Stack-based storage management (11)
Example: Ada program Get (M, N) declare A : array(1…N) of INTEGER; B : array(1…M) of FLOAT; begin end (2012-1) Fundamentals of Programming Languages

31 단위 프로그램의 활성 레코드 B 배열 (M) 1 (N) U 의 일부 준정적 변수 동적 링크 반환 주소 B에 대한 명세표 스택이
증가하는 방향 A A에 대한 명세표 U의 나머지 준정적 변수 준동적 변수가 있는 전형적인 활성 레코드 (2012-1) Fundamentals of Programming Languages

32 Stack-based storage management (12)
Dynamic binding Size of activation record Change during execution Dynamic variables Size is variable during execution Example C, C++, Java: heap dynamic array Variables whose creation time is specified in the program (2012-1) Fundamentals of Programming Languages

33 Stack-based storage management (13)
Features of dynamic variables Allocation/free during program execution Keep memory after program termination Can not be allocated to activation record in stack Heap: specification table is in activation record in stack Stack variables: semi-static, semi-dynamic variables Heap variables: dynamic variables (2012-1) Fundamentals of Programming Languages

34 Stack-based storage management (14)
Ex: PL/I (CONTROLLED, BASED), Pascal (pointer), Ada (access) Dynamic memory allocation (Pascal) P heap stack new(P) : P는 레코드 t의 포인터 ① t형의 기억장소를 힢에 할당 ② 할당된 주소를 P에 배정 ③ dispose(P)를 만날때까지 유지 (2012-1) Fundamentals of Programming Languages

35 Stack-based storage management (15)
Reference for non-local variables Reference variables in other activation record Local variables: local environment Non-local variables: non-local environment FORTRAN Local variables: activation record of current unit program Global variables: system-provided activation record (2012-1) Fundamentals of Programming Languages

36 Stack-based storage management (16)
ALGOL like languages Local variables: activation record of current unit program Non-local variables: static nesting relation (2012-1) Fundamentals of Programming Languages

37 Stack-based storage management (17)
Referential environment for non-local variables Static chain: static nesting relation Search for non-local variables Seek static chain: execution time spending (2012-1) Fundamentals of Programming Languages

38 Stack-based storage management (18)
Chain offset (distance) Chain offset: difference of static nesting level Difference of static depth Ex: Distance at D Local variables (declared in D) = 0 Non-local variables (declared in B) = 1 Non-local variables (declared in A) = 2 Variable address: (co, lo) co: chain offset lo: local offset Long distance: time spending (2012-1) Fundamentals of Programming Languages

39 • A B D C . . . X선언 y선언 Z선언 프로그램 구조와 활성 레코드 상태 D의 활성 레코드 CURRENT
실행중 A의 활성 레코드 B의 활성 레코드 C의 활성 레코드 D의 활성 레코드 정적 링크 CURRENT 프로그램 구조와 활성 레코드 상태 (b) (a)에서 ABCD 순으로 호출 실행 상태에서 있는 활성 레코드들의 스택 상태 (a) 한 프로그램 구조 예 (2012-1) Fundamentals of Programming Languages

40 ALGOL68의 지역/비지역 변수 참조 <단위 프로그램 구조> <활성레코드 실행 상태>
호출순서 : A →E →F →G →F →G →F unit A x 선언 end A unit B end B unit E x 선언 end E unit C end C unit D end D unit F y 선언 y := x end F unit G x 선언 end G F 의 A.R CURRENT 정적링크 - 단위프로그램 내포구조 표현 동적링크 - 단위프로그램 호출 순서 표현 G 의 A.R 정적 링크 F 의 A.R 동적 링크 G 의 A.R F F의 “y := x” y : CURRENT + y의 옵셋 (지역변수) x : E 활성레코드 시작 + x의 옵셋 (비지역변수) F 의 A.R E 의 A.R A 의 A.R 스택이 증가하는 방향 (2012-1) Fundamentals of Programming Languages

41 Static depth A: static depth = 0 B: static depth = 1
D: static depth = 3 C: static depth = 2 B: static depth = 1 A: static depth = 0 F: static depth = 2 E: static depth = 1 G: static depth = 2 (2012-1) Fundamentals of Programming Languages

42 Stack-based storage management (19)
Display Array for representing static chain relation Method (do, lo) Effective address (do, lo): DISPLAY(do) + lo Advantage/disadvantage Same reference time for all non-local variables Update display on creation/deletion of activation records 1 3 m DISPLAY current 2 ... m: DISPLAY 사용 활성 레코드 수 do: display offset, lo: local offset (2012-1) Fundamentals of Programming Languages

43 MAIN  A  SUB2  SUB1 Display offset = static depth
program MAIN procedure A; procedure SUB1; end; { SUB1 } procedure SUB2; procedure SUB3; end SUB3; { SUB3 } end; { SUB2 } end A; { A } end. {MAIN} MAIN’s A.R. A’s A.R. SUB2’s A.R. 1 2 MAIN’s A.R. A’s A.R. SUB2’s A.R. 1 2 SUB1’s A.R. (2012-1) Fundamentals of Programming Languages

44 MAIN’s A.R. A’s A.R. SUB2’s A.R. 1 2 … MAIN’s A.R. A’s A.R.
1 2 MAIN’s A.R. A’s A.R. SUB2’s A.R. 1 2 3 SUB3’s A.R. SUB1’s A.R. MAIN’s A.R. A’s A.R. SUB2’s A.R. 1 2 3 SUB3’s A.R. (2012-1) Fundamentals of Programming Languages

45 Heap-based storage management (1)
Memory block Free allocation and de-allocation Problems Allocation, recovery, compaction, reuse Collection technique (2012-1) Fundamentals of Programming Languages

46 Heap-based storage management (2)
Necessity for heap Memory allocation and free at random time during execution Data structure creation, destruction, expansion at random position in program (2012-1) Fundamentals of Programming Languages

47 X 부 프로그램의 N 활성 레코드 R CURRENT Y 부 프로그램의 Heap 활성 레코드 I Y 주 프로그램의 활성 레코드
Z 스택이 증가하는 방향 (2012-1) Fundamentals of Programming Languages

48 Heap-based storage management (3)
Heap memory management Fixed-size elements allocation Simple No compaction Variable-size elements allocation (2012-1) Fundamentals of Programming Languages

49 Heap-based storage management (4)
Fixed-size elements Heap K elements Block: N word size Heap size = N  K Free-space list (2012-1) Fundamentals of Programming Languages

50 Heap-based storage management (5)
Allocation Allocate the first element  removed from the list Free Link to head of the free list Head of free list Head of free list (b) Free-space after execution (a) Initial free-space list (2012-1) Fundamentals of Programming Languages

51 Heap-based storage management (6)
Recovery Explicit recovery by programmer or system dispose (PASCAL), free (C), delete (C++, Java) Garbage, dangling references Garbage int *p, *q; p = malloc(sizof(int)); p = q; Dangling References int *p, *q; p = malloc(sizof(int)); q = p; free(p); (2012-1) Fundamentals of Programming Languages

52 Heap-based storage management (7)
Reference counts Check pointer to elements  check dangling reference Extra space for counts Garbage collection When there are no free space, stop computation and start garbage collection 2 steps Mark: garbage (ON), Active element (OFF) Sweep: “ON” element  free list (2012-1) Fundamentals of Programming Languages

53 Heap-based storage management (8)
Variable-size elements More difficult Problem: reuse Reuse algorithm First-fit Best-fit Fragmentation Partial compaction Full compaction (2012-1) Fundamentals of Programming Languages


Download ppt "Chapter 8. Memory Management"

Similar presentations


Ads by Google