SlideShare a Scribd company logo
1 of 20
GARBAGE COLLECTOR
Automatic garbage collection is the process of looking at heap memory,
identifying which objects are in use and which are not, and deleting the unused
objects. An in use object, or a referenced object, means that some part of your
program still maintains a pointer to that object. An unused object, or
unreferenced object, is no longer referenced by any part of your program. So the
memory used by an unreferenced object can be reclaimed.
In a programming language like C, allocating and deallocating memory is a
manual process. In Java, process of deallocating memory is handled
automatically by the garbage collector.
The basic process can be described as follows:
● Step 1: Marking
The first step in the process is called marking. This is where the garbage
collector identifies which pieces of memory are in use and which are not.
Referenced objects are shown in blue. Unreferenced objects are shown in
gold. All objects are scanned in the marking phase to make this
determination. This can be a very time consuming process if all objects in a
system must be scanned.
Step 2: Normal Deletion
Normal deletion removes unreferenced objects leaving
referenced objects and pointers to free space.
The memory allocator holds references to blocks of free
space where new object can be allocated.
Step 2a: Deletion with Compacting
To further improve performance, in addition to deleting
unreferenced objects, you can also compact the remaining
referenced objects. By moving referenced object together,
this makes new memory allocation much easier and faster.
Why Generational Garbage Collection?
● As stated earlier, having to mark and compact all the objects in a JVM is inefficient. As more and more
objects are allocated, the list of objects grows and grows leading to longer and longer garbage collection
time. However, empirical analysis of applications has shown that most objects are short lived.
JVM Generations
● The information learned from the object allocation behavior can be used to enhance the performance of
the JVM. Therefore, the heap is broken up into smaller parts or generations. The heap parts are:
Young Generation,
Old or Tenured Generation
Permanent Generation
● The Young Generation is where all new objects are allocated and aged. When the young generation fills
up, this causes a minor garbage collection. Minor collections can be optimized assuming a high object
mortality rate. A young generation full of dead objects is collected very quickly. Some surviving objects are
aged and eventually move to the old generation.
● Stop the World Event - All minor garbage collections are "Stop the World" events. This
means that all application threads are stopped until the operation completes. Minor
garbage collections are always Stop the World events.
● The Old Generation is used to store long surviving objects. Typically, a threshold is
set for young generation object and when that age is met, the object gets moved to the
old generation. Eventually the old generation needs to be collected. This event is called a
major garbage collection.
● Major garbage collection are also Stop the World events. Often a major collection is
much slower because it involves all live objects. So for Responsive applications,
major garbage collections should be minimized. Also note, that the length of the Stop the
World event for a major garbage collection is affected by the kind of garbage collector
that is used for the old generation space.
● The Permanent generation contains metadata required by the JVM to describe the
classes and methods used in the application. The permanent generation is
populated by the JVM at runtime based on classes in use by the application. In
addition, Java SE library classes and methods may be stored here.
● Classes may get collected (unloaded) if the JVM finds they are no longer needed and
space may be needed for other classes. The permanent generation is included in a full
garbage collection.
The pictures that follow walks through the object
allocation and aging process in the JVM:
First, any new objects are allocated to the eden space.
Both survivor spaces start out empty.
When the eden space fills up, a minor garbage collection
is triggered.
● Referenced objects are moved to the first survivor space.
Unreferenced objects are deleted when the eden space is
cleared.
● At the next minor GC, the same thing happens for the eden space.
Unreferenced objects are deleted and referenced objects are moved to a
survivor space. However, in this case, they are moved to the second survivor
space (S1). In addition, objects from the last minor GC on the first survivor
space (S0) have their age incremented and get moved to S1. Once all
surviving objects have been moved to S1, both S0 and eden are cleared.
Notice we now have differently aged object in the survivor space.
● At the next minor GC, the same process repeats. However this
time the survivor spaces switch. Referenced objects are moved
to S0. Surviving objects are aged. Eden and S1 are cleared.
● This slide demonstrates promotion. After a minor GC, when
aged objects reach a certain age threshold (8 in this example)
they are promoted from young generation to old generation.
● As minor GCs continue to occure objects will continue to
be promoted to the old generation space.
● So that pretty much covers the entire process with the
young generation. Eventually, a major GC will be
performed on the old generation which cleans up and
compacts that space.
Common Heap Related Switches
There are many different command line switches that can be used with Java.
Switch Description
-Xms Sets the initial heap size for when the JVM starts.
-Xmx Sets the maximum heap size.
-Xmn Sets the size of the Young Generation.
-XX:PermSize Sets the starting size of the Permanent Generation.
-XX:MaxPermSize Sets the maximum size of the Permanent Generation
The Serial GC
The serial collector is the default for client style machines in Java SE 5 and 6.
With the serial collector, both minor and major garbage collections are done
serially (using a single virtual CPU). In addition, it uses a mark-compact
collection method. This method moves older memory to the beginning of the
heap so that new memory allocations are made into a single continuous chunk
of memory at the end of the heap. This compacting of memory makes it faster to
allocate new chunks of memory to the heap.
The Parallel GC
The parallel garbage collector uses multiple threads to perform the
young genertion garbage collection. By default on a host with N CPUs,
the parallel garbage collector uses N garbage collector threads in the
collection. The number of garbage collector threads can be controlled
with command-line options:
-XX:ParallelGCThreads=<desired number>
On a host with a single CPU the default garbage collector is used even
if the parallel garbage collector has been requested. On a host with two
CPUs the parallel garbage collector generally performs as well as the
default garbage collector and a reduction in the young generation
garbage collector pause times can be expected on hosts with more than
two CPUs. The Parallel GC comes in two flavors.
The Parallel collector is also called a throughput collector. Since it can
use multilple CPUs to speed up application throughput. This collector
should be used when a lot of work need to be done and long pauses
are acceptable. For example, batch processing like printing reports or
bills or performing a large number of database queries.
Parallel Old GC(-XX:+UseParallelOldGC)
● Parallel Old GC was supported since JDK 5 update.
Compared to the parallel GC, the only difference is the GC
algorithm for the old generation. It goes through three
steps: mark – summary – compaction. The summary step
identifies the surviving objects separately for the areas that
the GC have previously performed, and thus different from
the sweep step of the mark-sweep-compact algorithm. It
goes through a little more complicated steps.
The Concurrent Mark Sweep (CMS) Collector
(-XX:+UseConcMarkSweepGC)
The Concurrent Mark Sweep (CMS) collector (also
referred to as the concurrent low pause collector) collects
the tenured generation. It attempts to minimize the pauses
due to garbage collection by doing most of the garbage
collection work concurrently with the application threads.
Normally the concurrent low pause collector does not copy
or compact the live objects. A garbage collection is done
without moving the live objects. If fragmentation becomes
a problem, allocate a larger heap.
The G1 Garbage Collector
The Garbage First or G1 garbage collector is available in Java 7 and is designed to be the
long term replacement for the CMS collector. The G1 collector is a parallel, concurrent,
and incrementally compacting low-pause garbage collector that has quite a different
layout from the other garbage collectors described previously.
In G1 garbage collector, one object is allocated to each grid, and then a GC is executed.
Then, once one area is full, the objects are allocated to another area, and then a GC is
executed. The steps where the data moves from the three spaces of the young generation
to the old generation cannot be found in this GC type. This type was created to replace
the CMS GC, which has causes a lot of issues and complaints in the long term.
The biggest advantage of the G1 GC is its performance. It is faster than any other GC
types that we have discussed so far. But in JDK 6, this is called an early access and can
be used only for a test. It is officially included in JDK 7.

More Related Content

What's hot

What's hot (20)

Basic Garbage Collection Techniques
Basic  Garbage  Collection  TechniquesBasic  Garbage  Collection  Techniques
Basic Garbage Collection Techniques
 
Java input
Java inputJava input
Java input
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Deadlock ppt
Deadlock ppt Deadlock ppt
Deadlock ppt
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Java threads
Java threadsJava threads
Java threads
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects
 
Demand paging
Demand pagingDemand paging
Demand paging
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
 
Java-java virtual machine
Java-java virtual machineJava-java virtual machine
Java-java virtual machine
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languages
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Run time storage
Run time storageRun time storage
Run time storage
 

Viewers also liked

Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQueryachinth
 
Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Ashish Jha
 
Garbage Collection Pause Times - Angelika Langer
Garbage Collection Pause Times - Angelika LangerGarbage Collection Pause Times - Angelika Langer
Garbage Collection Pause Times - Angelika LangerJAXLondon_Conference
 
G1 Garbage Collector - Big Heaps and Low Pauses?
G1 Garbage Collector - Big Heaps and Low Pauses?G1 Garbage Collector - Big Heaps and Low Pauses?
G1 Garbage Collector - Big Heaps and Low Pauses?C2B2 Consulting
 
G1 collector and tuning and Cassandra
G1 collector and tuning and CassandraG1 collector and tuning and Cassandra
G1 collector and tuning and CassandraChris Lohfink
 
GC Tuning Confessions Of A Performance Engineer
GC Tuning Confessions Of A Performance EngineerGC Tuning Confessions Of A Performance Engineer
GC Tuning Confessions Of A Performance EngineerMonica Beckwith
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage CollectionAzul Systems Inc.
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage CollectionHaim Yadid
 
Understanding Garbage Collection
Understanding Garbage CollectionUnderstanding Garbage Collection
Understanding Garbage CollectionDoug Hawkins
 
Performance tuning jvm
Performance tuning jvmPerformance tuning jvm
Performance tuning jvmPrem Kuppumani
 
55 New Features in JDK 9
55 New Features in JDK 955 New Features in JDK 9
55 New Features in JDK 9Simon Ritter
 
G1 Garbage Collector: Details and Tuning
G1 Garbage Collector: Details and TuningG1 Garbage Collector: Details and Tuning
G1 Garbage Collector: Details and TuningSimone Bordet
 

Viewers also liked (14)

Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)
 
Garbage Collection Pause Times - Angelika Langer
Garbage Collection Pause Times - Angelika LangerGarbage Collection Pause Times - Angelika Langer
Garbage Collection Pause Times - Angelika Langer
 
G1 Garbage Collector - Big Heaps and Low Pauses?
G1 Garbage Collector - Big Heaps and Low Pauses?G1 Garbage Collector - Big Heaps and Low Pauses?
G1 Garbage Collector - Big Heaps and Low Pauses?
 
G1 collector and tuning and Cassandra
G1 collector and tuning and CassandraG1 collector and tuning and Cassandra
G1 collector and tuning and Cassandra
 
GC Tuning Confessions Of A Performance Engineer
GC Tuning Confessions Of A Performance EngineerGC Tuning Confessions Of A Performance Engineer
GC Tuning Confessions Of A Performance Engineer
 
Heap Management
Heap ManagementHeap Management
Heap Management
 
How long can you afford to Stop The World?
How long can you afford to Stop The World?How long can you afford to Stop The World?
How long can you afford to Stop The World?
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage Collection
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
 
Understanding Garbage Collection
Understanding Garbage CollectionUnderstanding Garbage Collection
Understanding Garbage Collection
 
Performance tuning jvm
Performance tuning jvmPerformance tuning jvm
Performance tuning jvm
 
55 New Features in JDK 9
55 New Features in JDK 955 New Features in JDK 9
55 New Features in JDK 9
 
G1 Garbage Collector: Details and Tuning
G1 Garbage Collector: Details and TuningG1 Garbage Collector: Details and Tuning
G1 Garbage Collector: Details and Tuning
 

Similar to Garbage collection

Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMjaganmohanreddyk
 
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
 
Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdfSudhanshiBakre1
 
JVM Performance Tuning
JVM Performance TuningJVM Performance Tuning
JVM Performance TuningJeremy Leisy
 
JVM memory metrics and rules for detecting possible OOM caused crash
JVM memory metrics and rules for detecting possible OOM caused crashJVM memory metrics and rules for detecting possible OOM caused crash
JVM memory metrics and rules for detecting possible OOM caused crashAtharva Bhingarkar
 
JVM memory metrics and rules for detecting likely OOM caused crash
JVM memory metrics and rules for detecting likely OOM caused crashJVM memory metrics and rules for detecting likely OOM caused crash
JVM memory metrics and rules for detecting likely OOM caused crashAjit Bhingarkar
 
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12sidg75
 
Performance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage CollectionPerformance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage CollectionHaribabu Nandyal Padmanaban
 
Choosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
Choosing Right Garbage Collector to Increase Efficiency of Java Memory UsageChoosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
Choosing Right Garbage Collector to Increase Efficiency of Java Memory UsageJelastic Multi-Cloud PaaS
 
A novel design of a parallel machine learnt
A novel design of a parallel machine learntA novel design of a parallel machine learnt
A novel design of a parallel machine learntcseij
 
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector A Novel Design of a Parallel Machine Learnt Generational Garbage Collector
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector cseij
 
An introduction to G1 collector for busy developers
An introduction to G1 collector for busy developersAn introduction to G1 collector for busy developers
An introduction to G1 collector for busy developersSanjoy Kumar Roy
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuningosa_ora
 
Elastic JVM for Scalable Java EE Applications Running in Containers #Jakart...
Elastic JVM  for Scalable Java EE Applications  Running in Containers #Jakart...Elastic JVM  for Scalable Java EE Applications  Running in Containers #Jakart...
Elastic JVM for Scalable Java EE Applications Running in Containers #Jakart...Jelastic Multi-Cloud PaaS
 
Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorWednesday Solutions
 
A quick view about Java Virtual Machine
A quick view about Java Virtual MachineA quick view about Java Virtual Machine
A quick view about Java Virtual MachineJoão Santana
 
Java at Scale, Dallas JUG, October 2013
Java at Scale, Dallas JUG, October 2013Java at Scale, Dallas JUG, October 2013
Java at Scale, Dallas JUG, October 2013Azul Systems Inc.
 
Garbage First and you
Garbage First and youGarbage First and you
Garbage First and youKai Koenig
 

Similar to Garbage collection (20)

Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
 
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)
 
Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdf
 
JVM Performance Tuning
JVM Performance TuningJVM Performance Tuning
JVM Performance Tuning
 
JVM memory metrics and rules for detecting possible OOM caused crash
JVM memory metrics and rules for detecting possible OOM caused crashJVM memory metrics and rules for detecting possible OOM caused crash
JVM memory metrics and rules for detecting possible OOM caused crash
 
JVM memory metrics and rules for detecting likely OOM caused crash
JVM memory metrics and rules for detecting likely OOM caused crashJVM memory metrics and rules for detecting likely OOM caused crash
JVM memory metrics and rules for detecting likely OOM caused crash
 
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
 
Performance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage CollectionPerformance Tuning - Understanding Garbage Collection
Performance Tuning - Understanding Garbage Collection
 
Choosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
Choosing Right Garbage Collector to Increase Efficiency of Java Memory UsageChoosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
Choosing Right Garbage Collector to Increase Efficiency of Java Memory Usage
 
A novel design of a parallel machine learnt
A novel design of a parallel machine learntA novel design of a parallel machine learnt
A novel design of a parallel machine learnt
 
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector A Novel Design of a Parallel Machine Learnt Generational Garbage Collector
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector
 
An introduction to G1 collector for busy developers
An introduction to G1 collector for busy developersAn introduction to G1 collector for busy developers
An introduction to G1 collector for busy developers
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuning
 
JVM Magic
JVM MagicJVM Magic
JVM Magic
 
Elastic JVM for Scalable Java EE Applications Running in Containers #Jakart...
Elastic JVM  for Scalable Java EE Applications  Running in Containers #Jakart...Elastic JVM  for Scalable Java EE Applications  Running in Containers #Jakart...
Elastic JVM for Scalable Java EE Applications Running in Containers #Jakart...
 
Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage Collector
 
A quick view about Java Virtual Machine
A quick view about Java Virtual MachineA quick view about Java Virtual Machine
A quick view about Java Virtual Machine
 
Java at Scale, Dallas JUG, October 2013
Java at Scale, Dallas JUG, October 2013Java at Scale, Dallas JUG, October 2013
Java at Scale, Dallas JUG, October 2013
 
Garbage First & You
Garbage First & YouGarbage First & You
Garbage First & You
 
Garbage First and you
Garbage First and youGarbage First and you
Garbage First and you
 

Recently uploaded

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 

Recently uploaded (20)

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 

Garbage collection

  • 1. GARBAGE COLLECTOR Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. An in use object, or a referenced object, means that some part of your program still maintains a pointer to that object. An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed. In a programming language like C, allocating and deallocating memory is a manual process. In Java, process of deallocating memory is handled automatically by the garbage collector.
  • 2. The basic process can be described as follows: ● Step 1: Marking The first step in the process is called marking. This is where the garbage collector identifies which pieces of memory are in use and which are not. Referenced objects are shown in blue. Unreferenced objects are shown in gold. All objects are scanned in the marking phase to make this determination. This can be a very time consuming process if all objects in a system must be scanned.
  • 3. Step 2: Normal Deletion Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space. The memory allocator holds references to blocks of free space where new object can be allocated.
  • 4. Step 2a: Deletion with Compacting To further improve performance, in addition to deleting unreferenced objects, you can also compact the remaining referenced objects. By moving referenced object together, this makes new memory allocation much easier and faster.
  • 5. Why Generational Garbage Collection? ● As stated earlier, having to mark and compact all the objects in a JVM is inefficient. As more and more objects are allocated, the list of objects grows and grows leading to longer and longer garbage collection time. However, empirical analysis of applications has shown that most objects are short lived. JVM Generations ● The information learned from the object allocation behavior can be used to enhance the performance of the JVM. Therefore, the heap is broken up into smaller parts or generations. The heap parts are: Young Generation, Old or Tenured Generation Permanent Generation ● The Young Generation is where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. Minor collections can be optimized assuming a high object mortality rate. A young generation full of dead objects is collected very quickly. Some surviving objects are aged and eventually move to the old generation.
  • 6. ● Stop the World Event - All minor garbage collections are "Stop the World" events. This means that all application threads are stopped until the operation completes. Minor garbage collections are always Stop the World events. ● The Old Generation is used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually the old generation needs to be collected. This event is called a major garbage collection. ● Major garbage collection are also Stop the World events. Often a major collection is much slower because it involves all live objects. So for Responsive applications, major garbage collections should be minimized. Also note, that the length of the Stop the World event for a major garbage collection is affected by the kind of garbage collector that is used for the old generation space. ● The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here. ● Classes may get collected (unloaded) if the JVM finds they are no longer needed and space may be needed for other classes. The permanent generation is included in a full garbage collection.
  • 7. The pictures that follow walks through the object allocation and aging process in the JVM: First, any new objects are allocated to the eden space. Both survivor spaces start out empty.
  • 8. When the eden space fills up, a minor garbage collection is triggered.
  • 9. ● Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the eden space is cleared.
  • 10. ● At the next minor GC, the same thing happens for the eden space. Unreferenced objects are deleted and referenced objects are moved to a survivor space. However, in this case, they are moved to the second survivor space (S1). In addition, objects from the last minor GC on the first survivor space (S0) have their age incremented and get moved to S1. Once all surviving objects have been moved to S1, both S0 and eden are cleared. Notice we now have differently aged object in the survivor space.
  • 11. ● At the next minor GC, the same process repeats. However this time the survivor spaces switch. Referenced objects are moved to S0. Surviving objects are aged. Eden and S1 are cleared.
  • 12. ● This slide demonstrates promotion. After a minor GC, when aged objects reach a certain age threshold (8 in this example) they are promoted from young generation to old generation.
  • 13. ● As minor GCs continue to occure objects will continue to be promoted to the old generation space.
  • 14. ● So that pretty much covers the entire process with the young generation. Eventually, a major GC will be performed on the old generation which cleans up and compacts that space.
  • 15.
  • 16. Common Heap Related Switches There are many different command line switches that can be used with Java. Switch Description -Xms Sets the initial heap size for when the JVM starts. -Xmx Sets the maximum heap size. -Xmn Sets the size of the Young Generation. -XX:PermSize Sets the starting size of the Permanent Generation. -XX:MaxPermSize Sets the maximum size of the Permanent Generation The Serial GC The serial collector is the default for client style machines in Java SE 5 and 6. With the serial collector, both minor and major garbage collections are done serially (using a single virtual CPU). In addition, it uses a mark-compact collection method. This method moves older memory to the beginning of the heap so that new memory allocations are made into a single continuous chunk of memory at the end of the heap. This compacting of memory makes it faster to allocate new chunks of memory to the heap.
  • 17. The Parallel GC The parallel garbage collector uses multiple threads to perform the young genertion garbage collection. By default on a host with N CPUs, the parallel garbage collector uses N garbage collector threads in the collection. The number of garbage collector threads can be controlled with command-line options: -XX:ParallelGCThreads=<desired number> On a host with a single CPU the default garbage collector is used even if the parallel garbage collector has been requested. On a host with two CPUs the parallel garbage collector generally performs as well as the default garbage collector and a reduction in the young generation garbage collector pause times can be expected on hosts with more than two CPUs. The Parallel GC comes in two flavors. The Parallel collector is also called a throughput collector. Since it can use multilple CPUs to speed up application throughput. This collector should be used when a lot of work need to be done and long pauses are acceptable. For example, batch processing like printing reports or bills or performing a large number of database queries.
  • 18. Parallel Old GC(-XX:+UseParallelOldGC) ● Parallel Old GC was supported since JDK 5 update. Compared to the parallel GC, the only difference is the GC algorithm for the old generation. It goes through three steps: mark – summary – compaction. The summary step identifies the surviving objects separately for the areas that the GC have previously performed, and thus different from the sweep step of the mark-sweep-compact algorithm. It goes through a little more complicated steps.
  • 19. The Concurrent Mark Sweep (CMS) Collector (-XX:+UseConcMarkSweepGC) The Concurrent Mark Sweep (CMS) collector (also referred to as the concurrent low pause collector) collects the tenured generation. It attempts to minimize the pauses due to garbage collection by doing most of the garbage collection work concurrently with the application threads. Normally the concurrent low pause collector does not copy or compact the live objects. A garbage collection is done without moving the live objects. If fragmentation becomes a problem, allocate a larger heap.
  • 20. The G1 Garbage Collector The Garbage First or G1 garbage collector is available in Java 7 and is designed to be the long term replacement for the CMS collector. The G1 collector is a parallel, concurrent, and incrementally compacting low-pause garbage collector that has quite a different layout from the other garbage collectors described previously. In G1 garbage collector, one object is allocated to each grid, and then a GC is executed. Then, once one area is full, the objects are allocated to another area, and then a GC is executed. The steps where the data moves from the three spaces of the young generation to the old generation cannot be found in this GC type. This type was created to replace the CMS GC, which has causes a lot of issues and complaints in the long term. The biggest advantage of the G1 GC is its performance. It is faster than any other GC types that we have discussed so far. But in JDK 6, this is called an early access and can be used only for a test. It is officially included in JDK 7.