Memory Management

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
Programmer & System-controlled
MM (1)
C
 Programmer controlled memory management
 malloc(), free()
(2012-1) Fundamentals of Programming Languages
5
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
6
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
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
(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
단위 프로그램 1
(주 프로그램)
단위 프로그램 2
...
단위 프로그램 K
코드부 1
코드부 2
...
코드부 K
활성
레코드 1
활성
레코드 2
...
활성
레코드 K
전역 변수
(COMMON)변수
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
단위 프로그램 구조
정적 내포 관계 트리
A
A unit A
B
unit B
C
unit C
unit D
end D
end C
B
E
D
end B
E
unit E
unit F
F
end F
G
unit G
end G
end E
end A
(2012-1) Fundamentals of Programming Languages
C
F
G
D
Algol 유사 언어의
정적 내포 관계 예
18
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
Block’s Activation
코드부
(Code segment)
Activation Record
Return address
Dynamic link
Static link
Environment
(local variables,
Parameters)
(2012-1) Fundamentals of Programming Languages
21
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
22
23
Current
Static
link
F’s activation record
G’s activation record
F’s activation record
E’s activation record
A’s activation record
Stack of activation records
(2012-1) Fundamentals of Programming Languages
Dynamic
link
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 배열
A 배열
U의 나머지
준정적 변수
•
(M)
1
•
(N)
1
U의 일부
준정적변수
동적 링크
반환주소
(2012-1) Fundamentals of Programming Languages
B에 대한
명세표
단위 프로그램의 활성 레코드
A에 대한
명세표
스택이
증가하는
방향
32
Stack-based storage management (12)
 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
(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)
heap
P
stack
(2012-1) Fundamentals of Programming Languages
• new(P) : P는 레코드 t의 포인터
① t형의 기억장소를 힢에 할당
② 할당된 주소를 P에 배정
③ dispose(P)를 만날때까지 유지
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 X선언
y선언
D의 활성 레코드 CURRENT
B y선언
D
. . .
. . .
•
•
•
•
•
•
•
•
•
B의 활성 레코드
Z선언
실행중
•
정적
링크
C의 활성 레코드
B의 활성 레코드
C
(a) 한 프로그램 구조 예
(2012-1) Fundamentals of Programming Languages
A의 활성 레코드
(b) (a)에서 ABCD 순으로
호출 실행 상태에서 있는
활성 레코드들의 스택 상태
프로그램 구조와
활성 레코드 상태
<단위 프로그램 구조>
x 선언
unit A
ALGOL68의 지역/비지역 변수 참조
호출순서 : A →E
→F →G →F →G
→F
<활성레코드 실행 상태>
unit B
F 의 A.R
unit C
unit D
end D
end C
end B
정적 링크
unit E x 선언
y 선언
y := x
end F
end E
end A
(2012-1) Fundamentals of Programming Languages
CURRENT
G 의 A.R
•정적링크
- 단위프로그램 내포구조 표현
F 의 A.R
• 동적링크
- 단위프로그램 호출 순서 표현
동적 링크
G 의 A.R
F
unit F
unit G x 선언
end G
40
F 의 A.R
E 의 A.R
A 의 A.R
• F의 “y := x”
y : CURRENT +
y의 옵셋 (지역변수)
x : E 활성레코드 시작 +
x의 옵셋 (비지역변수)
스택이
증가하는
방향
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
(2012-1) Fundamentals of Programming Languages
41
42
Stack-based storage management (19)
 Display
 Array for representing static chain relation
 Method
current
DISPLAY
 (do, lo)
m
 Effective address (do, lo): DISPLAY(do) + lo
...
3
m: DISPLAY 사용 활성 레코드 수
do: display offset,
lo: local offset
 Advantage/disadvantage
 Same reference time for all non-local variables
 Update display on creation/deletion of activation records
(2012-1) Fundamentals of Programming Languages
2
1
MAIN  A  SUB2  SUB1
Display offset = static depth
MAIN = 0, A = 1, SUB1 = 2
SUB2 = 2, SUB3 = 3
program MAIN
procedure A;
procedure SUB1;
end; { SUB1 }
procedure SUB2;
procedure SUB3;
end SUB3; { SUB3 }
end; { SUB2 }
end A; { A }
end. {MAIN}
(2012-1) Fundamentals of Programming Languages
…
SUB2’s A.R.
2
A’s A.R.
1
MAIN’s A.R.
0
SUB1’s A.R.
…
SUB2’s A.R.
2
A’s A.R.
1
MAIN’s A.R.
0
43
44
…
SUB2’s A.R.
2
A’s A.R.
1
MAIN’s A.R.
0
SUB1’s A.R.
…
SUB3’s A.R.
3
…
SUB2’s A.R.
2
SUB3’s A.R.
3
A’s A.R.
1
SUB2’s A.R.
2
MAIN’s A.R.
0
A’s A.R.
1
MAIN’s A.R.
0
(2012-1) Fundamentals of Programming Languages
Heap-based storage management (1)
 Heap
 Memory block
 Free allocation and de-allocation
 Problems
 Allocation, recovery, compaction, reuse
 Collection technique
(2012-1) Fundamentals of Programming Languages
45
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
I
Y
X
Z
스택이
증가하는
방향
(2012-1) Fundamentals of Programming Languages
Heap
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
(a) Initial free-space list
(b) Free-space after execution
(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
Dangling References
int *p, *q;
…
p = malloc(sizof(int));
p = q;
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