Docsity

Memory Management and Garbage Collection: Run-time Environments and Memory Organization, Study notes of Computer Science

Toggle sidebar
bg1
1
Run-time Environments
• The compiler creates and manages a run-time environment in
which it assumes the target program will be executed
• Issues for run-time environment
– Layout and allocation of storage locations for named program objects
– Mechanism for the target program to access variables
– Linkages between procedures
– Mechanisms for passing parameters
– Interfaces to the operating system for I/O and other programs
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Memory Management and Garbage Collection: Run-time Environments and Memory Organization and more Study notes Computer Science in PDF only on Docsity!

Run-time Environments

•^

The compiler creates and manages a run-time environment inwhich it assumes the target program will be executed

•^

Issues for run-time environment^ –

Layout and allocation of storage locations for named program objects

-^

Mechanism for the target program to access variables

-^

Linkages between procedures

-^

Mechanisms for passing parameters

-^

Interfaces to the operating system for I/O and other programs

Storage Organization •^

Assumes a logical address space^ –

Operating system will later map it to physical addresses, decidehow to use cache memory, etc.

•^

Memory typically divided into areas for^ –

Program code

-^

Other static data storage, including global constants and compilergenerated data

-^

Stack to support call/return policy for procedures

-^

Heap to store data that can outlive a call to a procedure Code

Static

Heap

Free Memory

Stack

Procedure Example: quicksort int a[11];void readArray( ) /* Reads 9 integers into a[1] through a[9] */{^

int i;

int partition ( int m, int n){^

/* picks a separator v and partitions a[m .. n] so that a[m .. p-1] are less than v,

a[p] = v, a[p+1 .. n] are equal to or greater than v. Returns p.

} void quicksort (int m, int n){^

int i;if ( n > m ){^

i = partition(m,n);

quicksort(m, i-1);

quicksort(i+1, n);

} main ( ){^

readArray ( );a[0] = -9999;

a[10] = 9999;

quicksort (1, 9);

}

Activation Records

•^

Elements in the activation record:^ –

temporary values that could not fit into registers

-^

local variables of the procedure

-^

saved machine status for point at which thisprocedure called. includes return address andcontents of registers to be restored.

-^

access link to activation record of previous blockor procedure in lexical scope chain

-^

control link pointing to the activation record of thecaller

-^

space for the return value of the function, if any

-^

actual parameters (or they may be placed inregisters, if possible)

actual paramsreturn valuescontrol link

access link

saved machine

state

local datatemporaries

Activation Record Example •^

Showing one way to divide responsibility between the callerand the callee.

actual params and return val

control link, access link,and saved machine statelocal data and temporaries

actual params and return val

control link, access link,and saved machine statelocal data and temporaries

Caller A.R. Callee A.R. TOP-SP

CallerresponsibilityCalleeresponsibility

actual top of stack

Calling Sequence •^

A possible calling sequence matching the previous diagram:– caller evaluates the actual parameters– caller stores the return address and old value of TOP-SP

in the callee’s AR. Caller then increments TOP-SP tothe callee’s AR. (Caller knows the size of the caller’slocal data and temps, and the callee’s parameters andstatus fields.). Caller jumps to callee code.

– callee saves the register values and other status fields– callee initializes local data and begins execution

10

Variable-length data on the stack

•^

It is possible to allocateobjects, arrays or otherstructures of unknownsize on the stack, as longas they are local to aprocedure and becomeinaccessible when theprocedure ends

•^

For example, represent adynamic array in theactivation record by apointer to an arraylocated between theactivation records

actual params and return val

control link, access link,and saved machine state

pointer to array a

array a

actual params and return val

control link, access link,and saved machine statelocal data and temporaries

proc p proc q

Access to Nonlocal Data on the Stack •^

Simplest case are languages without nested procedures orclasses^ –

C and many C-based languages

•^

All variables are defined either within a single procedure(function) or outside of any procedure at the global level

•^

Allocation of variables and access to variables^ –

Global variables are allocated static storage. Locations are fixed atcompile time.

-^

All other variables must be local to the activation on the top of thestack. These variables are allocated when the procedure is calledand accessed via the TOP-SP pointer.

•^

Nested procedures will use a set of access links to accessvariables at other levels on the stack

Access Links •^

Access links allow implementation of the normal staticscope rule^ –

if procedure p is nested immediately within q in the source code,then the access link of an activation of p points to the more recentactivation of q

•^

Access links form a chain – one link for each lexical level –allowing access to all data and procedures accessible to thecurrently executing procedure

•^

Look at example of access links from quicksort program inML (previous slide)

Defining Access Links for direct procedure calls •^

Procedure q calls procedure p explicitly:^ –

case 1: procedure p is at a nesting depth 1 higher than q (can’t bemore than 1 to follow scope rules). Then the access link is to theimmediately preceding activation record (of p) (example quicksortcalls partition)

-^

case 2: recursive call, i.e. q is p itself. The access link in the newactivation record for q is the same as the preceding activation recordfor q (example: quicksort called quicksort)

-^

case 3: procedure p is at a lower nesting depth than q. Thenprocedure p must be immediately nested in some procedure r(defined in r) and there must be an activation record for r in theaccess chain of q. Follow the access links of q to find the activationrecord of r and set the access link of p to point to that activationrecord of r. (partition calls exchange, which is defined in sort)

Displays •^

If the nesting depth of access links getslarge, then access to nonlocal variableswill be inefficient to follow the chainof access links.

-^

Solution is to keep an auxiliary array –the display – in which each element isthe highest activation record on thestack for the procedure at that nestingdepth.

-^

Whenever a new activation record iscreated at level l, it will save the valueof display[l] to restore when it is done

sort q(1,9) saved d[2]

q(1,3) saved d[2]

p(1,3) saved d[3]

e(1,3) saved d[2]

d[1]d[2]d[3] display

stack

Dangling pointers in the stack •^

In a stack-based environment, typically used for parametersand local variables, variables local to a procedure areremoved from the stack when the procedure exits

•^

There should not be any pointers still in use to such variables

•^

Example from C:

int* dangle(void){ int x;

return &x;

•^

An assignment “addr = dangle();” causes addr to point to adeallocated stack location^ –

In C, this is considered to be a programming error, not one that thecompiler checks for.

Heap Management •^

Store used for data that lives indefinitely, or until theprogram explicitly deletes it

•^

Memory manager allocates and deallocates memory in theheap^ –

serves as the interface between application programs, generated bythe compiler, and the operating system

-^

calls to free and delete can be generated by the compiler, or in somelanguages, explicitly by the programmer

•^

Garbage Collection is an important subsystem of thememory manager that finds spaces within the heap that areno longer used and can be returned to free storage^ –

the language Java uses the garbage collector as the deallocationoperation

Memory Manager •^

The memory manager has one large chunk of memory fromthe operating system that it can manage for the applicationprogram

•^

Allocation – when a program requests memory for avariable or an object (anything requiring space), the memorymanager gives it the address of a chunk of contiguous heapmemory^ –

if there is no space big enough, can request the operating system forvirtual space

-^

if out of space, inform the program

•^

Deallocation – returns deallocated space to the pool of freespace^ –

doesn’t reduce the size of space and return to the operating system