SlideShare a Scribd company logo
Tech shareJava Memory Management
Garbage Collection Responsibility Allocating memoryEnsuring that any referenced objects remain in memoryRecovering memory used by objects that are no longer reachable from references in executing code.
Generation CollectionMost allocated objects die youngFew references from older to younger objects exist.
Sun hotspot memory modelYoung GenerationEdenSurvivor FromSurvivor ToTenured (Old) GenerationPermanentobjects describing classes and methodsas well as the classes and methods themselves
Garbage Collection Typesyoung generation collection (minor collection)young generation fills upfull collection (major collection) old or permanent generation fills upSystem.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 Allocationbump-the-pointer technique large contiguous blocks of memory availableThread-Local Allocation Buffers (TLABs)multithread-safewithout global locks-XX:TLABWasteTargetPercent   (n/Eden) -XX:+PrintTLAB
Hotspot CollectorsSerial CollectorParallel CollectorParallel Compacting CollectorConcurrent Mark-Sweep (CMS) Collector
Hotspot Default garbage collector Java –versionjava 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 collectorNote: 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 Collectorusing a single CPUin a stop-the-world fashion
Serial CollectorYong Generation Collectiontoo large objects are directly copied to old generation-XX:InitialTenuringThreshold=7-XX:MaxTenuringThreshold=
Serial Collector Old Generation Collectionmark-sweep-compactMarkMark live objectsSweepSweep unmarked objectsCompactFor bump-the-pointer
Serial CollectorWhen to Use:do not have a requirement for low pause timesOn today’s hardware, less than half a second for full collections (64MB heaps)1024MB/64MB=16*0.5second = 8secondUsage:-XX:+UseSerialGC
Parallel Collectoralso known as the throughput collector-XX:+PrintGCDetails -XX:+PrintTLAB -XX:MaxTenuringThreshold=7 -XX:PretenureSizeThreshold=2M -XX:+PrintGCTimeStamps -Xms30M -Xmx30M -Xmn2M   -XX:+UseParallelGCPSYoungGen [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 CollectorYoung Generation Collectionstill a stop-the-world and copying collectorin parallelusing many CPUs
Parallel CollectorOld Generation CollectionStill mark-sweep-compact serial operationWhen to use:often appropriate include those that do batch processing, billing, payroll, scientific computing, and so on.Usage:-XX:+UseParallelGC
Parallel Compacting Collectorwas introduced in J2SE 5.0 update 6Note: willreplace the parallel collector.Young Generation Collection Same as parallel collector
Parallel Compacting CollectorOld Generation Collection marking phaselogically divided into fixed-sized regionsGCmarklive 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 regionsCalculates and stores the new location of the first byte of live data for each compacted regioncompaction phaseCompact use by summary data
Parallel Compacting CollectorWhen to use:more than one CPUreduces pause times  (multi-thread)Usage:-XX:+UseParallelOldGC-XX:ParallelGCThreads=n
Concurrent Mark-Sweep (CMS) Collectoralso known as the low-latency collector.Young Generation Collection Same as parallel collector
Concurrent Mark-Sweep (CMS) CollectorOld Generation Collection:identifies the initial set of live objects directly reachable from the application codemarks all live objects that are transitively reachable from this set
Concurrent Mark-Sweep (CMS)disadvantageonly collector that is non-compactingrequirement for larger heap sizes than the other collectorsCMS Incremental Modeperiodically stopping the concurrent phase toyield 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 processorsfor any application with a low pause time requirementUsage: -XX:+UseConcMarkSweepGC–XX:+CMSIncrementalMode (Incremental Mode)
CMS log2.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-compactingCodestatic 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:-PrintTenuringDistributionPSYoungGen      total 8960K, used 5336Keden space 7680K, 69% used  from space 1280K, 0% used   to   space 1280K, 0% used PSOldGen        total 10240K, used 6598K   object space 10240K, 64% usedPSPermGen       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:-PrintTenuringDistributionException 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=nPause time-XX:GCTimeRatio=991 / (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)Error example-XX:MaxTenuringThreshold=0 –Xms20M –Xmx20M -Xmn18M  -XX:+UseSerialGCException in thread "main" java.lang.OutOfMemoryError: Java heap spaceNotice: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================finalize123
finalize twice GC !!!
monitorJVisualVMJConsoleJRockit mission control
Garbage-First Garbage CollectorG1 GC for shortIs a new GC that is being introduced in the Java HotSpot VM in JDK 7also 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 CollectorParallelism and Concurrency.G1 performs heap compactionthere is a single contiguous heap which is split into same-sized regionsYoung/old generation is a set of potentially non-contiguous regions
G1 CollectorHeap 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% usedNo shared spaces configured.
G1 CollectorRS: regon set0.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.0Avg:   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.3Avg:   3.5, Min:   3.3, Max:   3.6]      [Mark Stack Scanning (ms):  0.0  0.0Avg:   0.0, Min:   0.0, Max:   0.0]      [Scan RS (ms):  0.0  0.0Avg:   0.0, Min:   0.0, Max:   0.0]      [Object Copy (ms):  3.8  3.6Avg:   3.7, Min:   3.6, Max:   3.8]      [Termination (ms):  0.0  0.0Avg:   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
referenceshttp://java.sun.com/products/hotspot/whitepaper.htmlhttp://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.htmlhttp://blogs.oracle.com/watt/resource/jvm-options-list.htmlhttp://www.iteye.com/topic/802638http://blog.csdn.net/calvinxiu/archive/2007/05/18/1614473.aspxhttp://unixboy.iteye.com/blog/174173http://java.sun.com/performance/reference/whitepapers/tuning.html

More Related Content

What's hot (20)

java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
Tanmoy Barman
 
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modules
monikadeshmane
 
Expressjs
ExpressjsExpressjs
Expressjs
Yauheni Nikanovich
 
C# basics
 C# basics C# basics
C# basics
Dinesh kumar
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Vikash Singh
 
All you need to know about the JavaScript event loop
All you need to know about the JavaScript event loopAll you need to know about the JavaScript event loop
All you need to know about the JavaScript event loop
Saša Tatar
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
Adieu
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
slire
 
JDBC
JDBCJDBC
JDBC
People Strategists
 
javascript objects
javascript objectsjavascript objects
javascript objects
Vijay Kalyan
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
Saai Vignesh P
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
Designveloper
 
Introduction Node.js
Introduction Node.jsIntroduction Node.js
Introduction Node.js
Erik van Appeldoorn
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
L&T Technology Services Limited
 
Servlets
ServletsServlets
Servlets
ZainabNoorGul
 
Java swing
Java swingJava swing
Java swing
Apurbo Datta
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
Manisha Keim
 

Viewers also liked (12)

The Java Memory Model
The Java Memory ModelThe Java Memory Model
The Java Memory Model
CA Technologies
 
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
AppDynamics
 
Java memory presentation
Java memory presentationJava memory presentation
Java memory presentation
Yury Bubnov
 
Java memory model
Java memory modelJava memory model
Java memory model
Michał Warecki
 
OAuth 2.0协议
OAuth 2.0协议OAuth 2.0协议
OAuth 2.0协议
jxqlovejava
 
Java性能调优浅谈
Java性能调优浅谈Java性能调优浅谈
Java性能调优浅谈
jxqlovejava
 
Java GC, Off-heap workshop
Java GC, Off-heap workshopJava GC, Off-heap workshop
Java GC, Off-heap workshop
Valerii Moisieienko
 
Tools for Metaspace
Tools for MetaspaceTools for Metaspace
Tools for Metaspace
Takahiro YAMADA
 
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
Abhishek Asthana
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage Collection
Azul Systems Inc.
 
The Java memory model made easy
The Java memory model made easyThe Java memory model made easy
The Java memory model made easy
Rafael Winterhalter
 
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
nick_garrod
 
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
AppDynamics
 
Java memory presentation
Java memory presentationJava memory presentation
Java memory presentation
Yury 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 Java
Abhishek Asthana
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage Collection
Azul 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 Developers
nick_garrod
 
Ad

Similar to java memory management & gc (20)

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
bluedavy lin
 
[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe
SAP HANA Cloud Platform
 
JVM memory management & Diagnostics
JVM memory management & DiagnosticsJVM memory management & Diagnostics
JVM memory management & Diagnostics
Dhaval Shah
 
Basics of JVM Tuning
Basics of JVM TuningBasics of JVM Tuning
Basics of JVM Tuning
Vladislav Gangan
 
JVM Garbage Collection Tuning
JVM Garbage Collection TuningJVM Garbage Collection Tuning
JVM Garbage Collection Tuning
ihji
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
Haim Yadid
 
Performance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage CollectionPerformance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage Collection
Haribabu Nandyal Padmanaban
 
[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?
Alonso Torres
 
Gclogs j1
Gclogs j1Gclogs j1
Gclogs j1
Kirk Pepperdine
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)
Prashanth Kumar
 
Performance tuning jvm
Performance tuning jvmPerformance tuning jvm
Performance tuning jvm
Prem Kuppumani
 
Java memory presentation IBM 7
Java memory presentation IBM 7Java memory presentation IBM 7
Java memory presentation IBM 7
Yury Bubnov
 
Taming GC Pauses for Humongous Java Heaps in Spark Graph Computing-(Eric Kacz...
Taming GC Pauses for Humongous Java Heaps in Spark Graph Computing-(Eric Kacz...Taming GC Pauses for Humongous Java Heaps in Spark Graph Computing-(Eric Kacz...
Taming GC Pauses for Humongous Java Heaps in Spark Graph Computing-(Eric Kacz...
Spark Summit
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
Mudit Gupta
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
jaganmohanreddyk
 
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
Kelum Senanayake
 
«Большие объёмы данных и сборка мусора в Java
«Большие объёмы данных и сборка мусора в Java«Большие объёмы данных и сборка мусора в Java
«Большие объёмы данных и сборка мусора в Java
Olga Lavrentieva
 
JVM Magic
JVM MagicJVM Magic
JVM Magic
Baruch Sadogursky
 
JVM and Garbage Collection Tuning
JVM and Garbage Collection TuningJVM and Garbage Collection Tuning
JVM and Garbage Collection Tuning
Kai Koenig
 
Garbage collection in JVM
Garbage collection in JVMGarbage collection in JVM
Garbage collection in JVM
aragozin
 
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
bluedavy lin
 
JVM memory management & Diagnostics
JVM memory management & DiagnosticsJVM memory management & Diagnostics
JVM memory management & Diagnostics
Dhaval Shah
 
JVM Garbage Collection Tuning
JVM Garbage Collection TuningJVM Garbage Collection Tuning
JVM Garbage Collection Tuning
ihji
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
Haim Yadid
 
Performance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage CollectionPerformance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage Collection
Haribabu Nandyal Padmanaban
 
[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?[Jbcn 2016] Garbage Collectors WTF!?
[Jbcn 2016] Garbage Collectors WTF!?
Alonso Torres
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)
Prashanth Kumar
 
Performance tuning jvm
Performance tuning jvmPerformance tuning jvm
Performance tuning jvm
Prem Kuppumani
 
Java memory presentation IBM 7
Java memory presentation IBM 7Java memory presentation IBM 7
Java memory presentation IBM 7
Yury Bubnov
 
Taming GC Pauses for Humongous Java Heaps in Spark Graph Computing-(Eric Kacz...
Taming GC Pauses for Humongous Java Heaps in Spark Graph Computing-(Eric Kacz...Taming GC Pauses for Humongous Java Heaps in Spark Graph Computing-(Eric Kacz...
Taming GC Pauses for Humongous Java Heaps in Spark Graph Computing-(Eric Kacz...
Spark Summit
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
Mudit Gupta
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
jaganmohanreddyk
 
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
Kelum Senanayake
 
«Большие объёмы данных и сборка мусора в Java
«Большие объёмы данных и сборка мусора в Java«Большие объёмы данных и сборка мусора в Java
«Большие объёмы данных и сборка мусора в Java
Olga Lavrentieva
 
JVM and Garbage Collection Tuning
JVM and Garbage Collection TuningJVM and Garbage Collection Tuning
JVM and Garbage Collection Tuning
Kai Koenig
 
Garbage collection in JVM
Garbage collection in JVMGarbage collection in JVM
Garbage collection in JVM
aragozin
 
Ad

More from exsuns (6)

Hadoop 20111215
Hadoop 20111215Hadoop 20111215
Hadoop 20111215
exsuns
 
Statistics
StatisticsStatistics
Statistics
exsuns
 
R
RR
R
exsuns
 
Cassandra
CassandraCassandra
Cassandra
exsuns
 
Hadoop 20111117
Hadoop 20111117Hadoop 20111117
Hadoop 20111117
exsuns
 
Hadoop 20111215
Hadoop 20111215Hadoop 20111215
Hadoop 20111215
exsuns
 
Statistics
StatisticsStatistics
Statistics
exsuns
 
Cassandra
CassandraCassandra
Cassandra
exsuns
 
Hadoop 20111117
Hadoop 20111117Hadoop 20111117
Hadoop 20111117
exsuns
 

Recently uploaded (20)

Let’s Get Slack Certified! 🚀- Slack Community
Let’s Get Slack Certified! 🚀- Slack CommunityLet’s Get Slack Certified! 🚀- Slack Community
Let’s Get Slack Certified! 🚀- Slack Community
SanjeetMishra29
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
 
Supercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMsSupercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMs
Francesco Corti
 
SDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhereSDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhere
Adtran
 
Cyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptxCyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptx
Ghimire B.R.
 
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure ModesCognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Dr. Tathagat Varma
 
Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Co-Constructing Explanations for AI Systems using Provenance
Co-Constructing Explanations for AI Systems using ProvenanceCo-Constructing Explanations for AI Systems using Provenance
Co-Constructing Explanations for AI Systems using Provenance
Paul Groth
 
Offshore IT Support: Balancing In-House and Offshore Help Desk Technicians
Offshore IT Support: Balancing In-House and Offshore Help Desk TechniciansOffshore IT Support: Balancing In-House and Offshore Help Desk Technicians
Offshore IT Support: Balancing In-House and Offshore Help Desk Technicians
john823664
 
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Aaryan Kansari
 
Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptxECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
Jasper Oosterveld
 
Introducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and ARIntroducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and AR
Safe Software
 
Measuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI SuccessMeasuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI Success
Nikki Chapple
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
Let’s Get Slack Certified! 🚀- Slack Community
Let’s Get Slack Certified! 🚀- Slack CommunityLet’s Get Slack Certified! 🚀- Slack Community
Let’s Get Slack Certified! 🚀- Slack Community
SanjeetMishra29
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
 
Supercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMsSupercharge Your AI Development with Local LLMs
Supercharge Your AI Development with Local LLMs
Francesco Corti
 
SDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhereSDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhere
Adtran
 
Cyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptxCyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptx
Ghimire B.R.
 
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure ModesCognitive Chasms - A Typology of GenAI Failure Failure Modes
Cognitive Chasms - A Typology of GenAI Failure Failure Modes
Dr. Tathagat Varma
 
Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Co-Constructing Explanations for AI Systems using Provenance
Co-Constructing Explanations for AI Systems using ProvenanceCo-Constructing Explanations for AI Systems using Provenance
Co-Constructing Explanations for AI Systems using Provenance
Paul Groth
 
Offshore IT Support: Balancing In-House and Offshore Help Desk Technicians
Offshore IT Support: Balancing In-House and Offshore Help Desk TechniciansOffshore IT Support: Balancing In-House and Offshore Help Desk Technicians
Offshore IT Support: Balancing In-House and Offshore Help Desk Technicians
john823664
 
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Agentic AI Explained: The Next Frontier of Autonomous Intelligence & Generati...
Aaryan Kansari
 
Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptxECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
ECS25 - The adventures of a Microsoft 365 Platform Owner - Website.pptx
Jasper Oosterveld
 
Introducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and ARIntroducing FME Realize: A New Era of Spatial Computing and AR
Introducing FME Realize: A New Era of Spatial Computing and AR
Safe Software
 
Measuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI SuccessMeasuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI Success
Nikki Chapple
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 

java memory management & gc

  • 2. Garbage Collection Responsibility Allocating memoryEnsuring that any referenced objects remain in memoryRecovering memory used by objects that are no longer reachable from references in executing code.
  • 3. Generation CollectionMost allocated objects die youngFew references from older to younger objects exist.
  • 4. Sun hotspot memory modelYoung GenerationEdenSurvivor FromSurvivor ToTenured (Old) GenerationPermanentobjects describing classes and methodsas well as the classes and methods themselves
  • 5. Garbage Collection Typesyoung generation collection (minor collection)young generation fills upfull collection (major collection) old or permanent generation fills upSystem.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 Allocationbump-the-pointer technique large contiguous blocks of memory availableThread-Local Allocation Buffers (TLABs)multithread-safewithout global locks-XX:TLABWasteTargetPercent (n/Eden) -XX:+PrintTLAB
  • 7. Hotspot CollectorsSerial CollectorParallel CollectorParallel Compacting CollectorConcurrent Mark-Sweep (CMS) Collector
  • 8. Hotspot Default garbage collector Java –versionjava 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 collectorNote: 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 Collectorusing a single CPUin a stop-the-world fashion
  • 10. Serial CollectorYong Generation Collectiontoo large objects are directly copied to old generation-XX:InitialTenuringThreshold=7-XX:MaxTenuringThreshold=
  • 11. Serial Collector Old Generation Collectionmark-sweep-compactMarkMark live objectsSweepSweep unmarked objectsCompactFor bump-the-pointer
  • 12. Serial CollectorWhen to Use:do not have a requirement for low pause timesOn today’s hardware, less than half a second for full collections (64MB heaps)1024MB/64MB=16*0.5second = 8secondUsage:-XX:+UseSerialGC
  • 13. Parallel Collectoralso known as the throughput collector-XX:+PrintGCDetails -XX:+PrintTLAB -XX:MaxTenuringThreshold=7 -XX:PretenureSizeThreshold=2M -XX:+PrintGCTimeStamps -Xms30M -Xmx30M -Xmn2M -XX:+UseParallelGCPSYoungGen [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 CollectorYoung Generation Collectionstill a stop-the-world and copying collectorin parallelusing many CPUs
  • 15. Parallel CollectorOld Generation CollectionStill mark-sweep-compact serial operationWhen to use:often appropriate include those that do batch processing, billing, payroll, scientific computing, and so on.Usage:-XX:+UseParallelGC
  • 16. Parallel Compacting Collectorwas introduced in J2SE 5.0 update 6Note: willreplace the parallel collector.Young Generation Collection Same as parallel collector
  • 17. Parallel Compacting CollectorOld Generation Collection marking phaselogically divided into fixed-sized regionsGCmarklive 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 regionsCalculates and stores the new location of the first byte of live data for each compacted regioncompaction phaseCompact use by summary data
  • 18. Parallel Compacting CollectorWhen to use:more than one CPUreduces pause times (multi-thread)Usage:-XX:+UseParallelOldGC-XX:ParallelGCThreads=n
  • 19. Concurrent Mark-Sweep (CMS) Collectoralso known as the low-latency collector.Young Generation Collection Same as parallel collector
  • 20. Concurrent Mark-Sweep (CMS) CollectorOld Generation Collection:identifies the initial set of live objects directly reachable from the application codemarks all live objects that are transitively reachable from this set
  • 21. Concurrent Mark-Sweep (CMS)disadvantageonly collector that is non-compactingrequirement for larger heap sizes than the other collectorsCMS Incremental Modeperiodically stopping the concurrent phase toyield 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 processorsfor any application with a low pause time requirementUsage: -XX:+UseConcMarkSweepGC–XX:+CMSIncrementalMode (Incremental Mode)
  • 23. CMS log2.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-compactingCodestatic 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:-PrintTenuringDistributionPSYoungGen total 8960K, used 5336Keden space 7680K, 69% used from space 1280K, 0% used to space 1280K, 0% used PSOldGen total 10240K, used 6598K object space 10240K, 64% usedPSPermGen 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:-PrintTenuringDistributionException 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=nPause time-XX:GCTimeRatio=991 / (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. 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)Error example-XX:MaxTenuringThreshold=0 –Xms20M –Xmx20M -Xmn18M -XX:+UseSerialGCException in thread "main" java.lang.OutOfMemoryError: Java heap spaceNotice:for Serial collector & CMS
  • 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================finalize123
  • 33. Garbage-First Garbage CollectorG1 GC for shortIs a new GC that is being introduced in the Java HotSpot VM in JDK 7also 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 CollectorParallelism and Concurrency.G1 performs heap compactionthere is a single contiguous heap which is split into same-sized regionsYoung/old generation is a set of potentially non-contiguous regions
  • 35. G1 CollectorHeap 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% usedNo shared spaces configured.
  • 36. G1 CollectorRS: regon set0.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.0Avg: 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.3Avg: 3.5, Min: 3.3, Max: 3.6] [Mark Stack Scanning (ms): 0.0 0.0Avg: 0.0, Min: 0.0, Max: 0.0] [Scan RS (ms): 0.0 0.0Avg: 0.0, Min: 0.0, Max: 0.0] [Object Copy (ms): 3.8 3.6Avg: 3.7, Min: 3.6, Max: 3.8] [Termination (ms): 0.0 0.0Avg: 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]