Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 8. MEMORY MANAGEMENT SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.

Similar presentations


Presentation on theme: "CHAPTER 8. MEMORY MANAGEMENT SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY."— 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 (2013-1) Understanding of Programming Languages 2

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 (2013-1) Understanding of Programming Languages 3

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 (2013-1) Understanding of Programming Languages 4

5 PROGRAMMER & SYSTEM- CONTROLLED MM (1)  C Programmer controlled memory management malloc(), free() (2013-1) Understanding of Programming Languages 5

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

7 PROGRAMMER & SYSTEM- CONTROLLED MM (3)  System-controlled storage management Reliability Low efficiency (2013-1) Understanding of Programming Languages 7

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 (2013-1) Understanding of Programming Languages 8

9 KINDS OF MEMORY MANAGEMENT  When ? Static memory management (allocation) Dynamic storage management (allocation)  Where ? Stack-based memory management Heap-based storage management (2013-1) Understanding of Programming Languages 9

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 (2013-1) Understanding of Programming Languages 10

11 STATIC STORAGE MANAGEMENT (2)  FORTRAN, COBOL, BASIC All memory is allocated statically  C Allow dynamic memory allocation Static data: for efficiency (2013-1) Understanding of Programming Languages 11

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 (2013-1) Understanding of Programming Languages 12

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

14 DYNAMIC STORAGE MANAGEMENT (1)  Features Allocation during execution Interpreter languages: LISP, SNOBOL4, APL, … Algol-like languages: stack-based allocation  recursion (2013-1) Understanding of Programming Languages 14

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) (2013-1) Understanding of Programming Languages 15

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 (2013-1) Understanding of Programming Languages 16

17 STACK-BASED STORAGE MANAGEMENT (2) Unit of program Block Static nested relations Subprogram Activated by call statement (2013-1) Understanding of Programming Languages 17

18 (2013-1) Understanding of Programming Languages 18 단위 프로그램 구조 unit A A B C D E F G 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 정적 내포 관계 트리 A B C D E F G Algol 유사 언어의 정적 내포 관계 예 Algol 유사 언어의 정적 내포 관계 예

19 STACK-BASED STORAGE MANAGEMENT (3)  Unit activation Structure = code + activation record Activation record Dynamic link (dynamic chain) Static link Return address Referential environment (2013-1) Understanding of Programming Languages 19

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

21 (2013-1) Understanding of Programming Languages 21 A code … call E … E code … call F … F code … call G … G code … call F … Block ABlock EBlock FBlock G Return to system A  E  F  G  F A’s A.R.E’s A.R.F’s A.R.G’s A.R. F’s A.R.

22 (2013-1) Understanding of Programming Languages 22 Stack of activation records A’s activation record E’s activation record F’s activation record G’s activation record F’s activation record Current Static link Dynamic link

23 STACK-BASED STORAGE MANAGEMENT (4)  Block’s memory binding = memory binding for A.R. According to determination time of variable’s size and offset Static binding Binding on activation Dynamic binding (2013-1) Understanding of Programming Languages 23 Layout of A.R.

24 STACK-BASED STORAGE MANAGEMENT (5)  Static binding Size and offset of variables Determined on translation time = determined statically Memory allocation for A.R. On block activation  subprogram call Allocation on every call  recursion Semi-static variable Size, offset: translation time Execution time allocation: activation(execution) time binding for actual address (2013-1) Understanding of Programming Languages 24

25 STACK-BASED STORAGE MANAGEMENT (6) Ex: activation record for Pascal and C (except pointer) (2013-1) Understanding of Programming Languages 25 a : array[0..10] of integer; (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;

26 STACK-BASED STORAGE MANAGEMENT (7)  Binding on activation Size and offset of variables Determined on activation time = determined dynamically At the same time, memory for A.R. is allocated Semi-dynamic variable Size, offset: activation time Execution time allocation: activation(execution) time binding for actual address Unchanged until the activated program (2013-1) Understanding of Programming Languages 26

27 STACK-BASED STORAGE MANAGEMENT (8) 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 (2013-1) Understanding of Programming Languages 27 Information known statically: dimension, … Fixed-size table

28 STACK-BASED STORAGE MANAGEMENT (9) 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 (2013-1) Understanding of Programming Languages 28

29 STACK-BASED STORAGE MANAGEMENT (10) (2013-1) Understanding of Programming Languages 29 Get (M, N) declare A : array(1…N) of INTEGER; B : array(1…M) of FLOAT; begin … end Get (M, N) declare A : array(1…N) of INTEGER; B : array(1…M) of FLOAT; begin … end Example: Ada program

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

31 STACK-BASED STORAGE MANAGEMENT (11)  Dynamic binding Size of activation record Dynamic binding 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 (2013-1) Understanding of Programming Languages 31

32 STACK-BASED STORAGE MANAGEMENT (12) Features of dynamic variables Allocation/free during program execution Keep memory after program termination Cannot be allocated to activation record in stack Heap: specification table is in activation record in stack (2013-1) Understanding of Programming Languages 32 Stack variables: semi-static, semi-dynamic variables Heap variables: dynamic variables

33 STACK-BASED STORAGE MANAGEMENT (13) Ex: PL/I (CONTROLLED, BASED), Pascal (pointer), Ada (access) Dynamic memory allocation (Pascal) (2013-1) Understanding of Programming Languages 33 new(P) : P 는 레코드 t 의 포인터 ① t 형의 기억장소를 힢에 할당 ② 할당된 주소를 P 에 배정 ③ dispose(P) 를 만날때까지 유지 new(P) : P 는 레코드 t 의 포인터 ① t 형의 기억장소를 힢에 할당 ② 할당된 주소를 P 에 배정 ③ dispose(P) 를 만날때까지 유지 P heap stack

34 STACK-BASED STORAGE MANAGEMENT (14)  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 (2013-1) Understanding of Programming Languages 34

35 STACK-BASED STORAGE MANAGEMENT (15)  ALGOL like languages Local variables: activation record of current unit program Non-local variables: static nesting relation (2013-1) Understanding of Programming Languages 35

36 STACK-BASED STORAGE MANAGEMENT (16)  Referential environment for non-local variables Static chain: static nesting relation Search for non-local variables 1)Seek static chain: execution time spending (2013-1) Understanding of Programming Languages 36

37 STACK-BASED STORAGE MANAGEMENT (17) 2)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 (2013-1) Understanding of Programming Languages 37

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

39 (2013-1) Understanding of Programming Languages 39 ALGOL68 의 지역 / 비지역 변수 참조 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 의 “y := x” y : CURRENT + y 의 옵셋 ( 지역변수 ) x : E 활성레코드 시작 + x 의 옵셋 ( 비지역변수 ) F 의 “y := x” y : CURRENT + y 의 옵셋 ( 지역변수 ) x : E 활성레코드 시작 + x 의 옵셋 ( 비지역변수 ) 정적링크 - 단위프로그램 내포구조 표현 동적링크 - 단위프로그램 호출 순서 표현 정적링크 - 단위프로그램 내포구조 표현 동적링크 - 단위프로그램 호출 순서 표현 호출순서 : A → E → F → G → F → G → F F 의 A.R G F G F E A 스택이 증가하는 방향 동적 링크 정적 링크 CURRENT F 의 A.R

40 (2013-1) Understanding of Programming Languages 40 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 Static depth

41 STACK-BASED STORAGE MANAGEMENT (18) 3)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 (2013-1) Understanding of Programming Languages 41 1 3 m DISPLAY current 2... m: DISPLAY 사용 활성 레코드 수 do: display offset,lo: local offset

42 (2013-1) Understanding of Programming Languages 42 program MAIN procedure A; procedure SUB1; end; { SUB1 } procedure SUB2; procedure SUB3; end SUB3; { SUB3 } end; { SUB2 } end A; { A } end. {MAIN} MAIN  A  SUB2  SUB1 MAIN’s A.R. A’s A.R. SUB2’s A.R. 0 1 2 … Display offset = static depth MAIN = 0, A = 1, SUB1 = 2 SUB2 = 2, SUB3 = 3 MAIN’s A.R. A’s A.R. SUB2’s A.R. 0 1 2 …SUB1’s A.R.

43 (2013-1) Understanding of Programming Languages 43 MAIN’s A.R. A’s A.R. SUB2’s A.R. 0 1 2 … MAIN’s A.R. A’s A.R. SUB2’s A.R. 0 1 2 3SUB3’s A.R. … MAIN’s A.R. A’s A.R. SUB2’s A.R. 0 1 2 3SUB3’s A.R. …SUB1’s A.R.

44 HEAP-BASED STORAGE MANAGEMENT (1)  Heap Memory block Free allocation and de-allocation  Problems Allocation, recovery, compaction, reuse Collection technique (2013-1) Understanding of Programming Languages 44

45 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 (2013-1) Understanding of Programming Languages 45

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

47 (2013-1) Understanding of Programming Languages 47 void main() { int *x, k = 1; double *y; … x = malloc(sizeof(int)); y = malloc(sizeof(double)); … free(x); free(y); } main() 의 활성 레코드 x k y 7 1 20 heap 20: 7: stack

48 HEAP-BASED STORAGE MANAGEMENT (3)  Heap memory management Fixed-size elements allocation Simple No compaction Variable-size elements allocation (2013-1) Understanding of Programming Languages 48

49 HEAP-BASED STORAGE MANAGEMENT (4)  Fixed-size elements Heap K elements Block: N word size Heap size = N  K Free-space list (2013-1) Understanding of Programming Languages 49

50 HEAP-BASED STORAGE MANAGEMENT (5) Allocation Allocate the first element  removed from the list Free Link to head of the free list (2013-1) Understanding of Programming Languages 50 (a) Initial free-space list Head of free list (b) Free-space after execution Head of free list

51 HEAP-BASED STORAGE MANAGEMENT (6) Recovery Explicit recovery by programmer or system dispose (PASCAL), free (C), delete (C++, Java) Garbage, dangling references (2013-1) Understanding of Programming Languages 51 Garbage int *p, *q; … p = malloc(sizof(int)); p = q; Garbage int *p, *q; … p = malloc(sizof(int)); p = q; Dangling References int *p, *q; … p = malloc(sizof(int)); q = p; free(p); Dangling References int *p, *q; … p = malloc(sizof(int)); q = p; free(p);

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 (2013-1) Understanding of Programming Languages 52

53 HEAP-BASED STORAGE MANAGEMENT (8)  Variable-size elements More difficult Problem: reuse Reuse algorithm First-fit Best-fit Fragmentation Partial compaction Full compaction (2013-1) Understanding of Programming Languages 53


Download ppt "CHAPTER 8. MEMORY MANAGEMENT SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY."

Similar presentations


Ads by Google