java memory management & gc

14,250 views
13,953 views

Published on

Tech share java memory management & gc

Published in: Technology
5 Comments
27 Likes
Statistics
Notes
No Downloads
Views
Total views
14,250
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
0
Comments
5
Likes
27
Embeds 0
No embeds

No notes for slide

java memory management & gc

  1. 1. Tech share<br />Java Memory Management<br />
  2. 2. Garbage Collection Responsibility <br />Allocating memory<br />Ensuring that any referenced objects remain in memory<br />Recovering memory used by objects that are no longer reachable from references in executing code. <br />
  3. 3. Generation Collection<br />Most allocated objects die young<br />Few references from older to younger objects exist.<br />
  4. 4. Sun hotspot memory model<br />Young Generation<br />Eden<br />Survivor From<br />Survivor To<br />Tenured (Old) Generation<br />Permanent<br />objects describing classes and methods<br />as well as the classes and methods themselves<br />
  5. 5. Garbage Collection Types<br />young generation collection (minor collection)<br />young generation fills up<br />full collection (major collection)<br /> old or permanent generation fills up<br />System.gc()<br />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. <br />
  6. 6. Fast Allocation<br />bump-the-pointer technique<br /> large contiguous blocks of memory available<br />Thread-Local Allocation Buffers (TLABs)<br />multithread-safe<br />without global locks<br />-XX:TLABWasteTargetPercent (n/Eden) <br />-XX:+PrintTLAB<br />
  7. 7. Hotspot Collectors<br />Serial Collector<br />Parallel Collector<br />Parallel Compacting Collector<br />Concurrent Mark-Sweep (CMS) Collector<br />
  8. 8. Hotspot Default garbage collector <br />Java –version<br />java version "1.6.0_23"<br />Java(TM) SE Runtime Environment (build 1.6.0_23-b05)<br />Java HotSpot(TM) Client VM (build 19.0-b09, mixed mode, sharing) <br />Server : parallel collector<br />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.<br />Client: Serial collector<br />
  9. 9. Serial Collector<br />using a single CPU<br />in a stop-the-world fashion<br />
  10. 10. Serial Collector<br />Yong Generation Collection<br />too large objects are directly copied to old generation<br />-XX:InitialTenuringThreshold=7<br />-XX:MaxTenuringThreshold=<br />
  11. 11. Serial Collector <br />Old Generation Collection<br />mark-sweep-compact<br />Mark<br />Mark live objects<br />Sweep<br />Sweep unmarked objects<br />Compact<br />For bump-the-pointer<br />
  12. 12. Serial Collector<br />When to Use:<br />do not have a requirement for low pause times<br />On today’s hardware, less than half a second for full collections (64MB heaps)<br />1024MB/64MB=16*0.5second = 8second<br />Usage:<br />-XX:+UseSerialGC<br />
  13. 13. Parallel Collector<br />also known as the throughput collector<br />-XX:+PrintGCDetails -XX:+PrintTLAB -XX:MaxTenuringThreshold=7 -XX:PretenureSizeThreshold=2M -XX:+PrintGCTimeStamps -Xms30M -Xmx30M -Xmn2M -XX:+UseParallelGC<br />PSYoungGen [0x085f0000, 0x087f0000, 0x087f0000)<br />eden space [0x085f0000,0x087673c0,0x08770000)<br /> from space [0x087b0000,0x087b0000,0x087f0000)<br /> to space [0x08770000,0x08770000,0x087b0000)<br />PSOldGen [0x069f0000, 0x085f0000, 0x085f0000)<br /> object space [0x069f0000,0x070f0070,0x085f0000)<br />
  14. 14. Parallel Collector<br />Young Generation Collection<br />still a stop-the-world and copying collector<br />in parallel<br />using many CPUs<br />
  15. 15. Parallel Collector<br />Old Generation Collection<br />Still mark-sweep-compact <br />serial operation<br />When to use:<br />often appropriate include those that do batch processing, billing, payroll, scientific computing, and so on.<br />Usage:<br />-XX:+UseParallelGC<br />
  16. 16. Parallel Compacting Collector<br />was introduced in J2SE 5.0 update 6<br />Note: <br />willreplace the parallel collector.<br />Young Generation Collection<br /> Same as parallel collector<br />
  17. 17. Parallel Compacting Collector<br />Old Generation Collection<br /> marking phase<br />logically divided into fixed-sized regions<br />GCmarklive objects with multi-thread<br />–XX:ParallelGCThreads=n (By default on a host with N CPUs)<br />summary phase (serial operation)<br />starting with the leftmost one to examine the density of the regions<br />Calculates and stores the new location of the first byte of live data for each compacted region<br />compaction phase<br />Compact use by summary data<br />
  18. 18. Parallel Compacting Collector<br />When to use:<br />more than one CPU<br />reduces pause times (multi-thread)<br />Usage:<br />-XX:+UseParallelOldGC<br />-XX:ParallelGCThreads=n<br />
  19. 19. Concurrent Mark-Sweep (CMS) Collector<br />also known as the low-latency collector.<br />Young Generation Collection<br /> Same as parallel collector<br />
  20. 20. Concurrent Mark-Sweep (CMS) Collector<br />Old Generation Collection:<br />identifies the initial set of live objects directly reachable from the application code<br />marks all live objects that are transitively reachable from this set<br />
  21. 21. Concurrent Mark-Sweep (CMS)<br />disadvantage<br />only collector that is non-compacting<br />requirement for larger heap sizes than the other collectors<br />CMS Incremental Mode<br />periodically stopping the concurrent phase to<br />yield back processing to the application<br />–XX:+CMSIncrementalMode<br />
  22. 22. Concurrent Mark-Sweep (CMS)<br />When to use:<br />applications that have a relatively large set of long-lived data (a large old generation)<br />run on machines with two or more processors<br />for any application with a low pause time requirement<br />Usage:<br /> -XX:+UseConcMarkSweepGC<br />–XX:+CMSIncrementalMode (Incremental Mode)<br />
  23. 23. CMS log<br />2.259: [GC [1 CMS-initial-mark: 4280K(5120K)] 6042K(18944K), 0.0003876 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] <br />2.260: [CMS-concurrent-mark-start]<br />2.267: [CMS-concurrent-mark: 0.007/0.007 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] <br />2.267: [CMS-concurrent-preclean-start]<br />2.267: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] <br />2.267: [GC[YG occupancy: 1761 K (13824 K)]2.268: [Rescan (parallel) , 0.0001977 secs]2.268: [weak refs processing, 0.0000046 secs] [1 CMS-remark: 4280K(5120K)] 6042K(18944K), 0.0003386 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] <br />2.268: [CMS-concurrent-sweep-start]<br />2.269: [CMS-concurrent-sweep: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] <br />2.269: [CMS-concurrent-reset-start]<br />2.269: [CMS-concurrent-reset: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] <br />
  24. 24. Disadvantage of non-compactingCode<br />static int alloc_1MB = 1024 * 1024 * 1;<br />public static void main(String[] args) throws Exception {<br /> //UseConcMarkSweepGC<br /> byte[] bytes10 = alloc(); alloc();<br /> byte[] bytes12 = alloc(); alloc();<br /> byte[] bytes14 = alloc(); alloc();<br /> byte[] bytes16 = alloc(); alloc();<br /> byte[] bytes18 = alloc(); alloc();<br /> byte[] bytes20 = alloc(); alloc();<br /> byte[] bytes22 = alloc();<br />alloc(3);<br /> }<br />static int count = 0;<br /> private static byte[] alloc() {<br /> return alloc(1);<br /> }<br /> private static byte[] alloc(inti) {<br /> count = count + 1 * i ;<br />System.out.println(count + "M");<br /> return new byte[alloc_1MB * i];<br /> }<br />
  25. 25. Disadvantage of non-compactingresult of Parallel&ParallelOld<br />-XX:+UseParallelGC -XX:PretenureSizeThreshold=1k -XX:MaxTenuringThreshold=30 -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:-PrintTenuringDistribution<br />PSYoungGen total 8960K, used 5336K<br />eden space 7680K, 69% used<br /> from space 1280K, 0% used <br /> to space 1280K, 0% used <br />PSOldGen total 10240K, used 6598K <br /> object space 10240K, 64% used<br />PSPermGen total 16384K, used 4969K <br /> object space 16384K, 30% used <br />
  26. 26. Disadvantage of non-compactingResult of CMS<br />-XX:+UseConcMarkSweepGC -XX:PretenureSizeThreshold=1k -XX:MaxTenuringThreshold=30 -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:-PrintTenuringDistribution<br />Exception in thread "main" java.lang.OutOfMemoryError: Java heap space<br /> par new generation total 9216K, used 491K <br />eden space 8192K, 6% used <br /> from space 1024K, 0% used <br /> to space 1024K, 0% used<br /> concurrent mark-sweep generation total 10240K, used 398K <br /> concurrent-mark-sweep perm gen total 16384K, used 4947K<br />
  27. 27. VM options<br />-XX:MaxGCPauseMillis=n<br />Pause time<br />-XX:GCTimeRatio=99<br />1 / (1 + n)<br />Throughput<br />-Xmx –Xms<br />-Xmn (=eden+survivor*2)<br />–XX:SurvivorRatio=32 (1:34)<br />Survivor:Eden<br />-XX:MaxPermSize<br />
  28. 28. Print gc<br />–XX:+PrintGC<br />–XX:+PrintGCDetails<br />–XX:+PrintGCTimeStamps<br />-verbose:gc<br />[GC 325407K->83000K(776768K), 0.2300771 secs]<br />[GC 325816K->83372K(776768K), 0.2454258 secs]<br />[Full GC 267628K->83769K(776768K), 1.8479984 secs]<br />
  29. 29. example<br />-XX:MaxTenuringThreshold=7<br />-XX:MaxTenuringThreshold=0 -Xms30M -Xmx30M -Xmn10M -XX:+UseSerialGC<br /> def new generation total 9216K, used 1106K [0x30be0000, 0x315e0000, 0x315e0000)<br />eden space 8192K, 13% used [0x30be0000, 0x30cf4830, 0x313e0000)<br />from space 1024K, 0% used [0x314e0000, 0x314e0000, 0x315e0000)<br /> to space 1024K, 0% used [0x313e0000, 0x313e0000, 0x314e0000)<br />-XX:MaxTenuringThreshold=0<br />-XX:MaxTenuringThreshold=7 -Xms30M -Xmx30M -Xmn10M -XX:+UseSerialGC<br /> def new generation total 9216K, used 1292K [0x30be0000, 0x315e0000, 0x315e0000)<br />eden space 8192K, 13% used [0x30be0000, 0x30cf4890, 0x313e0000)<br />from space 1024K, 18% used [0x314e0000, 0x3150e8b0, 0x315e0000)<br /> to space 1024K, 0% used [0x313e0000, 0x313e0000, 0x314e0000)<br /><ul><li>Error example</li></ul>-XX:MaxTenuringThreshold=0 –Xms20M –Xmx20M -Xmn18M -XX:+UseSerialGC<br />Exception in thread "main" java.lang.OutOfMemoryError: Java heap space<br />Notice:<br />for Serial collector & CMS<br />
  30. 30. System.gc() & finalize()<br />Code:<br />public class SerialTest {<br /> public static void main(String[] args) throws Exception {<br /> new SerialTest();<br />System.gc();<br />Thread.sleep(10);<br />System.out.println("123");<br /> }<br />@Override<br /> protected void finalize() throws Throwable {<br />System.out.println("heloo================finalize");<br /> }<br />}<br />Result:<br />0.227: [Full GC (System) TLAB: gc thread: 0x08839400 [id: 5820] ………..<br />heloo================finalize<br />123<br />
  31. 31. finalize<br /> twice GC !!!<br />
  32. 32. monitor<br />JVisualVM<br />JConsole<br />JRockit mission control<br />
  33. 33. Garbage-First Garbage Collector<br />G1 GC for short<br />Is a new GC that is being introduced in the Java HotSpot VM in JDK 7<br />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). <br />
  34. 34. Garbage-First Garbage Collector<br />Parallelism and Concurrency.<br />G1 performs heap compaction<br />there is a single contiguous heap which is split into same-sized regions<br />Young/old generation is a set of potentially non-contiguous regions <br />
  35. 35. G1 Collector<br />Heap<br /> garbage-first heap total 20480K, used 3491K<br /> region size 1024K, 3 young (3072K), 0 survivors (0K)<br /> compacting perm gen total 16384K, used 4967K<br /> the space 16384K, 30% used<br />No shared spaces configured.<br />
  36. 36. G1 Collector<br />RS: regon set<br />0.634: [GC pause (young), 0.00846287 secs]<br /> [Parallel Time: 8.3 ms]<br /> [GC Worker Start Time (ms): 633.9 634.3]<br /> [Update RS (ms): 0.0 0.0<br />Avg: 0.0, Min: 0.0, Max: 0.0]<br /> [Processed Buffers : 0 5<br /> Sum: 5, Avg: 2, Min: 0, Max: 5]<br /> [Ext Root Scanning (ms): 3.6 3.3<br />Avg: 3.5, Min: 3.3, Max: 3.6]<br /> [Mark Stack Scanning (ms): 0.0 0.0<br />Avg: 0.0, Min: 0.0, Max: 0.0]<br /> [Scan RS (ms): 0.0 0.0<br />Avg: 0.0, Min: 0.0, Max: 0.0]<br /> [Object Copy (ms): 3.8 3.6<br />Avg: 3.7, Min: 3.6, Max: 3.8]<br /> [Termination (ms): 0.0 0.0<br />Avg: 0.0, Min: 0.0, Max: 0.0]<br /> [Termination Attempts : 1 1<br /> Sum: 2, Avg: 1, Min: 1, Max: 1]<br /> [GC Worker End Time (ms): 641.3 641.3]<br /> [Other: 1.1 ms]<br /> [Clear CT: 0.0 ms]<br /> [Other: 0.2 ms]<br /> [Choose CSet: 0.0 ms]<br /> [ 2868K->1763K(20M)]<br /> [Times: user=0.00 sys=0.00, real=0.00 secs] <br />
  37. 37. comparison<br />
  38. 38. references<br />http://java.sun.com/products/hotspot/whitepaper.html<br />http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html<br />http://blogs.oracle.com/watt/resource/jvm-options-list.html<br />http://www.iteye.com/topic/802638<br />http://blog.csdn.net/calvinxiu/archive/2007/05/18/1614473.aspx<br />http://unixboy.iteye.com/blog/174173<br />http://java.sun.com/performance/reference/whitepapers/tuning.html<br />

×