Announcements - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Announcements

Description:

Have to account for the descendents also. Garbage Collection. Reference Counting ... The descendents are copied as well. Forwarding. Swap the roles of ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 34
Provided by: ashok5
Category:

less

Transcript and Presenter's Notes

Title: Announcements


1
Announcements
  • 2nd Midterm Friday
  • What ever is covered since last midterm and
    through Wednesday
  • Multiple choice
  • Closed book/notes

2
Memory Management
3
Basic Data Types
  • Values held in machine locations
  • Integers, reals, characters, Booleans
  • Built into languages
  • Efficiently implemented and used freely
  • Others built on top of them structured types
  • Laid out in sequence of locations in the machine
  • Arrays, records, pointers.
  • First class citizens

4
First class citizens
  • Denoted by name
  • Value of an expression
  • Appear in the RHS of an assignment
  • Used for comparison
  • Passed as parameters

5
Arrays
  • A sequence of elements of the same type
  • Element can be accessed quickly O(1)
  • Accessed via indexing
  • Ai i-gt index
  • Index is often an integer
  • Does not have to be
  • Must be efficiently computed e.g. char
  • When is array bound computed?
  • When is the space for array allocated?

6
Arrays Layout
  • Determines the machine address of the ith
    element relative to the address of the first
    element
  • Different from allocation
  • Reserve actual machine memory for the array
  • The elements of the array appear in consecutive
    locations

7
Arrays Layout(Java)
8
Arrays
  • var A array low .. high of T
  • base
  • Starting address of the first element Alow
  • width
  • size of an element of type T
  • The elements are stored at
  • base, basewidth, base 2width .
  • Address of Ai computed in 2 parts
  • Compile time offset from base
  • Run time location of base

9
Arrays
  • Address of Ai
  • base (i-low)width
  • iwidth (base-lowwidth)
  • (base-lowwidth) may be precomputed and stored
  • iwidth must be computed at runtime
  • If low 0
  • Address of Ai iwidth base
  • Time to compute the address is independent of i
  • Constant access time

10
Multidimensional Arrays
  • Common in all languages
  • C A200200
  • Allocated in linear fashion
  • Row major
  • Store by rows row 1, row 2, row 3, .
  • Column major
  • Store by columns

11
Multidimensional Arrays Layout(Java)
12
Multidimensional Arrays
  • Address of Mij
  • Fixed part
  • Variable part

13
Arrays(C)
  • Layout statically at compile time
  • Allocation procedure entry
  • Keyword static -gt static allocation
  • Retain values from one procedure to next
  • Pascal does not allow
  • int produce()
  • static char buffer128
  • char temp128

14
Array Bounds
  • When are they computed?
  • Static compile time constants 20,30
  • Upon procedure entry Algol 60
  • Dynamic C new char20

15
ArraysInitialization
  • Default
  • Depends on the language
  • Java has predefined values
  • May be done in declaration
  • int x 1,2,3,4,5,6,7
  • Size determined from the number

16
Records
  • Multiple values grouped together
  • Heterogeneous Elements
  • Variables relevant to an object to be grouped
    together and treated as a unit
  • Examples
  • record
  • ltname1gt lttype1gt
  • ltname2gt lttype2gt
  • ..
  • ltnamekgt lttypekgt
  • end
  • type complex record re, im real end

17
RecordsLayout
18
Records
  • Memory allocation at compile time
  • Operations to access them
  • Dot operator x.re, x.im
  • Assignment?
  • PASCAL allows
  • C?

19
Arrays vs. Records
20
Sets
  • Pascal allows set data type
  • Example
  • , 0..9, , -,
  • var a set of 1..3
  • Set Operations
  • Union
  • Set difference
  • Intersection
  • / Symmetric difference (A-B) U (B-A)

21
Pointers
  • Indirect access to elements of known type
  • Motivation Indirect access in m/c language
  • Pointer points to objects of a specific type
  • Fixed size, independent of type single machine
    location
  • Why?
  • Efficiency Avoid move/copy large data
    structures
  • Dynamic Data Allow data structures to grow and
    shrink during execution

22
Pointers
  • First class citizens
  • Operations
  • Allocation alloc, new
  • Dereferencing
  • Assignment
  • Equality testing
  • Deallocation free, dispose
  • NULL pointer NULL, nil (pascal)

23
Dangling Pointers
  • Dangling pointer/Widow
  • A pointer to storage used for another purpose
    and the storage is subsequently deallocated
  • Garbage/Orphan
  • Allocated but inaccessible memory locations
  • Programs that create garbage are called to have
    memory leaks
  • Example
  • pq free(q)

24
Arrays and Pointers in C
  • Intimately related
  • If A is an array
  • A points to A0
  • If p points to Ai, p1 points to Ai1

25
MiscellaneousVariable binding
  • Associate a property with a name (variable)
  • Early (static)
  • before program runs
  • Late (dynamic)
  • when the program runs
  • Types
  • Early for C/Pascal, late for LISP
  • Value
  • Dynamic for both

26
Garbage Collection
  • Motivation from functional programming
  • Increased importance due to OOP
  • How do we reduce/eliminate the burden of
    memory management from the programmer?

27
Garbage Collection
  • Programmer explicitly manages the heap
  • Allocate memory
  • Free memory (sometimes)
  • Problems
  • Memory management is not central to the problem
    the programmer is trying to solve
  • May run out of memory if not done efficiently.

28
Memory Management Issues
  • class node
  • int value
  • node next
  • node p,q
  • pnew node()
  • qnew node()
  • qp
  • delete(p)

29
Garbage Collection
  • Dangling reference/Widow
  • A pointer to storage used for another purpose
    and the storage is subsequently deallocated
  • Garbage/Orphan
  • Allocated but inaccessible memory locations
  • Programs that create garbage are called to have
    memory leaks

30
Garbage Collection
  • Imperative languages ask the programmer to manage
    the heap
  • C, C,
  • OOP and functional languages generally do
    automatic garbage collection
  • Java, Lisp,

31
Garbage CollectionReference Counting
  • Assumption of a free list
  • Heap is a continuous chain of nodes
  • Each node has an extra field to keep a count
  • Reference Count
  • Number of pointers referencing that node
  • Initially set to 0

32
Garbage CollectionReference Counting
  • Allocation
  • Get nodes from the free list
  • Set reference count to 1
  • Pointer Assignment
  • Increment the reference count of the source by 1
  • Decrement the reference count of the target by 1
  • Deallocation
  • Return nodes to the free list

33
Garbage CollectionReference Counting
  • Activate the algorithm dynamically
  • new
  • delete
  • Return as many orphans as possible
  • Orphan Any node whose reference count is 0
  • Have to account for the descendents also

34
Garbage CollectionReference Counting
35
Garbage CollectionReference Counting
  • Problems
  • Cant handle circular chains of nodes

p.nextnull
36
Garbage CollectionReference Counting
  • Disadvantages
  • Circular chains
  • Storage overhead (reference count at each node)
  • Advantage
  • Occurs dynamically leading to distributed load

37
Garbage Collection Mark-Sweep
  • Called when the heap becomes full
  • i.e. free list becomes empty
  • Orphans are reassigned to the free list
  • Possibly large number of nodes
  • May be time consuming
  • 2 Pass algorithm
  • 1st pass Mark all the nodes if they are
    accessible
  • 2nd pass Reassign the orphans

38
Garbage Collection Mark-Sweep
  • Mark Phase
  • Start with the active variables
  • Follow the links and mark the nodes that can be
    accessed
  • All unmarked nodes are orphans
  • Sweep Phase
  • Follow all nodes in the heap
  • If the node is unmarked return to free list
  • Unmark all nodes that were not returned

39
Garbage Collection Mark-Sweep
After Sweep Phase
After Mark Phase
40
Garbage Collection Mark-Sweep
  • Advantages
  • Not invoked unless needed
  • Small programs dont need it
  • Typically perform a large number of new/delete
    before this is needed
  • Reclaims all garbage
  • No problem with circular chains
  • Reduced memory overhead
  • Integer vs. a bit
  • Disadvantages
  • Time consuming when used
  • 2 pass algorithm

41
Garbage Collection Copy Collection
  • Time-space compromise in Mark-Sweep
  • Invoked only when heap becomes full
  • Significantly faster than Mark-Sweep
  • Only 1 pass over the heap
  • Heap size is effectively reduced by half

42
Garbage Collection Copy Collection
  • Divide the heap into two equal halves
  • from_space All active nodes are kept here.
  • to_space Used as a copy buffer
  • When the from_space becomes full
  • All accessible nodes are copied into to_space
  • The descendents are copied as well
  • Forwarding
  • Swap the roles of from_space and to_space
  • Eliminates the inaccessible nodes

43
Garbage Collection Copy Collection
After Copy Collection Activation
Initial Heap Organization
44
Garbage Collection Copy Collection
  • Performance
  • No. of active heap blocks R
  • Heap size n-h
  • Efficiency
  • Amount of memory reclaimed in unit time
  • Residency
  • Ratio of number of active blocks and heap size
    (r)

45
  • The tools we use have a profound (and devious!)
    influence on our thinking habits, and, therefore,
    on our thinking abilities.
  • Dijkstra, 1975
Write a Comment
User Comments (0)
About PowerShow.com