Run-Time Environments
Run-Time Environments
Environments
05/03/2025 2
Run-time Representation
Code
Static
Heap
Free Memory
Stack
Fig 1: Typical Subdivision of run-time memory into code and data areas
05/03/2025 3
Dynamic Storage Allocation
• Stack storage
• Names local to a procedure are allocated space on a stack.
• Stack supports the normal call /return policy for procedures.
• Heap storage
• Data that may outlive the call to the procedure that created it is usually
allocated on a heap of reusable storage.
05/03/2025 4
Stack Allocation of Space
• Almost all compilers for languages that use procedures/functions
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.
05/03/2025 5
Activation
• Each execution of a procedure body is referred to as an activation of
the procedure.
• Lifetime of an activation of a procedure P is the sequence of steps
between the first and last steps in the execution of the procedure
body.
• Includes the time spent executing procedures called by P, the procedure
called by them and so on.
05/03/2025 6
Activation
• If a and b are procedure activations, then their lifetimes are
• Non-overlapping or
• Nested.
• Nested activation occurs when a procedure calls another procedure.
05/03/2025 7
Activation Tree
• We can represent the activations of procedures during the running of
an entire program by a tree, called an activation tree.
• Each node represents an activation of a procedure.
• The root represents the activation of the main program.
• The node a is the parent of the node for b iff control flows from the
activation a to b.
• The node a is left to the node b iff the lifetime of a occurs before the
lifetime of b.
05/03/2025 8
Activation Tree
main(){ main
read();
output(); read() output()
}
output(){
format() read()
format();
write();
}
05/03/2025 9
Activation Records
• Procedure calls and returns are usually managed by a run-time stack
called the control stack.
• Each live activation has an activation record on the control stack, with
the root of the activation tree at the bottom.
• The entire sequence of activation records on the stack corresponding
to the path in the activation tree to the activation where control
currently resides.
05/03/2025 10
Activation Records
• Content of activation records vary with the language being
implemented.
Actual Parameters
Returned Values
Control Link
Access Link
Local Data
Temporaries
05/03/2025 11
Activation Records
• Temporaries
• Values arising from the evaluation of expressions.
• Cannot be held in registers.
• Local data
• Belongs to the procedure whose activation record this is.
• Saved Machine Status
• Information about the state of the machine just before the call to the
procedure.
• Return address and content of registers.
05/03/2025 12
Activation Record
• Access Link
• Locate data needed by the called procedure but found elsewhere.
• Control Link
• Points to the activation record of the caller.
• Returned Value
• Value might be placed in registers for efficiency.
• Actual Parameters
• Commonly not placed in activation record but rather in registers.
05/03/2025 13
Variable Length Data on the Stack
• Allocation of object might be required whose sizes are not known
during compile time but local to a procedure.
• Memory might be allocated on the stack.
• Another strategy we can follow is to store pointer in the activation
record.
05/03/2025 14
Variable-Length Data on the Stack
Control link and saved status
…
Pointer to a Activation record
Pointer to b for p
Pointer to C
…
Array a
Array b Arrays of p
Array C
05/03/2025 15
Heap Management
• Heap stores data that lives indefinitely
• Or until the program explicitly deletes it.
• Using new to create object.
• We will study memory manager.
• Subsystem that allocates and deallocates space within the heap.
05/03/2025 16
Memory Manager
• Keeps track of all the free space in heap storage.
• Two basic functions
• Allocation
• Deallocation
• Allocation
• Produce a chunk of contiguous heap memory of requested size.
• Increase heap size if memory is not enough.
• Deallocation
• Returns deallocated space to the pool of free space.
• Typically do not return memory to the operating system.
05/03/2025 17
Desired Properties of Memory
Manager
• Space Efficiency
• Minimize total heap space needed for a program.
• Achieved by minimizing fragmentation.
• Program Efficiency
• Make good use of the memory subsystem to allow programs to run faster.
• Time taken to execute a program vary depending on where objects are placed
in memory.
• Low Overhead
• Spend minimal time for allocation and deallocation.
05/03/2025 18
Garbage Collection
• Data that can not be referenced is generally known as garbage.
• Garbage collector deallocates unreachable data.
05/03/2025 19
Reference Counting Garbage
Collector
• Simple and imperfect.
• Based on reference counting
• Identifies garbage as an object changes from being reachable to unreachable.
• Every object must have a field for the reference count.
05/03/2025 20
Reference count maintenance
• Object Allocation
• Reference count of new object is set to 1.
• Parameter Passing
• Reference count of each object passed into a procedure is incremented by 1.
• Reference Assignments
• For statement u = v where u and v are references
• Reference count of object referred to by v goes up by 1.
• Reference count of object referred to by u goes down by 1.
05/03/2025 21
Reference count maintenance
• Procedure Returns
• All the references held by local variables of that procedure activation record is
decremented.
• If several local variables hold references to the same object, that object’s
count must be decremented for each reference.
• Transitive Loss of Reachability
• When the reference count of an object becomes zero, we must also
decrement the count of each object pointed to by a reference within that
object.
05/03/2025 22
Disadvantages of Reference
Counting
• Cannot collect unreachable, cyclic data structure.
• Expensive.
05/03/2025 23
Thank You
05/03/2025 24