GC, Memory Performance, java.lang.ref - Lecture Slides | CMSC 433, Papers of Programming Languages

Material Type: Paper; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Spring 2003;

Typology: Papers

Pre 2010

Uploaded on 07/30/2009

koofers-user-7nh-1
koofers-user-7nh-1 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
49
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
Not much control over it
50
Increasing Memory Performance
Don’t allocate as much memory
Less work for your application
Less work for the garbage collector
Should improve performance
(Why only “should”?)
Don’t hold on to references
Null out pointers in data structures
Or use weak references
51
Find the Memory Leak
class Stack {
private Object[] stack;
private int index;
public Stack(int size) {
stack = new Object[size];
}
public void push(Object o) {
stack[index++] = o;
}
public void pop() {
return stack[index--];
}
}
From Haggar, Garbage Collection and the Java Platform Memory Model
52
Bad Ideas (Usually)
Calling System.gc()
This is probably a bad idea
You have no idea what the GC will do
And it will take a while
Managing memory yourself
Object pools, free lists, object recycling
GC’s have been heavily tuned to be efficient
53
java.lang.ref
Package that lets you interact with GC
If there’s a strong (normal) reference to an
object, GC will consider it live
Can use java.lang.ref to make references to
object that “don’t count”
public abstract class Reference {
void clear();
public Object get();
...
}
54
SoftReference
Constructor SoftReference(Object o)
Make a soft reference to object o
Access using get() method
GC may free object if
Only soft, weak, or phantom refs to it
No strong refs
GC invokes clear() on SoftRef if freed
pf2

Partial preview of the text

Download GC, Memory Performance, java.lang.ref - Lecture Slides | CMSC 433 and more Papers Programming Languages in PDF only on Docsity!

49

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
    • Not much control over it 50

Increasing Memory Performance

  • Don’t allocate as much memory
    • Less work for your application
    • Less work for the garbage collector
    • Should improve performance
      • (Why only “should”?)
  • Don’t hold on to references
    • Null out pointers in data structures
    • Or use weak references 51

Find the Memory Leak

class Stack { private Object[] stack; private int index; public Stack(int size) { stack = new Object[size]; } public void push(Object o) { stack[index++] = o; } public void pop() { return stack[index--]; } }

  • From Haggar, Garbage Collection and the Java Platform Memory Model 52

Bad Ideas (Usually)

  • Calling System.gc()
    • This is probably a bad idea
    • You have no idea what the GC will do
    • And it will take a while
  • Managing memory yourself
    • Object pools, free lists, object recycling
    • GC’s have been heavily tuned to be efficient 53

java.lang.ref

  • Package that lets you interact with GC
    • If there’s a strong (normal) reference to an object, GC will consider it live
    • Can use java.lang.ref to make references to object that “don’t count” public abstract class Reference { void clear(); public Object get(); ... } 54

SoftReference

  • Constructor SoftReference(Object o)
    • Make a soft reference to object o
    • Access using get() method
  • GC may free object if
    • Only soft, weak, or phantom refs to it
      • No strong refs
  • GC invokes clear() on SoftRef if freed

55

WeakReference

  • Constructor WeakReference(Object o)
  • GC will free object if
    • Only weak or phantom refs to it
      • No strong or soft refs
  • GC invokes clear() on WeakRef if freed 56

PhantomReference

  • We won’t talk about these
    • Need to be used with ReferenceQueues
    • ...which we also haven’t discussed 57

Uses of Soft and Weak Refs

  • Use soft references for caches
    • Data can be recomputed/reconstructed
    • GC flushes cache (clears soft refs) if memory full
  • Use weak references for mappings
    • E.g., object reuse (if you’re going to do it...)
    • GC frees object once strong refs are gone 58

Caveat for Weak References

  • Which is correct? WeakReference wr; WeakReference wr; obj = wr.get(); obj = wr.get(); if (obj == null) { if (obj == null) { obj = new A(); wr = new WeakRef(new A()); wr = new WeakRef(obj); obj = wr.get(); } }
  • From Haggar, Garbage Collection and the Java Platform Memory Model 59

Caveat for Weak Refs (cont’d)

  • Left side is correct
    • Consider new WeakRef(new A())
    • Object A allocated
    • Put into WeakReference
    • Suppose GC happens right after
    • Then A may be deallocated before wr.get() 60

Dealing with GC Problems

  • Best idea: Measure where your problems are coming from - Find a Java implementation that has lots of heap profiling information - Understand thoroughly what’s happening - Find solution
  • No easy solution
    • But don’t try to optimize too early