Chapter 8 Memory Management SungDong Kim School of

  • Slides: 53
Download presentation
Chapter 8. Memory Management Sung-Dong Kim School of Computer Engineering, Hansung University

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

Contents • 메모리를 필요로 하는 요소들 • 누가 memory management를 하는가? • 기억 장소

Contents • 메모리를 필요로 하는 요소들 • 누가 memory management를 하는가? • 기억 장소 관리(memory management) 단계 • memory management 방법의 종류 2 (2019 -1) Understanding of Programming Languages

1. Elements requiring memory (1) • Code segments • User data —User-defined data structures

1. Elements requiring memory (1) • Code segments • User data —User-defined data structures and constants —Activation records • Subprogram 실행 —Subprogram 복귀 주소 —참조 환경 (referencing environments) 주소 3 (2019 -1) Understanding of Programming Languages

1. Elements requiring memory (2) • 프로그램 실행 중 필요한 메모리 – heap —new,

1. Elements requiring memory (2) • 프로그램 실행 중 필요한 메모리 – heap —new, delete, … • 다른 system data —수식 계산을 위한 임시 저장소 —매개변수 전달을 위한 임시 저장소 —Input-output buffers —참조 계수 (reference counts) —쓰레기 수집 비트 (garbage collection bits) 4 (2019 -1) Understanding of Programming Languages

Programmer & System-controlled MM (1) • C —Programmer controlled memory management —malloc(), free() 5

Programmer & System-controlled MM (1) • C —Programmer controlled memory management —malloc(), free() 5 (2019 -1) Understanding of Programming Languages

Programmer & System-controlled MM (2) • Programmer-controlled storage management —Problem –Risk: garbage, dangling reference

Programmer & System-controlled MM (2) • Programmer-controlled storage management —Problem –Risk: garbage, dangling reference –System의 메모리 관리 방해 가능성 –프로그래머가 모든 메모리를 관리할 수 없음 +임시 저장소 +Subprogram 복귀 주소(return points) +다른 시스템 데이터 +… 6 (2019 -1) Understanding of Programming Languages

Programmer & System-controlled MM (3) —Advantages –System은 메모리 할당/해제의 정확한 시점을 모름 –Programmers는 메모리

Programmer & System-controlled MM (3) —Advantages –System은 메모리 할당/해제의 정확한 시점을 모름 –Programmers는 메모리 할당/해제의 정확한 시점을 알고 있음 7 (2019 -1) Understanding of Programming Languages

Programmer & System-controlled MM (4) • System-controlled storage management —신뢰성 (reliability) —낮은 효율성 (low

Programmer & System-controlled MM (4) • System-controlled storage management —신뢰성 (reliability) —낮은 효율성 (low efficiency) 8 (2019 -1) Understanding of Programming Languages

Steps of Memory Management • 초기 할당 (initial allocation) • 복구 (recovery) —재사용을 위함

Steps of Memory Management • 초기 할당 (initial allocation) • 복구 (recovery) —재사용을 위함 free state —Stack: stack pointer만 변경 simple —Garbage collection: very complex • 압축/재사용 (compaction and reuse) —압축: 큰 빈 메모리 블록 만들기 —재사용 9 (2019 -1) Understanding of Programming Languages

Kinds of Memory Management • When ? —Static(정적) memory management (allocation) —Dynamic(동적) storage management

Kinds of Memory Management • When ? —Static(정적) memory management (allocation) —Dynamic(동적) storage management (allocation) • Where ? —Stack-based memory management —Heap-based storage management 10 (2019 -1) Understanding of Programming Languages

Static Storage Management (2) • FORTRAN, COBOL, BASIC —모든 메모리는 정적으로 (statically) 할당됨 •

Static Storage Management (2) • FORTRAN, COBOL, BASIC —모든 메모리는 정적으로 (statically) 할당됨 • C —동적 (dynamic) memory allocation 허용 —효율을 위해 정적 (static) data도 가능 12 (2019 -1) Understanding of Programming Languages

Static Storage Management (3) • Advantages —Simple —구현이 쉬움 • Disadvantages —융통성 없음: no

Static Storage Management (3) • Advantages —Simple —구현이 쉬움 • Disadvantages —융통성 없음: no dynamic array, no recursion —메모리 낭비: 실행되지 않은 코드 (예: 에러 처리 루틴, 호 출되지 않는 서브프로그램 등)/데이터를 위한 메모리 공간 도 미리 할당함 13 (2019 -1) Understanding of Programming Languages

Dynamic storage management (1) • 특징 —실행 (execution) 할당 —Interpreter languages: LISP, SNOBOL 4,

Dynamic storage management (1) • 특징 —실행 (execution) 할당 —Interpreter languages: LISP, SNOBOL 4, APL, … —Algol-like languages: 스택에 할당 recursion 가능 15 (2019 -1) Understanding of Programming Languages

Dynamic storage management (2) • 스택 동적 (static-dynamic) 기억 장소 관리 • ALGOL —Own

Dynamic storage management (2) • 스택 동적 (static-dynamic) 기억 장소 관리 • ALGOL —Own variable: 정적 할당 —Others: 동적 할당 recursion 가능 • PL/I —STATIC: static allocation —AUTOMATIC: dynamic allocation (stack-based) —CONTROLED, BASED: dynamic allocation (heap-based) 16 (2019 -1) Understanding of Programming Languages

Stack-based storage management (1) • 동적 할당하는 프로그래밍 언어 —Compiler-based (블록 구조) languages: ALGOL,

Stack-based storage management (1) • 동적 할당하는 프로그래밍 언어 —Compiler-based (블록 구조) languages: ALGOL, PASCAL, C, Java, … —Interpreter-based languages: APL, LISP, SNOBOL, PROLOG, … • Algol-like languages —블록 구조 –선언문 (declaration statement) : 새로운 환경 시작 –변수의 scope를 제한함 17 (2019 -1) Understanding of Programming Languages

Stack-based storage management (2) —프로그램 단위 –Block +정적 포함 관계 –Subprogram +호출문에 의해 활성화

Stack-based storage management (2) —프로그램 단위 –Block +정적 포함 관계 –Subprogram +호출문에 의해 활성화 됨 18 (2019 -1) Understanding of Programming Languages

단위 프로그램 구조 A 정적 내포 관계 트리 unit A B A unit B

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

Stack-based storage management (3) • 단위 활성화 (unit activation) — code + activation record

Stack-based storage management (3) • 단위 활성화 (unit activation) — code + activation record — Activation record –동적 링크 (dynamic link) = 동적 체인 (dynamic chain) –정적 링크 (static link) –복귀 주소 (return address) –참조 환경 (referential environment) 20 (2019 -1) Understanding of Programming Languages

Block’s Activation 코드부 (Code segment) Activation Record Return address Dynamic link Static link Environment

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

Block A A code … call E … Block E E code … call

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

Current Static link F’s activation record G’s activation record Dynamic link F’s activation record

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

Stack-based storage management (6) —예: 활성화 레코드 a : array[0. . 10] of integer;

Stack-based storage management (6) —예: 활성화 레코드 a : array[0. . 10] of integer; (int a[11]; // C) - 크기, 변위: static binding - 활성 레코드 위치 (x): 실행 시간 - 실제 주소 (effective address) = dynamic binding loc(a[i]) = x + a의 변위 + i * s; 26 (2019 -1) Understanding of Programming Languages

Stack-based storage management (10) Example: Ada program Get (M, N) declare A : array(1…N)

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

Stack-based storage management (12) —동적 변수의 특징 –실행 중 메모리 할당/해제 –프로그램 종료 후에도

Stack-based storage management (12) —동적 변수의 특징 –실행 중 메모리 할당/해제 –프로그램 종료 후에도 메모리 유지 가능 (해제 안하면) –블록의 활성 레코드에 메모리를 유지할 수 없음 –Heap Stack variables: semi-static, semi-dynamic variables Heap variables: dynamic variables 33 (2019 -1) Understanding of Programming Languages

Stack-based storage management (13) • 비지역 변수 (non-local variables) 참조 —다른 활성화 레코드에 있는

Stack-based storage management (13) • 비지역 변수 (non-local variables) 참조 —다른 활성화 레코드에 있는 변수 참조 • FORTRAN —Local variables: 현재 프로그램의 activation record —Global variables: 시스템 제공 (system-provided) activation record 34 (2019 -1) Understanding of Programming Languages

Stack-based storage management (14) • ALGOL 계열 언어들 —Local variables: 현재 프로그램의 activation record

Stack-based storage management (14) • ALGOL 계열 언어들 —Local variables: 현재 프로그램의 activation record 내 에서 변수를 찾음 —Non-local variables: 정적 내포 관계 (static nesting relation)를 이용하여 변수를 찾음 35 (2019 -1) Understanding of Programming Languages

Static depth A: static depth = 0 B: static depth = 1 C: static

Static depth A: static depth = 0 B: static depth = 1 C: static depth = 2 D: static depth = 3 E: static depth = 1 F: static depth = 2 G: static depth = 2 40 (2019 -1) Understanding of Programming Languages

Stack-based storage management (18) —디스플레이 (display) – 정적 체인 관계를 나타내는 배열 – 방법

Stack-based storage management (18) —디스플레이 (display) – 정적 체인 관계를 나타내는 배열 – 방법 + (do=display offset, lo=local offset) current DISPLAY m + 유효주소 (do, lo): DISPLAY(do) + lo . . . 3 2 1 m: DISPLAY 사용 활성 레코드 수 do: display offset, lo: local offset – Advantage/disadvantage + 모든 비지역 변수를 같은 시간에 참조할 수 있음 (모든 비지역 변수를 찾는 시간 이 동일함) + 활성화 레코드 생성/삭제 할 때마다 display 값을 수정해야 함 41 (2019 -1) Understanding of Programming Languages

MAIN A SUB 2 SUB 1 Display offset = static depth MAIN = 0,

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

MAIN A SUB 2 SUB 3 SUB 2’s A. R. A’s A. R. MAIN’s

MAIN A SUB 2 SUB 3 SUB 2’s A. R. A’s A. R. MAIN’s A. R. … 2 1 0 SUB 3’s A. R. SUB 2’s A. R. … 3 2 A’s A. R. 1 MAIN’s A. R. 0 43 MAIN A SUB 2 SUB 3 SUB 2’s A. R. SUB 3’s A. R. … 3 SUB 2’s A. R. A’s A. R. MAIN’s A. R. 2 1 0 (2019 -1) Understanding of Programming Languages

Heap-based storage management (1) • Heap —메모리 블록 —자유롭게 할당(allocation )/해제(de-allocation) • Problems —Allocation,

Heap-based storage management (1) • Heap —메모리 블록 —자유롭게 할당(allocation )/해제(de-allocation) • Problems —Allocation, recovery, compaction, reuse 등을 위한 기술이 필요 —기억 장소 수집 (collection technique) 기술도 필요 44 (2019 -1) Understanding of Programming Languages

void main() { int *x, k = 1; double *y; … x = malloc(sizeof(int));

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 stack 47 (2019 -1) Understanding of Programming Languages 7: heap 20 :

Heap-based storage management (3) • 힢 메모리 관리 —고정 크기 기억 장소 할당 (fixed-size

Heap-based storage management (3) • 힢 메모리 관리 —고정 크기 기억 장소 할당 (fixed-size elements allocation) –Simple –No compaction —가변 크기 기억 장소 할당 (variable-size elements allocation) 48 (2019 -1) Understanding of Programming Languages

Heap-based storage management (4) • 고정 크기 기억 장소 할당 —Heap –K 블록 –Block:

Heap-based storage management (4) • 고정 크기 기억 장소 할당 —Heap –K 블록 –Block: N word size –Heap 크기 = N K —사용 가능 블록 리스트 (free-space list) 유지 49 (2019 -1) Understanding of Programming Languages

Heap-based storage management (5) —할당 (allocation) –첫번째 블록 할당 사용 가능 리스트에서 삭제 —해제

Heap-based storage management (5) —할당 (allocation) –첫번째 블록 할당 사용 가능 리스트에서 삭제 —해제 (free) –사용 가능 리스트의 처음에 연결 50 Head of free list (a) Initial free-space list (b) Free-space after execution (2019 -1) Understanding of Programming Languages

Heap-based storage management (6) —복구 (recovery) –프로그래머 또는 시스템에 의한 복구 +dispose (PASCAL), free

Heap-based storage management (6) —복구 (recovery) –프로그래머 또는 시스템에 의한 복구 +dispose (PASCAL), free (C), delete (C++, Java) +Garbage, dangling reference의 위험성 존재 51 Garbage Dangling References int *p, *q; … p = malloc(sizof(int)); p = q; int *p, *q; … p = malloc(sizof(int)); q = p; free(p); (2019 -1) Understanding of Programming Languages