Docsity

Lecture Notes on Memory Management Program Language Technique | CMSC 433, Papers of Programming Languages

Partial preview of the text

Download Lecture Notes on Memory Management Program Language Technique | CMSC 433 and more Papers Programming Languages in PDF only on Docsity!

CMSC 433 – Programming Language

Technologies and Paradigms

Fall 2006

Memory Management

Memory Management in Java

• Local variables live on the stack

  • Allocated at method invocation time
  • Deallocated when method returns

• Other data lives on the heap

  • Memory is allocated with new
  • But never explicitly deallocated
    • Java uses automatic memory management

Performance Criteria

• Throughput

  • How much work does my application complete?

• Latency (promptness)

  • How long might my application pause (due to a

memory management)?

• Memory footprint

  • How much memory is required by the

application above what’s needed for its work?

Garbage Collection (GC)

• At any point during execution, can divide

the objects in the heap into two classes:

  • Live objects will be used later
  • Dead objects will never be used again
    • They are garbage

• Idea: Can reuse memory from dead objects

Reachability

  • An object is reachable if it can be accessed by

chasing pointers from live data

  • Safe policy: delete unreachable objects
    • An unreachable object can never be accessed again by the program (the object is definitely garbage).
    • A reachable object may be accessed in the future (the object could be garbage but will be retained anyway). - Could lead to memory leaks
  • How to implement reachability?
    • What data is live?
    • How to perform pointer chasing?

Roots

  • At a given program point, we define liveness as

being data reachable from the root set:

  • Global variables (i.e., static fields)
  • Local variables of all live method activations (i.e., the stack)
  • At the machine level, we also consider the register

set (usually stores local or global variables)

  • Next: techniques for pointer chasing

Reference Counting Example

stack 1 1

Reference Counting Example

stack 1 1

Reference Counting Example

stack 1 1

Reference Counting Example

stack 1 2 1

Reference Counting Example

stack 1

Tradeoffs with Ref Counting

  • Advantage: Incremental technique
    • Small amount of work per memory write
    • With more effort, can bound running time
      • Useful for real-time systems
  • Problems:
    • Data on cycles can’t be collected; counts never go to 0
    • Cascading decrements can be expensive
      • E.g., when an aggregate data structure that is dropped

Mark and Sweep Example

stack

Mark and Sweep Example

stack