2. What does garbage collection do?
● Find live objects
● Remove dead objects
● Compact free memory
3. What is in the Heap?
● Eden (New Objects)
● Survivor Space (Survived at least one Minor GC)
● Old Generation (Survived some number of Minor GCs or out of memory in
survivor space)
4. How do objects age in the heap?
An object’s age is determined by the number of GCs it has survived.
7. Old Generation
Contains objects that have hit a certain age and objects that were promoted, due to a
lack of space in the survivor space.
Only gets cleaned up when filled up.
If it has a reference to a new object and the object in old gen is no longer pointed to,
the new object will not be garbage collected until the old one is.
8. PermGen
A space for memory that is garbage collected separately
Things included in PermGen
● ClassLoaders
● Interned Strings/String pool (Removed from PermGen in Java 7)
10. Old GC and Concurrent Mark and Sweep Heap
● 1 Eden
● 2 Survivor Spaces (SS0, SS1)
● 1 Old Generation
11. Old Parallel GC
Allocates until Eden fills up
Find survivors and move to SS0
Allocates until Eden fills up
Moves Survivors from Eden and SS0 to SS1
Repeat until Survivor Space is full then move survivors to Old Gen
Repeat until Old Gen is full then do a full GC (Old Gen is compacted here)
12. Concurrent Mark and Sweep
6 Phases
1. Initial Mark : Stops all threads and scans for object references from the stack and
from static variables (object roots)
2. Concurrent Mark: Finds all live objects from objects object roots
3. Concurrent Preclean: Remarks objects as dead or alive as application runs
4. Remark: Stops all threads and rescans from the object roots
5. Concurrent Sweep: Adds dead objects to free list.
6. Concurrent Reset: Cleans up internal data structures
15. G1
G1 stands for Garbage First
Mix between Old GC and CMS
Performs Old GC on the memory of the segments that get full
Performs something similar CMS on the actual segments.
16.
17.
18.
19.
20.
21. What is wrong here?
String repeat = “”;
for ( int i = 0; i < 100; ++i ) {
repeat += “foo”;
}
return repeat;
25. StringBuilder builder = new StringBuilder(100 * “foo”.length());
for ( int i = 0; i < 100; ++i ) {
builder.append(“foo”);
}
return builder.toString();