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

Slides:



Advertisements
Similar presentations
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Names and Bindings.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
1 Programming Languages Storage Management Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 10 Storage management
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
Run time vs. Compile time
The Concept of Variables
Copyright © 1995 by Addison-Wesley Publishing Co. 1 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case sensitive?
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Storage Management Lecture 12: Dolores Zage.
Name Binding and Object Lifetimes Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
Runtime Environments Compiler Construction Chapter 7.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
ISBN Chapter 5 Names, Bindings, and Scopes.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Basic Semantics Associating meaning with language entities.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 9.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
COMP3190: Principle of Programming Languages
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Chap 9 Run-time Storage Consider the C program : static int x; int abc(int y) { float z; static int w;  abc(z);  malloc(20) } main {  abc(3)
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
LECTURE 13 Names, Scopes, and Bindings: Memory Management Schemes.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Names, Scope, and Bindings Programming Languages and Paradigms.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Chapter 10 : Implementing Subprograms
Data Types In Text: Chapter 6.
Chapter 8. Memory Management
Implementing Subprograms Chapter 10
Implementing Subprograms
Chapter 4 Variables & Binding
Names, Bindings, and Scopes
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Names and Binding In Text: Chapter 5.
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Implementing Subprograms
Presentation transcript:

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

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

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

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

PROGRAMMER & SYSTEM- CONTROLLED MM (1)  C Programmer controlled memory management malloc(), free() (2013-1) Understanding 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 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

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

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

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

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

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

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

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

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

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

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

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

(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 유사 언어의 정적 내포 관계 예

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

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

(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.

(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

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.

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

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;

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

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

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

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

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

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

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

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

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

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

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

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

(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) 한 프로그램 구조 예

(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

(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

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 m DISPLAY current 2... m: DISPLAY 사용 활성 레코드 수 do: display offset,lo: local offset

(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 … 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 …SUB1’s A.R.

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

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

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

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

(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 heap 20: 7: stack

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

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

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

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);

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

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