Memory Management in Java: Garbage Collection and Memory Management Techniques, Study notes of Programming Languages

An overview of memory management in java, focusing on garbage collection and various memory management techniques such as reference counting, mark and sweep, and copying gc. It covers the concepts of local variables, heap memory, performance criteria, and garbage collection algorithms. The document also discusses the tradeoffs and considerations for each technique.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-4f3
koofers-user-4f3 🇺🇸

9 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2006
Memory Management
2
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Memory Management in Java: Garbage Collection and Memory Management Techniques and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2006

Memory Management

2

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

3

Memory Mgmt and the JVM

• The JVM Specification doesn’t say how to

manage the heap

• Simplest valid memory management

strategy: never delete any objects

  • Not such a bad idea in some circumstances

(when?)

  • Need to consider relevant performance criteria 4

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?

7

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? 8

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

9

Reference Counting

  • Old technique (1960)
  • Each object tracks the number of pointers to it

from other objects and from the roots.

  • When count reaches 0, object can be deallocated
  • Counts incremented/decremented by the compiler

(auto) or by hand (manual) when program

statements create or remove aliases.

10

Reference Counting Example

stack 1 1

13

Reference Counting Example

stack 1 1

14

Reference Counting Example

stack 1 2 1

15

Reference Counting Example

stack 1 2 1

16

Reference Counting Example

stack 1

19

Mark and Sweep Example

stack 20

Mark and Sweep Example

stack

21

Mark and Sweep Example

stack 22

Mark and Sweep Example

stack

25

Mark and Sweep Example

stack 26

Tradeoffs with Mark and Sweep

• Pros:

  • No problem with cycles
  • Memory writes/dropped aliases have no cost

• Cons:

  • Fragmentation
  • Cost proportional to heap size
    • Sweep phase needs to traverse whole heap

27

Copying GC

• Like mark and sweep, but only touches live

objects

  • Divide heap into two equal parts (semispaces)
  • Only one semispace active at a time
  • GC copies data from one to the other
    • Trace the live data starting from the stack
    • Copy live data into other semispace
    • Declare everything in current semispace dead; switch to other semispace 28

Copying GC Example

stack

31

Copying GC Example

stack ① ①

32

Copying GC Tradeoffs

• Pros:

  • Only touches live data
  • No fragmentation; automatically compacts
    • Will probably increase locality

• Cons:

  • Requires twice the memory space
  • Like mark and sweep, need to stop the world

33

The Generational Principle

Object lifetime increasesMore objects live

“Young objects die quickly; old objects keep living” 34

Generational Collection

• Divide heap into generations

• Objects that survive many collections get

pushed into older generations

  • Older generations collected less often
  • Need to track pointers from old to young

generations to use as roots for young generation

collection

  • Usually implemented with a “write barrier”

37

Scaling up GC

  • Incremental collection
    • GC runs in mutator thread, but runs only for a short time and then resumes
  • Concurrent collection
    • GC thread runs in parallel with the mutator
  • Parallel collection
    • Many GC threads, all running in parallel
  • All techniques require read/write barriers, which

can be expensive

38

HotSpot VM

• Parallel collection of young space

  • Easy: each thread has its own young generation

• Concurrent collection of tenured space

• Incremental collection of tenured space

  • Does a little work with each minor collection

• All enabled by separate flags

39

Metronome

• GC developed by IBM research, targetting

real-time systems.

• Uses parallel, concurrent collection

• Can provide strong guarantees of pause time

and throughput.

40

What Does GC Mean to You?

• Ideally, nothing

  • It should make your life easier
  • And shouldn’t affect performance too much
    • May even give better performance than you’d have with explicit deallocation

• If GC becomes a problem, hard to solve

  • You can set parameters of the GC
  • You can modify your program