1/83
Looks like no tags are added yet.
Runtime Stack (Purpose)
Used for static memory allocation, storing local variables, parameters, and return addresses.
Runtime Stack (Management)
Managed automatically; variables are pushed on function call and popped on exit.
Runtime Stack (Lifetime)
Short lifetime; variables exist only during the execution of the function.
Runtime Stack (Access)
Fast access because it follows a Last-In-First-Out (LIFO) order.
Heap (Purpose)
Used for dynamic memory allocation, storing objects persisting beyond a single function.
Heap (Management)
Managed manually by the programmer using functions like malloc/free or new/delete.
Heap (Lifetime)
Long lifetime; exists until explicitly deallocated or reclaimed by garbage collection.
Heap (Access)
Slower access due to more complex memory management.
Stack Frame
A runtime stack structure containing a function's local variables, parameters, and return address.
Recursion: Base Case Process
The function stops calling itself, starts returning, and stack frames are popped.
Recursion: Unwinding
The stack pops each frame, restoring the state of the previous function call.
Recursion (Disadvantage)
Deep recursion can run out of stack space, causing a stack overflow.
Garbage Collection (GC)
Automatic memory management that reclaims heap memory no longer in use.
GC: Reachability
Objects accessible from root references are considered live and not collected.
GC: Marking Phase
The collector starts from root references and marks all live, reachable objects.
GC: Sweeping Phase
Reclaiming memory from dead objects.
GC: Compacting Phase
The collector moves live objects together to reduce fragmentation.
Reference Counting GC
Reclaims an object when its count of references drops to zero.
Tracing GC
Traces the stack and heap to find and mark reachable objects.
Generational GC (Concept)
Categorizes objects by age, collecting younger objects more frequently to optimize performance.
Young Generation
Heap section containing newly created objects, which are collected frequently.
Old Generation
Heap section containing long-lived objects that survived multiple GC cycles.
Permanent Generation
Heap section containing metadata and class definitions.
Minor GC
A fast garbage collection process triggered when the young generation fills up.
GC: Promotion
Moving objects that survived several minor GCs to the old generation.
Major GC
A comprehensive garbage collection process triggered when the old generation fills up.
Stop-and-Copy: Semi-Spaces
The heap is divided into two equal parts: active and inactive.
Stop-and-Copy: Copy Phase
All live objects are copied contiguously from active to inactive space.
Stop-and-Copy: Flip Phase
The roles of the active and inactive spaces are swapped.
Stop-and-Copy (Disadvantage)
Requires twice the memory because of the two semi-spaces.
Pass-by-Value
Passing a copy of the argument; local changes do not affect the original.
Pass-by-Reference
Passing a reference to the actual variable; local changes modify the original.
Using Pointers (Parameter Passing)
Passing a pointer to the variable, allowing the function to modify the original.
Pass-by-Value (Pro/Con)
Pros: Efficiency: No need to copy large objects, saving time and memory. Flexibility: Allows functions to modify the original variable.
Cons: Performance: Copying large objects can be inefficient.
Memory Usage: Requires additional memory for the copies.
Pass-by-Reference (Pro/Con)
Pros: Efficiency: No need to copy large objects, saving time and memory. Flexibility: Allows functions to modify the original variable.
Cons: risks unintended side effects. Requires understanding of references and potential issues like aliasing
(meaning the same object or value has more than one variable name).
Aliasing
When the same object or memory location has more than one variable name.
Recursion: Function Calls
Each recursive call creates a separate stack frame holding its state.
Recursion: State Management
The stack automatically maintains the state of each recursive call.
Recursion: Memory Efficiency
Each stack frame contains only the information needed for its function call.
GC: Root References
Global and stack variables can serve as starting points for reachability tracing.
Reference Counting: Cyclic References
Cyclic references can prevent reference counting from reclaiming objects.
Stop-and-Copy: Allocation
New objects are allocated in the active space.
Stop-and-Copy: Stop Phase
Program execution pauses when the active space fills.
Stop-and-Copy: Reference Updates
References are updated when live objects are moved to the inactive space.
Stop-and-Copy: Pause Times
Copying live objects can cause significant pauses in program execution.
Stop-and-Copy: Young Generation
This method is often used where most objects are short-lived.
Pass-by-Value: Memory Usage
Copies require additional memory.
Pass-by-Reference: Complexity
References require understanding potential aliasing issues. (meaning the same object or value has more than one variable name)
Recursion: Recursive Case
The function calls itself on a smaller or otherwise modified problem.
Memory Leak
Allocated memory remains unreclaimed after the program no longer needs it.
Dangling Pointer
A pointer that refers to memory that has already been deallocated.
Heap Fragmentation
Free heap memory becomes divided into scattered blocks, complicating allocation.
Pass-by-Value: Object References
A copied reference can still allow modification of the referenced object.
Automatic memory management
Reduces the burden on developers to manually manage
memory.
Generational garbage collection Advantages
Efficiency: By focusing on the young generation, which is smaller and contains most short-
lived objects, minor GCs are fast and efficient.
⢠Reduced Pause Times: Major GCs are less frequent, reducing the overall pause times for the
application.
Advantages of stop and copy
Defragmentation: By copying live objects to a contiguous memory area, it eliminates
fragmentation.
⢠Simplicity: The algorithm is straightforward to implement.
Disadvantages of stop and copy
Memory Overhead: Requires twice the memory since it needs two spaces.
⢠Pause Times: Can introduce significant pauses in program execution during the copying
phase.
generational garbage collection disadvantages
Performance Overhead: Garbage collection can introduce pauses in program execution,
which might affect performance.
⢠Non-Deterministic: The timing of garbage collection is not predictable, which can be an
issue for real-time systems
Using Pointers description
A reference to the actual variable is passed to the function. Changes made to
the parameter inside the function affect the original value.
What does a stack store?
Local variables
Function parameters
Return addresses
What happens to the stack when a function is called?
A new stack record (stack frame) is pushed onto the stack.
The record contains the function's parameters, local variables, and return address.
When the function finishes, its entire stack record is popped off.
Important: Every recursive function call creates a NEW stack record, even though it's the same function.
What does not go on the heap?
Local variables (int x = 5) : Stack
Function parameters: Stack
Return addresses :Stack
Local reference variables (Student s) :Stack
What does the heap store?
Dynamically allocated objects
Dynamically allocated arrays
Data created using new or malloc()
Objects that need to persist beyond a single function
Linked lists, trees, and other dynamically allocated data structures
Simple explanation of GC Reference counting
Every object has a reference count.
When a reference points to an object, its count increases.
When a reference is removed, its count decreases.
When the count reaches 0, the object can be collected as garbage.
example of GC reference counting
Imagine Object A points to Object B, and Object B points back to Object A.
Even if nothing else points to either object, their reference counts are still 1.
Reference counting may fail to recognize them as garbage.
Mark and sweep GC (tracing)
Reference counting may fail to recognize them as garbage.
difference between Reference counting and marking + sweeping
Mark and Sweep can identify circular references that are unreachable.
Generational GC parts
Young Generation: Newly created objects.
Old Generation: Objects that have survived multiple garbage collection cycles.
Permanent Generation: In some implementations, stores metadata and class definitions.
Process of generational garbage collection
A new object is created in the Young Generation.
When the young generation fills up, a Minor GC occurs.
Objects still being used survive and may be promoted to the Old Generation.
When the old generation fills up, a Major GC occurs.
cdr in scheme
gets everything except the first element from a list
car (scheme)
gets the first element of a list
cons function (scheme)
adds an element to the front of a list
null? in scheme
checks if a list is empty
length function in scheme
returns the length of a list
list in scheme
creates a list
equal? function in scheme
checks whether two values are equal
define(
begins all scheme work
odd? function
checks if given is odd
even? function
checks if given is even
max
returns largest number
pros of mark and sweep
is always correct
cons of mark and sweeep
very slow, program must be paused when going through the stack and heap
Pros of reference counting
less time than Mark & Sweep, doesn't have to check Stack and Heap
cons of reference counting
doesn't find all the garbage