Object Lifetime and Storage Management
Object Lifetime and Storage Management
Object Lifetime
Period of time between the creation and destruction of the object.
Example: time between creation and destruction of a dynamically allocated
variable in C++ using new and delete
Binding lifetime
Period of time between the creation and destruction of a name to object binding
is called the binding’s lifetime. (Name-to-object association)
E.g. a Java reference variable is assigned the address of an object
E.g. a function's formal argument is bound to an actual argument (object)
1. Static objects
Objects have an absolute storage address that is retained throughout the
execution of the program.
Static values are assigned with absolute address.
Static values will not change during the execution. Eg., Global variables
Object’s lifetime spans the whole execution of the program.
Subroutine code
Class method code
2. Stack objects
Objects are allocated in last-in first-out (LIFO) order, usually in
conjunction with subroutine calls and returns.
Stackframe is allocated based on subroutine/ function call.
Stack deallocates on function return.
Object’s lifetime spans period between invocation of the subroutine and
return from the subroutine.
Actual arguments of a subroutine
Local variables of a subroutine
3. Heap objects
Dr Sruthy S, PP, S7CSE-B, SJCET Palai
Object stored on heap.
Run Time Objects may be allocated and deallocated (created/destroyed)
at arbitrary times
Explicitly by programmer or
Implicitly by garbage collector
Heap objects require an expensive/ (COSTLIER mechanism) storage
management algorithm.
E.g. Java class instances are always stored on the heap.
1. Static Allocation- value stored should not change during the execution.
2. Stack-Based Allocation
The stack is used to allocate space for subroutines in languages that permit
recursion.
During function call a stack frame is allocated and is deallocated on function
return.
Step1: The Subroutine A is called first. Thus the corresponding stack frame is
created for Subroutine A.
The word sizes of the types of local variables and arguments determines the fp
offset in a frame.
Fp + displacement address can locate and access all the local variables
and its values.
Compiler determines
Frame pointer: a register pointing to a known location within the
current stack frame
Offsets from the frame pointer specifying the location of objects in the
stack frame.
The absolute size of the stack frame may not be known at compile time (e.g.,
variable-size arrays allocated on the stack).
Stack pointer
• Register pointing to the first unused location on the stack (used as the
starting position for the next stack frame).
Specified at runtime
Dr Sruthy S, PP, S7CSE-B, SJCET Palai
• The absolute location of the stack frame in memory (on the stack)
• The size of the stack frame
3. Heap-Based Allocation
The heap is a region of memory where blocks can be allocated and deallocated
dynamically at arbitrary times and in arbitrary order.
More unstructured
Memory may become fragmented
Need for garbage collection
Implicit heap allocation:
Java class instances are always placed on the heap
Scripting languages and functional languages make extensive use of the
heap for storing objects
Some procedural languages allow array declarations with run-time
dependent array size
Resizable character strings.
Explicit heap allocation:
Statements and/or functions for allocation and deallocation
Heap allocation is performed by searching heap for available free space.
Deletion of objects leaves free blocks in the heap that can be reused.
Disadvantage of heap based memory allocation are of 2 types. They are:
Internal heap fragmentation: If allocated object is smaller than the free block
the extra space is wasted.
External heap fragmentation: Smaller free blocks are scattered. And hence we
cannot place the objects.
First-fit: select the first large block and place the data.
Best-fit: search entire list for the smallest free block that is large enough to hold
the object. If extra space is there, it’s added to free list.
If an object is smaller than the block, the extra space can be added to the list of
free blocks
When a block is freed, adjacent free blocks are coalesced
Heap allocates memory in 2 ways:
1) static allocation 2) Dynamic allocation
i. Buddy System ii.
Fibonacci Heap
Buddy system: maintain heap pools of standard sized blocks of size 2k.
If no free block is available for object of size between 2k-
1
+1 and 2kthen find block of size 2k+1and split it in half, adding the
halves to the pool of free 2k blocks, etc.
Fibonacci heap: maintain heap pools of standard size blocks according to
Fibonacci numbers
More complex but leads to slower internal fragmentation.
Automatic garbage collection removes all objects from the heap that are not
accessible, i.e. are not referenced
Used in Lisp, Scheme, Prolog, Ada, Java, Haskell
Disadvantage is GC overhead, but GC algorithm efficiency has been
improved
Not always suitable for real-time programming.