SlideShare una empresa de Scribd logo
1 de 38
Tech share Java Memory Management
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.
Generation Collection Most allocated objects die young Few references from older to younger objects exist.
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
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.
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
Hotspot Collectors Serial Collector Parallel Collector Parallel Compacting Collector Concurrent Mark-Sweep (CMS) Collector
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
Serial Collector using a single CPU in a stop-the-world fashion
Serial Collector Yong Generation Collection too large objects are directly copied to old generation -XX:InitialTenuringThreshold=7 -XX:MaxTenuringThreshold=
Serial Collector  Old Generation Collection mark-sweep-compact Mark Mark live objects Sweep Sweep unmarked objects Compact For bump-the-pointer
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
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)
Parallel Collector Young Generation Collection still a stop-the-world and copying collector in parallel using many CPUs
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
Parallel Compacting Collector was introduced in J2SE 5.0 update 6 Note:  willreplace the parallel collector. Young Generation Collection  Same as parallel collector
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
Parallel Compacting Collector When to use: more than one CPU reduces pause times  (multi-thread) Usage: -XX:+UseParallelOldGC -XX:ParallelGCThreads=n
Concurrent Mark-Sweep (CMS) Collector also known as the low-latency collector. Young Generation Collection  Same as parallel collector
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
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
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)
CMS log 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]  2.260: [CMS-concurrent-mark-start] 2.267: [CMS-concurrent-mark: 0.007/0.007 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]  2.267: [CMS-concurrent-preclean-start] 2.267: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.01 sys=0.00, real=0.02 secs]  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]  2.268: [CMS-concurrent-sweep-start] 2.269: [CMS-concurrent-sweep: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]  2.269: [CMS-concurrent-reset-start] 2.269: [CMS-concurrent-reset: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Disadvantage of non-compactingCode static int alloc_1MB = 1024 * 1024 * 1; public static void main(String[] args) throws Exception {         //UseConcMarkSweepGC         byte[] bytes10 = alloc();  alloc();         byte[] bytes12 = alloc();  alloc();         byte[] bytes14 = alloc();  alloc();         byte[] bytes16 = alloc();  alloc();         byte[] bytes18 = alloc();  alloc();         byte[] bytes20 = alloc();  alloc();         byte[] bytes22 = alloc(); alloc(3);     } static int count = 0;     private static byte[] alloc() {         return alloc(1);     }     private static byte[] alloc(inti) {         count = count +  1 * i ; System.out.println(count + "M");         return new byte[alloc_1MB * i];     }
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
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
VM options -XX:MaxGCPauseMillis=n Pause time -XX:GCTimeRatio=99 1 / (1 + n) Throughput -Xmx –Xms -Xmn (=eden+survivor*2) –XX:SurvivorRatio=32 (1:34) Survivor:Eden -XX:MaxPermSize
Print gc –XX:+PrintGC –XX:+PrintGCDetails –XX:+PrintGCTimeStamps -verbose:gc [GC 325407K->83000K(776768K), 0.2300771 secs] [GC 325816K->83372K(776768K), 0.2454258 secs] [Full GC 267628K->83769K(776768K), 1.8479984 secs]
example -XX:MaxTenuringThreshold=7 -XX:MaxTenuringThreshold=0 -Xms30M -Xmx30M -Xmn10M  -XX:+UseSerialGC  def new generation   total 9216K, used 1106K [0x30be0000, 0x315e0000, 0x315e0000) eden space 8192K,  13% used [0x30be0000, 0x30cf4830, 0x313e0000) from space 1024K,   0% used [0x314e0000, 0x314e0000, 0x315e0000)   to   space 1024K,   0% used [0x313e0000, 0x313e0000, 0x314e0000) -XX:MaxTenuringThreshold=0 -XX:MaxTenuringThreshold=7 -Xms30M -Xmx30M -Xmn10M  -XX:+UseSerialGC  def new generation   total 9216K, used 1292K [0x30be0000, 0x315e0000, 0x315e0000) eden space 8192K,  13% used [0x30be0000, 0x30cf4890, 0x313e0000) from space 1024K,  18% used [0x314e0000, 0x3150e8b0, 0x315e0000)   to   space 1024K,   0% used [0x313e0000, 0x313e0000, 0x314e0000) ,[object Object],-XX:MaxTenuringThreshold=0 –Xms20M –Xmx20M -Xmn18M  -XX:+UseSerialGC Exception in thread "main" java.lang.OutOfMemoryError: Java heap space Notice: for Serial collector & CMS
System.gc()  &  finalize() Code: public class SerialTest {     public static void main(String[] args) throws Exception {         new SerialTest(); System.gc(); Thread.sleep(10); System.out.println("123");     } @Override     protected void finalize() throws Throwable { System.out.println("heloo================finalize");     } } Result: 0.227: [Full GC (System) TLAB: gc thread: 0x08839400 [id: 5820]  ……….. heloo================finalize 123
finalize  twice GC !!!
monitor JVisualVM JConsole JRockit mission control
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).
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
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.
G1 Collector RS: regon set 0.634: [GC pause (young), 0.00846287 secs]    [Parallel Time:   8.3 ms]       [GC Worker Start Time (ms):  633.9  634.3]       [Update RS (ms):  0.0  0.0 Avg:   0.0, Min:   0.0, Max:   0.0]          [Processed Buffers : 0 5           Sum: 5, Avg: 2, Min: 0, Max: 5]       [Ext Root Scanning (ms):  3.6  3.3 Avg:   3.5, Min:   3.3, Max:   3.6]       [Mark Stack Scanning (ms):  0.0  0.0 Avg:   0.0, Min:   0.0, Max:   0.0]       [Scan RS (ms):  0.0  0.0 Avg:   0.0, Min:   0.0, Max:   0.0]       [Object Copy (ms):  3.8  3.6 Avg:   3.7, Min:   3.6, Max:   3.8]       [Termination (ms):  0.0  0.0 Avg:   0.0, Min:   0.0, Max:   0.0]          [Termination Attempts : 1 1           Sum: 2, Avg: 1, Min: 1, Max: 1]       [GC Worker End Time (ms):  641.3  641.3]       [Other:   1.1 ms]    [Clear CT:   0.0 ms]    [Other:   0.2 ms]       [Choose CSet:   0.0 ms]    [ 2868K->1763K(20M)]  [Times: user=0.00 sys=0.00, real=0.00 secs]
comparison
references http://java.sun.com/products/hotspot/whitepaper.html http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html http://blogs.oracle.com/watt/resource/jvm-options-list.html http://www.iteye.com/topic/802638 http://blog.csdn.net/calvinxiu/archive/2007/05/18/1614473.aspx http://unixboy.iteye.com/blog/174173 http://java.sun.com/performance/reference/whitepapers/tuning.html

Más contenido relacionado

La actualidad más candente

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Java Presentation
Java PresentationJava Presentation
Java Presentationpm2214
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it worksMindfire Solutions
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivityTanmoy Barman
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Edureka!
 
Presentation on Core java
Presentation on Core javaPresentation on Core java
Presentation on Core javamahir jain
 
Garbage collection
Garbage collectionGarbage collection
Garbage collectionSomya Bagai
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Java Serialization
Java SerializationJava Serialization
Java Serializationimypraz
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with MockitoRichard Paul
 

La actualidad más candente (20)

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Java Basic Oops Concept
Java Basic Oops ConceptJava Basic Oops Concept
Java Basic Oops Concept
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
 
Presentation on Core java
Presentation on Core javaPresentation on Core java
Presentation on Core java
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Java Serialization
Java SerializationJava Serialization
Java Serialization
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 

Destacado

Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8AppDynamics
 
Java memory presentation
Java memory presentationJava memory presentation
Java memory presentationYury Bubnov
 
Java性能调优浅谈
Java性能调优浅谈Java性能调优浅谈
Java性能调优浅谈jxqlovejava
 
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAn Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAbhishek Asthana
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage CollectionAzul Systems Inc.
 
3978 Why is Java so different... A Session for Cobol/PLI/Assembler Developers
3978   Why is Java so different... A Session for Cobol/PLI/Assembler Developers3978   Why is Java so different... A Session for Cobol/PLI/Assembler Developers
3978 Why is Java so different... A Session for Cobol/PLI/Assembler Developersnick_garrod
 

Destacado (12)

The Java Memory Model
The Java Memory ModelThe Java Memory Model
The Java Memory Model
 
Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8
 
Java memory presentation
Java memory presentationJava memory presentation
Java memory presentation
 
Java memory model
Java memory modelJava memory model
Java memory model
 
OAuth 2.0协议
OAuth 2.0协议OAuth 2.0协议
OAuth 2.0协议
 
Java性能调优浅谈
Java性能调优浅谈Java性能调优浅谈
Java性能调优浅谈
 
Java GC, Off-heap workshop
Java GC, Off-heap workshopJava GC, Off-heap workshop
Java GC, Off-heap workshop
 
Tools for Metaspace
Tools for MetaspaceTools for Metaspace
Tools for Metaspace
 
An Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in JavaAn Introduction to JVM Internals and Garbage Collection in Java
An Introduction to JVM Internals and Garbage Collection in Java
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage Collection
 
The Java memory model made easy
The Java memory model made easyThe Java memory model made easy
The Java memory model made easy
 
3978 Why is Java so different... A Session for Cobol/PLI/Assembler Developers
3978   Why is Java so different... A Session for Cobol/PLI/Assembler Developers3978   Why is Java so different... A Session for Cobol/PLI/Assembler Developers
3978 Why is Java so different... A Session for Cobol/PLI/Assembler Developers
 

Similar a java memory management & gc

Chicago-Java-User-Group-Meetup-Some-Garbage-Talk-2015-01-14
Chicago-Java-User-Group-Meetup-Some-Garbage-Talk-2015-01-14Chicago-Java-User-Group-Meetup-Some-Garbage-Talk-2015-01-14
Chicago-Java-User-Group-Meetup-Some-Garbage-Talk-2015-01-14Jayesh Thakrar
 
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english versionSun jdk 1.6 gc english version
Sun jdk 1.6 gc english versionbluedavy lin
 
JVM performance options. How it works
JVM performance options. How it worksJVM performance options. How it works
JVM performance options. How it worksDmitriy Dumanskiy
 
Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...
Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...
Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...Monica Beckwith
 
Am I reading GC logs Correctly?
Am I reading GC logs Correctly?Am I reading GC logs Correctly?
Am I reading GC logs Correctly?Tier1 App
 
Performance tuning jvm
Performance tuning jvmPerformance tuning jvm
Performance tuning jvmPrem Kuppumani
 
Become a Garbage Collection Hero
Become a Garbage Collection HeroBecome a Garbage Collection Hero
Become a Garbage Collection HeroTier1app
 
Pick diamonds from garbage
Pick diamonds from garbagePick diamonds from garbage
Pick diamonds from garbageTier1 App
 
Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialJin-Hwa Kim
 
JVM Garbage Collection Tuning
JVM Garbage Collection TuningJVM Garbage Collection Tuning
JVM Garbage Collection Tuningihji
 
The Technology behind Shadow Warrior, ZTG 2014
The Technology behind Shadow Warrior, ZTG 2014The Technology behind Shadow Warrior, ZTG 2014
The Technology behind Shadow Warrior, ZTG 2014Jarosław Pleskot
 
What you need to know about GC
What you need to know about GCWhat you need to know about GC
What you need to know about GCKelum Senanayake
 
Java profiling Do It Yourself
Java profiling Do It YourselfJava profiling Do It Yourself
Java profiling Do It Yourselfaragozin
 
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...Amazon Web Services
 
CICS Memory Objects and MEMLIMIT
 CICS Memory Objects and MEMLIMIT CICS Memory Objects and MEMLIMIT
CICS Memory Objects and MEMLIMITDavid Clancy
 
JVM memory management & Diagnostics
JVM memory management & DiagnosticsJVM memory management & Diagnostics
JVM memory management & DiagnosticsDhaval Shah
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010RonnBlack
 

Similar a java memory management & gc (20)

Chicago-Java-User-Group-Meetup-Some-Garbage-Talk-2015-01-14
Chicago-Java-User-Group-Meetup-Some-Garbage-Talk-2015-01-14Chicago-Java-User-Group-Meetup-Some-Garbage-Talk-2015-01-14
Chicago-Java-User-Group-Meetup-Some-Garbage-Talk-2015-01-14
 
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english versionSun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
 
[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe
 
JVM performance options. How it works
JVM performance options. How it worksJVM performance options. How it works
JVM performance options. How it works
 
Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...
Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...
Garbage First Garbage Collector (G1 GC): Current and Future Adaptability and ...
 
Am I reading GC logs Correctly?
Am I reading GC logs Correctly?Am I reading GC logs Correctly?
Am I reading GC logs Correctly?
 
Performance tuning jvm
Performance tuning jvmPerformance tuning jvm
Performance tuning jvm
 
Become a Garbage Collection Hero
Become a Garbage Collection HeroBecome a Garbage Collection Hero
Become a Garbage Collection Hero
 
Pick diamonds from garbage
Pick diamonds from garbagePick diamonds from garbage
Pick diamonds from garbage
 
Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorial
 
JVM Garbage Collection Tuning
JVM Garbage Collection TuningJVM Garbage Collection Tuning
JVM Garbage Collection Tuning
 
alexnet.pdf
alexnet.pdfalexnet.pdf
alexnet.pdf
 
The Technology behind Shadow Warrior, ZTG 2014
The Technology behind Shadow Warrior, ZTG 2014The Technology behind Shadow Warrior, ZTG 2014
The Technology behind Shadow Warrior, ZTG 2014
 
What you need to know about GC
What you need to know about GCWhat you need to know about GC
What you need to know about GC
 
Java profiling Do It Yourself
Java profiling Do It YourselfJava profiling Do It Yourself
Java profiling Do It Yourself
 
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
SRV402 Deep Dive on Amazon EC2 Instances, Featuring Performance Optimization ...
 
CICS Memory Objects and MEMLIMIT
 CICS Memory Objects and MEMLIMIT CICS Memory Objects and MEMLIMIT
CICS Memory Objects and MEMLIMIT
 
Deep Dive on Amazon EC2
Deep Dive on Amazon EC2Deep Dive on Amazon EC2
Deep Dive on Amazon EC2
 
JVM memory management & Diagnostics
JVM memory management & DiagnosticsJVM memory management & Diagnostics
JVM memory management & Diagnostics
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010
 

Más de exsuns

Hadoop 20111215
Hadoop 20111215Hadoop 20111215
Hadoop 20111215exsuns
 
Statistics
StatisticsStatistics
Statisticsexsuns
 
Cassandra
CassandraCassandra
Cassandraexsuns
 
Hadoop 20111117
Hadoop 20111117Hadoop 20111117
Hadoop 20111117exsuns
 

Más de exsuns (6)

Hadoop 20111215
Hadoop 20111215Hadoop 20111215
Hadoop 20111215
 
Statistics
StatisticsStatistics
Statistics
 
R
RR
R
 
Ios
IosIos
Ios
 
Cassandra
CassandraCassandra
Cassandra
 
Hadoop 20111117
Hadoop 20111117Hadoop 20111117
Hadoop 20111117
 

Último

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 

Último (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 

java memory management & gc

  • 1. Tech share Java Memory Management
  • 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
  • 7. Hotspot Collectors Serial Collector Parallel Collector Parallel Compacting Collector Concurrent Mark-Sweep (CMS) Collector
  • 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
  • 9. Serial Collector using a single CPU in a stop-the-world fashion
  • 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)
  • 23. CMS log 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] 2.260: [CMS-concurrent-mark-start] 2.267: [CMS-concurrent-mark: 0.007/0.007 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 2.267: [CMS-concurrent-preclean-start] 2.267: [CMS-concurrent-preclean: 0.001/0.001 secs] [Times: user=0.01 sys=0.00, real=0.02 secs] 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] 2.268: [CMS-concurrent-sweep-start] 2.269: [CMS-concurrent-sweep: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 2.269: [CMS-concurrent-reset-start] 2.269: [CMS-concurrent-reset: 0.001/0.001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
  • 24. Disadvantage of non-compactingCode static int alloc_1MB = 1024 * 1024 * 1; public static void main(String[] args) throws Exception { //UseConcMarkSweepGC byte[] bytes10 = alloc(); alloc(); byte[] bytes12 = alloc(); alloc(); byte[] bytes14 = alloc(); alloc(); byte[] bytes16 = alloc(); alloc(); byte[] bytes18 = alloc(); alloc(); byte[] bytes20 = alloc(); alloc(); byte[] bytes22 = alloc(); alloc(3); } static int count = 0; private static byte[] alloc() { return alloc(1); } private static byte[] alloc(inti) { count = count + 1 * i ; System.out.println(count + "M"); return new byte[alloc_1MB * i]; }
  • 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
  • 27. VM options -XX:MaxGCPauseMillis=n Pause time -XX:GCTimeRatio=99 1 / (1 + n) Throughput -Xmx –Xms -Xmn (=eden+survivor*2) –XX:SurvivorRatio=32 (1:34) Survivor:Eden -XX:MaxPermSize
  • 28. Print gc –XX:+PrintGC –XX:+PrintGCDetails –XX:+PrintGCTimeStamps -verbose:gc [GC 325407K->83000K(776768K), 0.2300771 secs] [GC 325816K->83372K(776768K), 0.2454258 secs] [Full GC 267628K->83769K(776768K), 1.8479984 secs]
  • 29.
  • 30. System.gc() & finalize() Code: public class SerialTest { public static void main(String[] args) throws Exception { new SerialTest(); System.gc(); Thread.sleep(10); System.out.println("123"); } @Override protected void finalize() throws Throwable { System.out.println("heloo================finalize"); } } Result: 0.227: [Full GC (System) TLAB: gc thread: 0x08839400 [id: 5820] ……….. heloo================finalize 123
  • 31. finalize twice GC !!!
  • 32. monitor JVisualVM JConsole JRockit mission control
  • 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.
  • 36. G1 Collector RS: regon set 0.634: [GC pause (young), 0.00846287 secs] [Parallel Time: 8.3 ms] [GC Worker Start Time (ms): 633.9 634.3] [Update RS (ms): 0.0 0.0 Avg: 0.0, Min: 0.0, Max: 0.0] [Processed Buffers : 0 5 Sum: 5, Avg: 2, Min: 0, Max: 5] [Ext Root Scanning (ms): 3.6 3.3 Avg: 3.5, Min: 3.3, Max: 3.6] [Mark Stack Scanning (ms): 0.0 0.0 Avg: 0.0, Min: 0.0, Max: 0.0] [Scan RS (ms): 0.0 0.0 Avg: 0.0, Min: 0.0, Max: 0.0] [Object Copy (ms): 3.8 3.6 Avg: 3.7, Min: 3.6, Max: 3.8] [Termination (ms): 0.0 0.0 Avg: 0.0, Min: 0.0, Max: 0.0] [Termination Attempts : 1 1 Sum: 2, Avg: 1, Min: 1, Max: 1] [GC Worker End Time (ms): 641.3 641.3] [Other: 1.1 ms] [Clear CT: 0.0 ms] [Other: 0.2 ms] [Choose CSet: 0.0 ms] [ 2868K->1763K(20M)] [Times: user=0.00 sys=0.00, real=0.00 secs]
  • 38. references http://java.sun.com/products/hotspot/whitepaper.html http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html http://blogs.oracle.com/watt/resource/jvm-options-list.html http://www.iteye.com/topic/802638 http://blog.csdn.net/calvinxiu/archive/2007/05/18/1614473.aspx http://unixboy.iteye.com/blog/174173 http://java.sun.com/performance/reference/whitepapers/tuning.html