CS F301-L19-21

Embed Size (px)

Citation preview

  • 8/10/2019 CS F301-L19-21

    1/61

    BITSPilaniHyderabad Campus

    BITS PilaniDr.Aruna Malapati

    Asst ProfessorDepartment of CSIS

  • 8/10/2019 CS F301-L19-21

    2/61

    BITSPilaniHyderabad Campus

    Dynamic Memory Management

  • 8/10/2019 CS F301-L19-21

    3/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Object Lifetime and Storage

    Typical Program and Data Layout in Memory

    Heap Organization

    Heap Allocation

    Heap allocation problem

    Heap Allocation Algorithms

    Todays Agenda

  • 8/10/2019 CS F301-L19-21

    4/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Objects (program data and code) have to bestored in memory during their lifetime.

    In general there are three types of objects in aprogram

    The objects that are alive throughout the execution ofa program

    E.g. global variables

    The objects that are alive within a routine

    E.g. local variables

    The objects whose lifetime can be dynamicallychanged.

    The objects that are managed by the new/delete constructs.

    Object Lifetime and Storage

  • 8/10/2019 CS F301-L19-21

    5/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    The three types of objects correspond to three principal storageallocation mechanisms.

    Static objectshave an absolute storage address that is retainedthroughout the execution of the program

    Global variables and data

    Subroutine code and class method code Stack objects are allocated in last-in first-out order, usually in

    conjunction with subroutine calls and returns

    Actual arguments passed by value to a subroutine

    Local variables of a subroutine

    Heap objects may be allocated and deallocated at arbitrarytimes, but require an expensive storage management algorithm

    Dynamically allocated data in C++.

    Java class instances are always stored on the heap

    Object Lifetime and Storage

  • 8/10/2019 CS F301-L19-21

    6/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Typical Program and DataLayout in Memory

    Program code is at the bottom

    of the memory region (code

    section)

    The code section is protected

    from run-time modification by

    the OS

    Static data objects are stored

    in the static region

    Stack grows downward

    Heap grows upward

    heap

    stack

    static data

    code

    0000

    Upper addr

    Virtualmemoryaddress

    space

  • 8/10/2019 CS F301-L19-21

    7/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Heap is organized as linear array where dynamic elements

    are stored.

    Three types of memory blocks

    Allocated : active blocks used by process

    Released : not used by process need to be recycled to be

    in free pool

    Free : can be allocated to a process based on run timerequest.

    Each memory block contains the size information along

    with it.

    Heap Organization

  • 8/10/2019 CS F301-L19-21

    8/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Heap can be modeled as a sequence of quadruples

    where an element of the sequence can be modeled as

    (allocated,,,)

    (released,,,)

    (free,,,)

    Heap organization

  • 8/10/2019 CS F301-L19-21

    9/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Heap Organization

  • 8/10/2019 CS F301-L19-21

    10/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Heap Organization

  • 8/10/2019 CS F301-L19-21

    11/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Heap is used to store objects who lifetime is dynamic Implicit heap allocation:

    Done automatically

    Java class instances are 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

    Malloc/free, new/delete

    Heap Allocation

  • 8/10/2019 CS F301-L19-21

    12/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Heap is a large block of memory (say N bytes)

    Requests for memory of various sizes may arriverandomly: program runs new

    Each request may ask for 1 to N bytes

    If a request of X bytes is granted, a continuousX bytesin the heap is allocated for the request. The memory willbe used for a while and then return to the system (when

    the program runs delete

    The problem: how to allocate memory so that as manyrequest can be satisfied as possible.

    Heap allocation problem

  • 8/10/2019 CS F301-L19-21

    13/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Heap Allocation Algorithms

    Heap allocation is performed by searching the heap foravailable free space

    For example, suppose we want to allocate a new objectE of 20 bytes, where would it fit?

    Deletion of objects leaves free blocks in the heap thatcan be reused

    Two questions

    How to keep track of free blocks?

    How to find the right free block for each request?

    Object A Free Object B Object C Free Object D Free30 bytes 8 bytes 10 bytes 24 bytes 24 bytes 8 bytes 20 bytes

  • 8/10/2019 CS F301-L19-21

    14/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    How to keep track of free blocks? Maintain a linked list of free heap blocks

    To satisfy a request (new), search the list to find oneblock whose size is equal to or larger than the

    requested size If the size is equal, remove the block from the free list

    If the size is larger, modify the size to (sizerequestedsize).

    When an object is deleted (freed), the block ofmemory is returned to the heap.

    Insert a new block to the free list. If new block can bemerged with its neighboring blocks to be a larger block,merge the blocks.

    Heap Allocation Algorithms(contd)

  • 8/10/2019 CS F301-L19-21

    15/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    How to pick which block to be used for each

    request?

    There can be many choices. First-fit: select the first block in the list that is large

    enough

    Best-fit: search the entire list for the smallest free

    block that is large enough to hold the object

    O(N) for both new and delete operations

    Heap Allocation Algorithms(contd)

  • 8/10/2019 CS F301-L19-21

    16/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Example: Best Fit

  • 8/10/2019 CS F301-L19-21

    17/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Example: First Fit

  • 8/10/2019 CS F301-L19-21

    18/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Example: Worst Fit

  • 8/10/2019 CS F301-L19-21

    19/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Example: Next Fit

  • 8/10/2019 CS F301-L19-21

    20/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

  • 8/10/2019 CS F301-L19-21

    21/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Manual (for Ex free in C language)

    Automatic (Garbage collection)

    Deallocation does not require traversal of every memory

    allocation.

    Fragmentaion

    Deallocation of objects

  • 8/10/2019 CS F301-L19-21

    22/61CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Start and stop: Suspends execution of the program

    temporarily when it starts execution.

    Real time: interleaves the garbage collection with

    program execution.

    Incremental garbage collection

    Continuous garbage collection: collects released memory

    immediately

    Reference count

    Concurrent garbage collection

    Hard real time garbage collection

    Garbage collectionapproaches

  • 8/10/2019 CS F301-L19-21

    23/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Mark and scan algorithm

    Copying garbage collection

    Cheneys modified Copying garbage collection Generational garbage collection

    Start and stop garbagecollection

  • 8/10/2019 CS F301-L19-21

    24/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Has two phases

    Mark

    Scan

    Mark and scan algorithm

  • 8/10/2019 CS F301-L19-21

    25/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Each cell has a mark bit

    Garbage remains unreachable and undetected until

    heap is used up; then GC goes to work, while program

    execution is suspended

    Marking phase

    Starting from the roots, set the mark bit on all live

    cells

    Sweep phase

    Return all unmarked cells to the free list

    Reset the mark bit on all marked cells

    Mark-Sweep GarbageCollection

  • 8/10/2019 CS F301-L19-21

    26/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Mark-Sweep Example

    root

    set

    Heap space

  • 8/10/2019 CS F301-L19-21

    27/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Mark-Sweep Example

    root

    set

    Heap space

  • 8/10/2019 CS F301-L19-21

    28/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Mark-Sweep Example

    root

    set

    Heap space

  • 8/10/2019 CS F301-L19-21

    29/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Mark-Sweep Example

    root

    set

    Heap space

    Reset mark bitof marked cells

    Free unmarkedcells

  • 8/10/2019 CS F301-L19-21

    30/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Copying garbage collection

    Divide heap into two semispaces.Allocate from one space (fromspace) till full.

    Copy live data into other space (tospace).

    Switch roles of the spaces.

    Requires fixing pointers to moved data

    (forwarding).

    Eliminates fragmentation.

    DFS improves locality, while BFS does not

    require any extra storage.

  • 8/10/2019 CS F301-L19-21

    31/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Stop and Copy GC: Example

    A B C D Froot E

    Before collection:

    new space

    A C F

    root

    new space

    After collection:

    free

    heappointer

  • 8/10/2019 CS F301-L19-21

    32/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Form an initial queue of objects which can beimmediately reached from the root set.

    A "scan" pointer is advanced through the objects location

    by location. Every time a pointer into fromspace isencountered, the object the pointer refers to is copied to

    the end of the queue.

    When the "scan" reaches the end of the queue, all liveobjects have been copied, so the garbage collector is

    terminated.

    Cheney's Algorithm

  • 8/10/2019 CS F301-L19-21

    33/61

  • 8/10/2019 CS F301-L19-21

    34/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Advantages

    The allocation of free objects is simple and fast.

    This method does not cause memory fragmentation,

    even when objects of different sizes are copied.

    Optimization

    To increase copying collectors efficiency, increase theamount of memory allocated for the heap space to

    reduce the number of times the collector is invoked.

    Cheney's Algorithm

  • 8/10/2019 CS F301-L19-21

    35/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Generational garbage collection divides the heap into

    two or more regions, called generations.

    Objects are always allocated in the youngest generation.

    The garbage collection algorithm scans the youngest

    generation most frequently, and performs scanning of

    successive generation more rarely.

    Generational GarbageCollection

    G G

  • 8/10/2019 CS F301-L19-21

    36/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Object allocation

    Generational Garbagecollection

    G i l G b

  • 8/10/2019 CS F301-L19-21

    37/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    When the eden space fills up, a minor garbage collectionis triggered.

    Generational Garbagecollection

    G ti l G b

  • 8/10/2019 CS F301-L19-21

    38/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Referenced objects are moved to the first survivorspace. Unreferenced objects are deleted when the eden

    space is cleared.

    Generational Garbagecollection

    G ti l G b

  • 8/10/2019 CS F301-L19-21

    39/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    At the next minor GC, the same thing happens for theeden space. Unreferenced objects are deleted and

    referenced objects are moved to a survivor space.

    However, in this case, they are moved to the second

    survivor space (S1).

    In addition, objects from the last minor GC on the first

    survivor space (S0) have their age incremented and get

    moved to S1. Once all surviving objects have beenmoved to S1, both S0 and eden are cleared.

    Notice we now have differently aged object in the

    survivor space.

    Generational Garbagecollection

  • 8/10/2019 CS F301-L19-21

    40/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Object aging

  • 8/10/2019 CS F301-L19-21

    41/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    At the next minor GC, the same process repeats.However this time the survivor spaces switch.

    Referenced objects are moved to S0. Surviving objects

    are aged. Eden and S1 are cleared.

    Additional aging

  • 8/10/2019 CS F301-L19-21

    42/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    After a minor GC, when aged objects reach a certain agethreshold (8 in this example) they are promoted from

    young generation to old generation.

    Promotion

  • 8/10/2019 CS F301-L19-21

    43/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    As minor GCs continue to occure objects will continue tobe promoted to the old generation space

    Promotion

  • 8/10/2019 CS F301-L19-21

    44/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    So that pretty much covers the entire process with theyoung generation. Eventually, a major GC will be

    performed on the old generation which cleans up and

    compacts that space.

    GC process summary

    I t l G b

  • 8/10/2019 CS F301-L19-21

    45/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Interleaves Partial GC(PGC) with ProgramExecution(PE)

    Once GC starts PGC,PE, PGC,PE, PGC,PE, where

    PGC is very small time slice.

    More memory is recycled than allocated.

    After GC is over PE continues execution.

    Incremental Garbagecollection

    T i l M ki d

  • 8/10/2019 CS F301-L19-21

    46/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Tricolor marking is a method of marking which objectshave been looked at in a collection cycle, and

    determining which ones to recycle at the end of the

    cycle.

    Grey Are ready to be examined by the collector

    Are assumed to be in use by the mutator

    Black Have already been examined by the collector

    Are assumed to be in use by the mutator

    White Have not yet been examined by the collector

    May or may not be in use by the mutator

    Tricolor Marking andCoherence

    Continuous GC using

  • 8/10/2019 CS F301-L19-21

    47/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Continuous GC usingreference counting

  • 8/10/2019 CS F301-L19-21

    48/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Periodically copy K (K>1) cells from space to the tospace.

    When GC starts the Form space is sealed and memory

    allocation is in the tospace.

    If m blocks are requested them m*k blocks are copied

    fromspace to tospace.

    Bakers algorithm

  • 8/10/2019 CS F301-L19-21

    49/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Black:The cells that arecopied formspace to

    tospace along with

    children.

    Gray: The cells that copiedformspace to tospace has

    links to children in the

    other space.

    White: cells are that arereleased and will not be

    copied.

    Bakers algorithm

  • 8/10/2019 CS F301-L19-21

    50/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    The root set is all the data that can be accessed (reached)directly by a program without having to dereference any pointer

    Recursively, any object whose reference is stored in a field of a

    member of the root set is also reachable

    New objects are introduced through object allocations and add

    to the set of reachable objects

    Parameter passing and assignments can propagate reachability

    Assignments and ends of procedures can terminate reachability

    Reachability of Objects

  • 8/10/2019 CS F301-L19-21

    51/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Similarly, an object that becomes unreachable can causemore objects to become unreachable

    A garbage collector periodically finds all unreachable

    objects by one of the two methods. Catch the transitionsas reachable objects become unreachable

    Or, periodically locate all reachable objects and infer that

    all other objects are unreachable

    Reachability of Objects

  • 8/10/2019 CS F301-L19-21

    52/61

  • 8/10/2019 CS F301-L19-21

    53/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    New object allocation.ref_count=1for the new object

    Parameter passing.ref_count++for each object passed

    into a procedure

    Reference assignments.For u:=v, where u and v are

    references, ref_count++for the object *v, and ref_count--for the object *u

    Procedure returns. ref_count--for each object pointed to

    by the local variables

    Transitive loss of reachability.Whenever ref_countof anobject becomes zero, we must also decrement the

    ref_countof each object pointed to by a reference within

    the object

    Maintaining Reference Counts

  • 8/10/2019 CS F301-L19-21

    54/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Reference Count Manipulation

  • 8/10/2019 CS F301-L19-21

    55/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Reference Count Manipulation

  • 8/10/2019 CS F301-L19-21

    56/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Node D is now unreachable.It is collected too.

    Reference Count Manipulation

    Indicated number is

    the reference count

    Reference Counting GC:

  • 8/10/2019 CS F301-L19-21

    57/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    High overhead due to reference maintenance Cannot collect unreachable cyclic data structures (ex:

    circularly linked lists), since the reference counts never

    become zero

    Garbage collection is incrementaloverheads aredistributed to the mutatorsoperations and are spread out

    throughout the life time of the mutator

    Garbage is collected immediately and hence spaceusage is low

    Useful for real-time and interactive applications, where

    long and sudden pauses are unacceptable

    Reference Counting GC:Advantages and Disadvantages

    Unreachable Cyclic Data

  • 8/10/2019 CS F301-L19-21

    58/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Unreachable Cyclic DataStructure

  • 8/10/2019 CS F301-L19-21

    59/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Example

    Concurrent garbage

  • 8/10/2019 CS F301-L19-21

    60/61

    CS/IS F301 First Semester 2013-14 BITS Pilani, Hyderabad Campus

    Two conditions to be satisfied The Memory space of the collection process and the

    memory allocation process are separated.

    The two process are synchronized when working on the

    same memory location.

    Two process in this process Mutator: allocate or release the memory locations.

    Collector: coverts the released memory blocks into free memory

    Concurrent garbagecollection

  • 8/10/2019 CS F301-L19-21

    61/61

    Needs info as to whether a cell is a data or pointer.

    Takes 20-30% of execution time.

    Start and stop GC has performance issues.

    Issues in GC