The document discusses Java memory management and garbage collection. It describes the responsibilities of garbage collection as allocating memory, ensuring referenced objects remain in memory, and recovering memory from unreachable objects. It discusses generation collection and the Hotspot memory model with young and old generations. The major garbage collectors are described as serial, parallel, parallel compacting, and concurrent mark-sweep collectors. The document provides examples of VM options and logging output.
2. Garbage Collection Responsibility Allocating memory Ensuring that any referenced objects remain in memory Recovering memory used by objects that are no longer reachable from references in executing code.
3. Generation Collection Most allocated objects die young Few references from older to younger objects exist.
4. Sun hotspot memory model Young Generation Eden Survivor From Survivor To Tenured (Old) Generation Permanent objects describing classes and methods as well as the classes and methods themselves
5. Garbage Collection Types young generation collection (minor collection) young generation fills up full collection (major collection) old or permanent generation fills up System.gc() the old generation collection algorithm is used on : Sometimes the old generation is too full to accept all the objects that would be likely to be promoted from the young generation to the old generation if the young generation was collected first.
6. Fast Allocation bump-the-pointer technique large contiguous blocks of memory available Thread-Local Allocation Buffers (TLABs) multithread-safe without global locks -XX:TLABWasteTargetPercent (n/Eden) -XX:+PrintTLAB
8. Hotspot Default garbage collector Java –version java version "1.6.0_23" Java(TM) SE Runtime Environment (build 1.6.0_23-b05) Java HotSpot(TM) Client VM (build 19.0-b09, mixed mode, sharing) Server : parallel collector Note: For Java SE 6, the definition of a server-class machine is one with at least 2 CPUs and at least 2GB of physical memory. Client: Serial collector
10. Serial Collector Yong Generation Collection too large objects are directly copied to old generation -XX:InitialTenuringThreshold=7 -XX:MaxTenuringThreshold=
11. Serial Collector Old Generation Collection mark-sweep-compact Mark Mark live objects Sweep Sweep unmarked objects Compact For bump-the-pointer
12. Serial Collector When to Use: do not have a requirement for low pause times On today’s hardware, less than half a second for full collections (64MB heaps) 1024MB/64MB=16*0.5second = 8second Usage: -XX:+UseSerialGC
13. Parallel Collector also known as the throughput collector -XX:+PrintGCDetails -XX:+PrintTLAB -XX:MaxTenuringThreshold=7 -XX:PretenureSizeThreshold=2M -XX:+PrintGCTimeStamps -Xms30M -Xmx30M -Xmn2M -XX:+UseParallelGC PSYoungGen [0x085f0000, 0x087f0000, 0x087f0000) eden space [0x085f0000,0x087673c0,0x08770000) from space [0x087b0000,0x087b0000,0x087f0000) to space [0x08770000,0x08770000,0x087b0000) PSOldGen [0x069f0000, 0x085f0000, 0x085f0000) object space [0x069f0000,0x070f0070,0x085f0000)
14. Parallel Collector Young Generation Collection still a stop-the-world and copying collector in parallel using many CPUs
15. Parallel Collector Old Generation Collection Still mark-sweep-compact serial operation When to use: often appropriate include those that do batch processing, billing, payroll, scientific computing, and so on. Usage: -XX:+UseParallelGC
16. Parallel Compacting Collector was introduced in J2SE 5.0 update 6 Note: willreplace the parallel collector. Young Generation Collection Same as parallel collector
17. Parallel Compacting Collector Old Generation Collection marking phase logically divided into fixed-sized regions GCmarklive objects with multi-thread –XX:ParallelGCThreads=n (By default on a host with N CPUs) summary phase (serial operation) starting with the leftmost one to examine the density of the regions Calculates and stores the new location of the first byte of live data for each compacted region compaction phase Compact use by summary data
18. Parallel Compacting Collector When to use: more than one CPU reduces pause times (multi-thread) Usage: -XX:+UseParallelOldGC -XX:ParallelGCThreads=n
19. Concurrent Mark-Sweep (CMS) Collector also known as the low-latency collector. Young Generation Collection Same as parallel collector
20. Concurrent Mark-Sweep (CMS) Collector Old Generation Collection: identifies the initial set of live objects directly reachable from the application code marks all live objects that are transitively reachable from this set
21. Concurrent Mark-Sweep (CMS) disadvantage only collector that is non-compacting requirement for larger heap sizes than the other collectors CMS Incremental Mode periodically stopping the concurrent phase to yield back processing to the application –XX:+CMSIncrementalMode
22. Concurrent Mark-Sweep (CMS) When to use: applications that have a relatively large set of long-lived data (a large old generation) run on machines with two or more processors for any application with a low pause time requirement Usage: -XX:+UseConcMarkSweepGC –XX:+CMSIncrementalMode (Incremental Mode)
25. Disadvantage of non-compactingresult of Parallel&ParallelOld -XX:+UseParallelGC -XX:PretenureSizeThreshold=1k -XX:MaxTenuringThreshold=30 -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:-PrintTenuringDistribution PSYoungGen total 8960K, used 5336K eden space 7680K, 69% used from space 1280K, 0% used to space 1280K, 0% used PSOldGen total 10240K, used 6598K object space 10240K, 64% used PSPermGen total 16384K, used 4969K object space 16384K, 30% used
26. Disadvantage of non-compactingResult of CMS -XX:+UseConcMarkSweepGC -XX:PretenureSizeThreshold=1k -XX:MaxTenuringThreshold=30 -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:-PrintTenuringDistribution Exception in thread "main" java.lang.OutOfMemoryError: Java heap space par new generation total 9216K, used 491K eden space 8192K, 6% used from space 1024K, 0% used to space 1024K, 0% used concurrent mark-sweep generation total 10240K, used 398K concurrent-mark-sweep perm gen total 16384K, used 4947K
33. Garbage-First Garbage Collector G1 GC for short Is a new GC that is being introduced in the Java HotSpot VM in JDK 7 also been released in Java SE 6 Update 14. G1 is the long-term replacement for HotSpot's low-latency Concurrent Mark-Sweep GC (widely referred to as CMS).
34. Garbage-First Garbage Collector Parallelism and Concurrency. G1 performs heap compaction there is a single contiguous heap which is split into same-sized regions Young/old generation is a set of potentially non-contiguous regions
35. G1 Collector Heap garbage-first heap total 20480K, used 3491K region size 1024K, 3 young (3072K), 0 survivors (0K) compacting perm gen total 16384K, used 4967K the space 16384K, 30% used No shared spaces configured.