0% found this document useful (0 votes)
2 views8 pages

Unit-3_Run Time Memory Management

The document discusses run-time memory management, detailing the run-time environment, activation trees, and storage allocation methods including static, stack, and heap allocation. It explains how activation records are used in procedure calls and the importance of managing memory dynamically during program execution. Additionally, it covers parameter passing techniques such as pass by value, pass by reference, and pass by name, highlighting their implications on variable manipulation.

Uploaded by

devanshisoni2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Unit-3_Run Time Memory Management

The document discusses run-time memory management, detailing the run-time environment, activation trees, and storage allocation methods including static, stack, and heap allocation. It explains how activation records are used in procedure calls and the importance of managing memory dynamically during program execution. Additionally, it covers parameter passing techniques such as pass by value, pass by reference, and pass by name, highlighting their implications on variable manipulation.

Uploaded by

devanshisoni2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit-3 Run Time Memory Management:

Run-Time Environment:
A program as a source code is merely a collection of text (code, statements etc.) and to make
it alive, it requires actions to be performed on the target machine. A program needs memory
resources to execute instructions. A program contains names for procedures, identifiers etc.,
that require mapping with the actual memory location at runtime.

By runtime, we mean a program in execution. Runtime environment is a state of the target


machine, which may include software libraries, environment variables, etc., to provide
services to the processes running in the system.

Runtime support system is a package, mostly generated with the executable program itself
and facilitates the process communication between the process and the runtime environment.
It takes care of memory allocation and de-allocation while the program is being executed.

Activation Trees
A program is a sequence of instructions combined into a number of procedures. Instructions
in a procedure are executed sequentially. A procedure has a start and an end delimiter and
everything inside it is called the body of the procedure. The procedure identifier and the
sequence of finite instructions inside it make up the body of the procedure.

The execution of a procedure is called its activation. An activation record contains all the
necessary information required to call a procedure. An activation record may contain the
following units (depending upon the source language used).

Temporaries Stores temporary and intermediate values of an expression.

Local Data Stores local data of the called procedure.

Stores machine status such as Registers, Program Counter etc., just


Machine Status
before the procedure is called.

Control Link Stores the address of activation record of the caller procedure.

Access Link Stores the information of data which is outside the local scope.

Stores actual parameters, i.e., parameters which are used to send input to
Actual Parameters
the called procedure.

Return Value Stores return values of a procedure call.

Whenever a procedure is executed, its activation record is stored on the stack, also known as
control stack. When a procedure calls another procedure, the execution of the caller is suspended

CHAPTER-3 1
until the called procedure finishes execution. At this time, the activation record of the called
procedure is stored on the stack.

We assume that the program control flows in a sequential manner and when a procedure is
called, its control is transferred to the called procedure. When a called procedure is executed, it
returns the control back to the caller. This type of control flow makes it easier to represent a
series of activations in the form of a tree, known as the activation tree. An activation tree tells
the flow of execution of a program and represents it in a graphical manner where each node
depicts a particular procedure.

To understand this concept, we take a piece of code as an example:

. . .
printf(“Enter Your Name: “);
scanf(“%s”, username);
show_data(username);
printf(“Press any key to continue…”);
. . .
int show_data(char *user)
{
printf(“Your name is %s”, username);
return 0;
}
. . .

Below is the activation tree of the code given.

Now we understand that procedures are executed in depth-first manner, thus stack allocation is
the best suitable form of storage for procedure activations.

CHAPTER-3 2
Storage Allocation
Runtime environment manages runtime memory requirements for the following entities:
 Code : It is known as the text part of a program that does not change at runtime. Its memory
requirements are known at the compile time.
 Procedures : Their text part is static but they are called in a random manner. That is why,
stack storage is used to manage procedure calls and activations.
 Variables : Variables are known at the runtime only, unless they are global or constant.
Heap memory allocation scheme is used for managing allocation and de-allocation of
memory for variables in runtime.
Static Allocation
In this allocation scheme, the compilation data is bound to a fixed location in the memory
and it does not change when the program executes. As the memory requirement and storage
locations are known in advance, runtime support package for memory allocation and de-
allocation is not required. Static storage allocation is efficient because no time or space is
expended for storage management during execution. However, it is incompatible with
recursive subprogram calls and with data structures whose size is dependent on computed or
input data.

Stack Allocation
Procedure calls and their activations are managed by means of stack memory allocation. It
works in last-in-first-out (LIFO) method and this allocation strategy is very useful for
recursive procedure calls.

Heap Allocation
Variables local to a procedure are allocated and de-allocated only at runtime. Heap allocation
is used to dynamically allocate memory to the variables and claim it back when the variables
are no more required.

Except statically allocated memory area, both stack and heap memory can grow and shrink
dynamically and unexpectedly. Therefore, they cannot be provided with a fixed amount of
memory in the system.

As shown in the image above, the text part of the code is allocated a fixed amount of memory.
Stack and heap memory are arranged at the extremes of total memory allocated to the program.
Both shrink and grow against each other.

CHAPTER-3 3
Stack Allocation of Space:
Almost all compilers for languages that use procedures, functions, or methods as units of
user-defined actions manage at least part of their run-time memory as a stack. Each time a
procedure is called, space for its local variables is pushed onto a stack, and when the
procedure terminates, that space is popped off the stack. As we shall see, this arrangement
not only allows space to be shared by procedure calls whose durations do not overlap in time,
but it allows us to compile code for a procedure in suchaway that the relative addresses of its
nonlocal variables are always the same, regardless of the sequence of procedure calls.
Dynamic Allocation (Stack Allocation):
Stack allocation is a procedure in which stack is used to organize the storage. The stack used
in stack allocation is known as control stack.
The stack allocation is a runtime storage management technique. The activation records are
pushed and popped as activations begin and end respectively using Last In First Out (LIFO)
method.
Storage for the locals in each call of the procedure is contained in the activation record for
that call. Thus, locals are bound to fresh storage in each activation, because a new activation
record is pushed onto the stack when the call is made.
It can be determined the size of the variables at a run time & hence local variables can have
different storage locations & different values during various activations.
It allows recursive subprograms.
On each execution of a procedure, An Activation Record is generated, which contains
information like Local data, actual parameter, return value, return address of a procedure.
The Activation Record for that procedure is saved onto the stack.

If procedure A calls B, and then B calls C, then stack allocation will be:

Procedure calls are implemented by what are known as calling sequences, which consists of
code that allocates an activation record on the stack and enters information into its fields. A
return sequence is similar code to restore the state of the machine so the calling procedure
can continue its execution after the call.

CHAPTER-3 4
Heap Management:
Heap management refers to the process of allocating and deallocating memory dynamically
during program execution. Unlike stack memory, which is automatically managed by the
compiler, heap memory is managed manually by the programmer.
Heap allocation is the most flexible allocation scheme. Allocation and deallocation of
memory can be done at any time and any place depending upon the user's requirement. Heap
allocation is used to allocate memory to the variables dynamically and when the variables are
no more used then claim it back.

There are two methods used for Heap management are as follows −
Garbage Collection Method
When the all-access path to an object is destroyed, but data object continues to exist, such
types of objects are said to be garbage. Garbage collection is a technique that is used to reuse
that object space.
In garbage collection, firstly, we mark all the active objects, and all the remaining elements
whose garbage collection is 'on' are garbaged and returned to the free space list.

Reference Counter
By reference counter, an attempt is made to reclaim each element of heap storage
immediately after it can no longer be accessed.
Each memory cell on the heap has a reference counter associated with it that contains a count
of the number of values that points to it. The count has incremented each time a new value
point to the cell and decremented each time an amount ceases to point to it. When the counter
becomes zero, the cell is returned to the free list for further allocation.

The memory manager keeps track of all the free space in heap storage at all times. It performs
two basic functions:

• Allocation. When a program requests memory for a variable or object,3 the memory
manager produces a chunk of contiguous heap memory of the requested size. If possible, it
satisfies an allocation request using free space in the heap; if no chunk of the needed size is
available, it seeks to increase the heap storage space by getting consecutive bytes of virtual
memory from the operating system. If space is exhausted, the memory manager passes that
information back to the application program.

• Deallocation. The memory manager returns deallocated space to the pool of free space, so
it can reuse the space to satisfy other allocation requests. Memory managers typically do not
return memory to the operating sys-tem, even if the program's heap usage drops.

Thus, the memory manager must be prepared to service, in any order, allo-cation and
deallocation requests of any size, ranging from one byte to as large as the program's entire
address space.

CHAPTER-3 5
At the beginning of program execution, the heap is one contiguous unit of free space. As the
program allocates and deallocates memory, this space is broken up into free and used chunks
of memory, and the free chunks need not reside in a contiguous area of the heap. We refer to
the free chunks of memory as holes. With each allocation request, the memory manager must
place the requested chunk of memory into a large-enough hole. Unless a hole of exactly the
right size is found, we need to split some hole, creating a yet smaller hole.

With each deallocation request, the freed chunks of memory are added back to the pool of
free space. We coalesce contiguous holes into larger holes, as the holes can only get smaller
otherwise. If we are not careful, the memory may end up getting fragmented, consisting of
large numbers of small, noncontiguous holes. It is then possible that no hole is large enough
to satisfy a future request, even though there may be sufficient aggregate free space.

Best - Fit and First - Fit Object Placement:


We reduce fragmentation by controlling how the memory manager places new objects in the
heap. It has been found empirically that a good strategy for mini-mizing fragmentation for
real-life programs is to allocate the requested memory in the smallest available hole that is
large enough.
This best-fit algorithm tends to spare the large holes to satisfy subsequent, larger requests.
An alternative, called first-fit, where an object is placed in the first (lowest-address) hole in
which it fits, takes less time to place objects, but has been found inferior to best-fit in overall
performance.
Next-fit strategy, trying to allocate the object in the chunk that has last been split, whenever
enough space for the new object remains in that chunk. Next-fit also tends to improve the
speed of the allocation operation.

Parameter Passing
The communication medium among procedures is known as parameter passing. The values
of the variables from a calling procedure are transferred to the called procedure by some
mechanism. Before moving ahead, first go through some basic terminologies pertaining to
the values in a program.

r-value
The value of an expression is called its r-value. The value contained in a single variable also
becomes an r-value if it appears on the right-hand side of the assignment operator. r-values
can always be assigned to some other variable.

l-value
The location of memory (address) where an expression is stored is known as the l-value of
that expression. It always appears at the left hand side of an assignment operator.

For example:
day = 1;
week = day * 7;
month = 1;

CHAPTER-3 6
year = month * 12;

From this example, we understand that constant values like 1, 7, 12, and variables like day,
week, month and year, all have r-values. Only variables have l-values as they also represent the
memory location assigned to them.

For example:

7 = x + y;

is an l-value error, as the constant 7 does not represent any memory location.

Formal Parameters
Variables that take the information passed by the caller procedure are called formal parameters.
These variables are declared in the definition of the called function.

Actual Parameters
Variables whose values or addresses are being passed to the called procedure are called actual
parameters. These variables are specified in the function call as arguments.

Example:

fun_one()
{
int actual_parameter = 10;
call fun_two(int actual_parameter);
}
fun_two(int formal_parameter)
{
print formal_parameter;
}

Formal parameters hold the information of the actual parameter, depending upon the parameter
passing technique used. It may be a value or an address.

Pass by Value
In pass by value mechanism, the calling procedure passes the r-value of actual parameters and
the compiler puts that into the called procedure’s activation record. Formal parameters then hold
the values passed by the calling procedure. If the values held by the formal parameters are
changed, it should have no impact on the actual parameters.

Pass by Reference
In pass by reference mechanism, the l-value of the actual parameter is copied to the activation
record of the called procedure. This way, the called procedure now has the address (memory
location) of the actual parameter and the formal parameter refers to the same memory location.
Therefore, if the value pointed by the formal parameter is changed, the impact should be seen
on the actual parameter as they should also point to the same value.

CHAPTER-3 7
Pass by Copy-restore
This parameter passing mechanism works similar to ‘pass-by-reference’ except that the changes
to actual parameters are made when the called procedure ends. Upon function call, the values
of actual parameters are copied in the activation record of the called procedure. Formal
parameters if manipulated have no real-time effect on actual parameters (as l-values are passed),
but when the called procedure ends, the l-values of formal parameters are copied to the l-values
of actual parameters.

Example:

int y;
calling_procedure()
{
y = 10;
copy_restore(y); //l-value of y is passed
printf y; //prints 99
}
copy_restore(int x)
{
x = 99; // y still has value 10 (unaffected)
y = 0; // y is now 0
}

When this function ends, the l-value of formal parameter x is copied to the actual parameter y.
Even if the value of y is changed before the procedure ends, the l-value of x is copied to the l-
value of y making it behave like call by reference.

Pass by Name
Languages like Algol provide a new kind of parameter passing mechanism that works like
preprocessor in C language. In pass by name mechanism, the name of the procedure being called
is replaced by its actual body. Pass-by-name textually substitutes the argument expressions in a
procedure call for the corresponding parameters in the body of the procedure so that it can now
work on actual parameters, much like pass-by-reference.

CHAPTER-3 8

You might also like