SlideShare a Scribd company logo
1 of 21
Download to read offline
Java Garbage Collection
Presenter: Rupal Chatterjee, Mindfire Solutions
Date: 27/09/2013
Java Memory Management

Presenter: Rupal Chatterjee, Mindfire Solutions
Java Memory Management (Contd...)
The first tier consists of Heap
Stores all created objects in runtime.
PermGen / Method Area
- The segment where the actual compiled Java byte codes resides when loaded.
- Static members (variables or methods) also reside in this segment.
- PermGen is also considered as a part of Heap.
Thread 1..N / Stack
Stores local variables and Reference variables(variables that hold the address of an
object in the heap)

Presenter: Rupal Chatterjee, Mindfire Solutions
Java Memory Management (Contd...)
Key Notes 1. Classes (loaded by the class-loaders) go tp Permanent Generation area.
2. All the static member variables are kept on the Permanent Generation area.
3. All variables except the static ones are kept in Stack.
4. Objects created run-time are stored in heap.
5. There is only one copy of each method per class, be the method static or non-static.
That copy is put in the Permanent Generation area.

Presenter: Rupal Chatterjee, Mindfire Solutions
Java Memory Management (Contd...)
Key Notes (Contd...) 6. For non-static and static methods, all the parameters and local variables go onto the
stack.
7. The return value of a method get stored in stack.
8. Local variables reside in stack.
– Memory allocated at method invocation time.
– Memory deallocated when method returns.
9. Objects reside in heap.
– Memory is allocated with new keyword.
– But never explicitly deallocated.
10. Java uses a automatic mechanism to free heap memory.

Presenter: Rupal Chatterjee, Mindfire Solutions
Java Memory Management (Contd...)

Source: http://blog.pointsoftware.ch

Presenter: Rupal Chatterjee, Mindfire Solutions
Garbage Collection
Key Notes 1. It is a mechanism provided by Java Virtual Machine to reclaim heap space from
objects which are eligible for Garbage collection.
2. Garbage collection relieves java programmer from memory management which is
essential part of C++ programming and gives more time to focus on business logic.
3. Garbage Collection in Java is carried by a thread called Garbage Collector.
4. Before removing an object from memory, Garbage collection thread invokes finalize()
method of that object and gives an opportunity to perform any sort of custom cleanup
required.

Presenter: Rupal Chatterjee, Mindfire Solutions
Garbage Collection (Contd...)
Key Notes (Contd...) 5. Programmer can not force Garbage collection in Java; it will only trigger if JVM thinks
it needs a garbage collection based on Java heap size.
6. There are methods like System.gc() and Runtime.gc() which is used to send request
of Garbage collection to JVM but it’s not guaranteed that garbage collection will be
triggered right away.
7. If there is no memory space present for creating new objects in Heap, Java Virtual
Machine throws OutOfMemoryError or java.lang.OutOfMemoryError.

Presenter: Rupal Chatterjee, Mindfire Solutions
Garbage Collection (Contd...)
When an Object becomes Eligible for Garbage Collection
If its not reachable from any references, in other words you can say that an object
becomes eligible for garbage collection if its all references are null.
Cyclic dependencies are not counted as reference so if Object A has reference of
Object B and Object B has reference of Object A and they don't have any other live
reference then both Objects A and B will be eligible for Garbage collection.
Generally an object becomes eligible for garbage collection in Java on following cases:
1) All references of that object explicitly set to null e.g. object = null
2) Object is created inside a block and reference goes out of scope once control exit that
block.
3) Parent object set to null, if an object holds reference of another object and parent
object's reference set to null, child objects automatically becomes eligible for garbage
collection.

Presenter: Rupal Chatterjee, Mindfire Solutions
Garbage Collection (Contd...)
How it works?
Java objects are created in Heap and Heap is divided into two parts or generations for
sake of garbage collection in Java, these are called as Young Generation and Tenured
or Old Generation.

Presenter: Rupal Chatterjee, Mindfire Solutions
Garbage Collection (Contd...)
How it works? (Contd...)
Young Generation is further divided into three parts.
- Eden space,
- From Space (Survivor 1)
- To Space (Survivor 2)
When an object first created,
1) It gets into Young Generation inside Eden space.
2) After subsequent Minor Garbage Collection, if object survives it gets moved to
From Space / Survivor 1.
3) Then to To Space / Survivor 2.
4) Then eventually object moved to Old or Tenured Generation.

Presenter: Rupal Chatterjee, Mindfire Solutions
Garbage Collection (Contd...)
How it works? (Contd...)
Young Generation is where all new objects are allocated and aged. When the young
generation fills up, this causes a minor garbage collection. 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.
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.
(extract from Oracle site)

Presenter: Rupal Chatterjee, Mindfire Solutions
Type of Garbage Collectors
The Serial GC
1. The serial collector is the default GC in Java SE 5 and 6.
2. Here both minor and major garbage collections are done using a single Thread.
3. It uses a mark-compact collection method.
4. Compacting of memory makes it faster to allocate new chunks of memory to the heap.
5. The Serial GC is the choice for most applications that do not have low pause time
requirements.
6. To enable the Serial Collector use: -XX:+UseSerialGC
Here is a sample command line for starting a sample JAR:
java -XX:+UseSerialGC -jar GcTest.jar
Presenter: Rupal Chatterjee, Mindfire Solutions
Type of Garbage Collectors (Contd...)
The Parallel GC
1. The parallel GC uses multiple threads to perform minor garbage collections.
2. By default on a host with N CPUs, the parallel GC uses N GC threads in the
collection.
3. The number of GC threads can be controlled with command-line options:
-XX:ParallelGCThreads=<desired number>.
4. On a host with a single CPU the default GC is used even if the parallel GC has been
requested.
5. On a host with two CPUs the parallel GC generally performs as well as the default
GC.

Presenter: Rupal Chatterjee, Mindfire Solutions
Type of Garbage Collectors (Contd...)
The Parallel GC (Contd...)
6.. Reduction in the young generation GC pause times can be expected on hosts with
more than two CPUs.
7. There are two ways to enable Parallel GC,
-XX:+UseParallelGC, With this command line option you get a multi-thread young
GC with a single-threaded old GC.
-XX:+UseParallelOldGC, With this option, you get both a multi-threaded young GC
and multi-threaded old GC.

Presenter: Rupal Chatterjee, Mindfire Solutions
Type of Garbage Collectors (Contd...)
The Concurrent Mark Sweep (CMS) Collector
1. It collects the old / tenured generation.
2. Here garbage collection is done concurrently with the application thread. Hence it
reduces the pause time.
3. As it works with live object, compacting is not done here.
4. CMS collectors are used for applications which require low pause time.
5. To enable the CMS Collector use: -XX:+UseConcMarkSweepGC
6. To set the number of threads use: -XX:ParallelCMSThreads=<n>
Here is a sample command line for starting a sample JAR:
java -XX:+UseConcMarkSweepGC -XX:ParallelCMSThreads=2 -jar GcTest.jar

Presenter: Rupal Chatterjee, Mindfire Solutions
Type of Garbage Collectors (Contd...)
The G1 Garbage Collector
1. The Garbage First or G1 garbage collector is available in Java 7.
2. It is designed to be the long term replacement for the CMS collector.
3. The G1 collector is a parallel, concurrent, and compacting low-pause garbage
collector.
4. To enable the G1 Collector use: -XX:+UseG1GC
Here is a sample command line for starting a sample JAR:
java -XX:+UseG1GC -jar GcTest.jar

Presenter: Rupal Chatterjee, Mindfire Solutions
Full GC And Concurrent GC
1. Concurrent GC in java runs concurrently with the application threads.
2. The goal is to complete the collection of Tenured Generation before it becomes full.
3. If the Concurrent GC fails to finish before the Tenured Generation fill up. Then?
4. The application will be paused and the collection is completed with all the application
threads stopped.
5. Such collections with the application stopped are referred as Full GC.
6. Full GC affects performance of Java applications.

Presenter: Rupal Chatterjee, Mindfire Solutions
Summary on Garbage collection in Java
1. Java Heap is divided into three generation for sake of garbage collection. These are
Young Generation, Tenured or Old Generation and PermGen.
2. New objects are created in Young Generation and subsequently moved to Old
Generation.
3. Minor GC is used to move object from Eden space to Survivor 1 and Survivor 2
space and then to Tenured Generation.
4. Whenever Major GC occurs application threads stops during that period, which will
reduce application’s performance.
5. JVM command line options –Xmx and -Xms is used to setup min and max size for
Java Heap. Ideal ratio of this parameter is either 1:1 or 1:1.5.
6. There is no manual way of doing garbage collection in Java.

Presenter: Rupal Chatterjee, Mindfire Solutions
Question and
Answer

Presenter: Rupal Chatterjee, Mindfire Solutions
Thank you

Presenter: Rupal Chatterjee, Mindfire Solutions

More Related Content

What's hot

Introduction to java
Introduction to java Introduction to java
Introduction to java Sandeep Rawat
 
Java Virtual Machine (JVM), Difference JDK, JRE & JVM
Java Virtual Machine (JVM), Difference JDK, JRE & JVMJava Virtual Machine (JVM), Difference JDK, JRE & JVM
Java Virtual Machine (JVM), Difference JDK, JRE & JVMshamnasain
 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 
java memory management & gc
java memory management & gcjava memory management & gc
java memory management & gcexsuns
 
JVM Memory Management Details
JVM Memory Management DetailsJVM Memory Management Details
JVM Memory Management DetailsAzul Systems Inc.
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in JavaSpotle.ai
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)slire
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in javaVishnu Suresh
 

What's hot (20)

Introduction to java
Introduction to java Introduction to java
Introduction to java
 
Java Virtual Machine (JVM), Difference JDK, JRE & JVM
Java Virtual Machine (JVM), Difference JDK, JRE & JVMJava Virtual Machine (JVM), Difference JDK, JRE & JVM
Java Virtual Machine (JVM), Difference JDK, JRE & JVM
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Applets in java
Applets in javaApplets in java
Applets in java
 
java memory management & gc
java memory management & gcjava memory management & gc
java memory management & gc
 
Java GC
Java GCJava GC
Java GC
 
JVM Memory Management Details
JVM Memory Management DetailsJVM Memory Management Details
JVM Memory Management Details
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Thread
ThreadThread
Thread
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 

Viewers also liked

Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage CollectionAzul Systems Inc.
 
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
 
Garbage collection in JVM
Garbage collection in JVMGarbage collection in JVM
Garbage collection in JVMaragozin
 
Understanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About ItUnderstanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About ItAzul Systems Inc.
 
Understanding Garbage Collection
Understanding Garbage CollectionUnderstanding Garbage Collection
Understanding Garbage CollectionDoug Hawkins
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningCarol McDonald
 
Java Garbage Collection
Java Garbage CollectionJava Garbage Collection
Java Garbage CollectionVinay H G
 
15 ooad uml-20
15 ooad uml-2015 ooad uml-20
15 ooad uml-20Niit Care
 
11 ds and algorithm session_16
11 ds and algorithm session_1611 ds and algorithm session_16
11 ds and algorithm session_16Niit Care
 
Vb.net session 09
Vb.net session 09Vb.net session 09
Vb.net session 09Niit Care
 
09 iec t1_s1_oo_ps_session_13
09 iec t1_s1_oo_ps_session_1309 iec t1_s1_oo_ps_session_13
09 iec t1_s1_oo_ps_session_13Niit Care
 
Jdbc session01
Jdbc session01Jdbc session01
Jdbc session01Niit Care
 
Garbage collection algorithms
Garbage collection algorithmsGarbage collection algorithms
Garbage collection algorithmsachinth
 
14 ooad uml-19
14 ooad uml-1914 ooad uml-19
14 ooad uml-19Niit Care
 
Deawsj 7 ppt-2_c
Deawsj 7 ppt-2_cDeawsj 7 ppt-2_c
Deawsj 7 ppt-2_cNiit Care
 

Viewers also liked (20)

Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage Collection
 
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
 
Garbage collection in JVM
Garbage collection in JVMGarbage collection in JVM
Garbage collection in JVM
 
Understanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About ItUnderstanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About It
 
Understanding Garbage Collection
Understanding Garbage CollectionUnderstanding Garbage Collection
Understanding Garbage Collection
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
 
Java Garbage Collection
Java Garbage CollectionJava Garbage Collection
Java Garbage Collection
 
15 ooad uml-20
15 ooad uml-2015 ooad uml-20
15 ooad uml-20
 
Oops recap
Oops recapOops recap
Oops recap
 
11 ds and algorithm session_16
11 ds and algorithm session_1611 ds and algorithm session_16
11 ds and algorithm session_16
 
 
Dacj 2-2 c
Dacj 2-2 cDacj 2-2 c
Dacj 2-2 c
 
Vb.net session 09
Vb.net session 09Vb.net session 09
Vb.net session 09
 
09 iec t1_s1_oo_ps_session_13
09 iec t1_s1_oo_ps_session_1309 iec t1_s1_oo_ps_session_13
09 iec t1_s1_oo_ps_session_13
 
OOP Java
OOP JavaOOP Java
OOP Java
 
Jdbc session01
Jdbc session01Jdbc session01
Jdbc session01
 
Rdbms xp 01
Rdbms xp 01Rdbms xp 01
Rdbms xp 01
 
Garbage collection algorithms
Garbage collection algorithmsGarbage collection algorithms
Garbage collection algorithms
 
14 ooad uml-19
14 ooad uml-1914 ooad uml-19
14 ooad uml-19
 
Deawsj 7 ppt-2_c
Deawsj 7 ppt-2_cDeawsj 7 ppt-2_c
Deawsj 7 ppt-2_c
 

Similar to Java Garbage Collection - How it works

Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdfSudhanshiBakre1
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuningosa_ora
 
JVM Performance Tuning
JVM Performance TuningJVM Performance Tuning
JVM Performance TuningJeremy Leisy
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4aminmesbahi
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMjaganmohanreddyk
 
Jvm performance tuning
Jvm performance tuningJvm performance tuning
Jvm performance tuningIgor Igoroshka
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performanceRoger Xia
 
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
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android ApplicationsLokesh Ponnada
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage CollectionHaim Yadid
 
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 - Memory leaks, Thread deadlocks, JDK tools
Performance Tuning -  Memory leaks, Thread deadlocks, JDK toolsPerformance Tuning -  Memory leaks, Thread deadlocks, JDK tools
Performance Tuning - Memory leaks, Thread deadlocks, JDK toolsHaribabu Nandyal Padmanaban
 
Learn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage CollectionLearn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage CollectionDiya Singh
 
Garbage collector complete information
Garbage collector complete informationGarbage collector complete information
Garbage collector complete informationMubarak Hussain
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsPhillip Koza
 
Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad ideaPVS-Studio
 
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
 

Similar to Java Garbage Collection - How it works (20)

Garbage Collection in Java.pdf
Garbage Collection in Java.pdfGarbage Collection in Java.pdf
Garbage Collection in Java.pdf
 
Profiler Guided Java Performance Tuning
Profiler Guided Java Performance TuningProfiler Guided Java Performance Tuning
Profiler Guided Java Performance Tuning
 
Javasession10
Javasession10Javasession10
Javasession10
 
JVM Performance Tuning
JVM Performance TuningJVM Performance Tuning
JVM Performance Tuning
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
 
Jvm performance tuning
Jvm performance tuningJvm performance tuning
Jvm performance tuning
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performance
 
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)
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
 
Let's talk about Garbage Collection
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage Collection
 
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 - Memory leaks, Thread deadlocks, JDK tools
Performance Tuning -  Memory leaks, Thread deadlocks, JDK toolsPerformance Tuning -  Memory leaks, Thread deadlocks, JDK tools
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
 
Learn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage CollectionLearn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage Collection
 
Garbage collector complete information
Garbage collector complete informationGarbage collector complete information
Garbage collector complete information
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java Applications
 
Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad idea
 
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
 

More from Mindfire Solutions (20)

Physician Search and Review
Physician Search and ReviewPhysician Search and Review
Physician Search and Review
 
diet management app
diet management appdiet management app
diet management app
 
Business Technology Solution
Business Technology SolutionBusiness Technology Solution
Business Technology Solution
 
Remote Health Monitoring
Remote Health MonitoringRemote Health Monitoring
Remote Health Monitoring
 
Influencer Marketing Solution
Influencer Marketing SolutionInfluencer Marketing Solution
Influencer Marketing Solution
 
ELMAH
ELMAHELMAH
ELMAH
 
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure Applications
 
IOT Hands On
IOT Hands OnIOT Hands On
IOT Hands On
 
Glimpse of Loops Vs Set
Glimpse of Loops Vs SetGlimpse of Loops Vs Set
Glimpse of Loops Vs Set
 
Oracle Sql Developer-Getting Started
Oracle Sql Developer-Getting StartedOracle Sql Developer-Getting Started
Oracle Sql Developer-Getting Started
 
Adaptive Layout In iOS 8
Adaptive Layout In iOS 8Adaptive Layout In iOS 8
Adaptive Layout In iOS 8
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/Mac
 
LINQPad - utility Tool
LINQPad - utility ToolLINQPad - utility Tool
LINQPad - utility Tool
 
Get started with watch kit development
Get started with watch kit developmentGet started with watch kit development
Get started with watch kit development
 
Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
 
Material Design in Android
Material Design in AndroidMaterial Design in Android
Material Design in Android
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
 
Ext js Part 2- MVC
Ext js Part 2- MVCExt js Part 2- MVC
Ext js Part 2- MVC
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
 
Spring Security Introduction
Spring Security IntroductionSpring Security Introduction
Spring Security Introduction
 

Recently uploaded

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Java Garbage Collection - How it works

  • 1. Java Garbage Collection Presenter: Rupal Chatterjee, Mindfire Solutions Date: 27/09/2013
  • 2. Java Memory Management Presenter: Rupal Chatterjee, Mindfire Solutions
  • 3. Java Memory Management (Contd...) The first tier consists of Heap Stores all created objects in runtime. PermGen / Method Area - The segment where the actual compiled Java byte codes resides when loaded. - Static members (variables or methods) also reside in this segment. - PermGen is also considered as a part of Heap. Thread 1..N / Stack Stores local variables and Reference variables(variables that hold the address of an object in the heap) Presenter: Rupal Chatterjee, Mindfire Solutions
  • 4. Java Memory Management (Contd...) Key Notes 1. Classes (loaded by the class-loaders) go tp Permanent Generation area. 2. All the static member variables are kept on the Permanent Generation area. 3. All variables except the static ones are kept in Stack. 4. Objects created run-time are stored in heap. 5. There is only one copy of each method per class, be the method static or non-static. That copy is put in the Permanent Generation area. Presenter: Rupal Chatterjee, Mindfire Solutions
  • 5. Java Memory Management (Contd...) Key Notes (Contd...) 6. For non-static and static methods, all the parameters and local variables go onto the stack. 7. The return value of a method get stored in stack. 8. Local variables reside in stack. – Memory allocated at method invocation time. – Memory deallocated when method returns. 9. Objects reside in heap. – Memory is allocated with new keyword. – But never explicitly deallocated. 10. Java uses a automatic mechanism to free heap memory. Presenter: Rupal Chatterjee, Mindfire Solutions
  • 6. Java Memory Management (Contd...) Source: http://blog.pointsoftware.ch Presenter: Rupal Chatterjee, Mindfire Solutions
  • 7. Garbage Collection Key Notes 1. It is a mechanism provided by Java Virtual Machine to reclaim heap space from objects which are eligible for Garbage collection. 2. Garbage collection relieves java programmer from memory management which is essential part of C++ programming and gives more time to focus on business logic. 3. Garbage Collection in Java is carried by a thread called Garbage Collector. 4. Before removing an object from memory, Garbage collection thread invokes finalize() method of that object and gives an opportunity to perform any sort of custom cleanup required. Presenter: Rupal Chatterjee, Mindfire Solutions
  • 8. Garbage Collection (Contd...) Key Notes (Contd...) 5. Programmer can not force Garbage collection in Java; it will only trigger if JVM thinks it needs a garbage collection based on Java heap size. 6. There are methods like System.gc() and Runtime.gc() which is used to send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will be triggered right away. 7. If there is no memory space present for creating new objects in Heap, Java Virtual Machine throws OutOfMemoryError or java.lang.OutOfMemoryError. Presenter: Rupal Chatterjee, Mindfire Solutions
  • 9. Garbage Collection (Contd...) When an Object becomes Eligible for Garbage Collection If its not reachable from any references, in other words you can say that an object becomes eligible for garbage collection if its all references are null. Cyclic dependencies are not counted as reference so if Object A has reference of Object B and Object B has reference of Object A and they don't have any other live reference then both Objects A and B will be eligible for Garbage collection. Generally an object becomes eligible for garbage collection in Java on following cases: 1) All references of that object explicitly set to null e.g. object = null 2) Object is created inside a block and reference goes out of scope once control exit that block. 3) Parent object set to null, if an object holds reference of another object and parent object's reference set to null, child objects automatically becomes eligible for garbage collection. Presenter: Rupal Chatterjee, Mindfire Solutions
  • 10. Garbage Collection (Contd...) How it works? Java objects are created in Heap and Heap is divided into two parts or generations for sake of garbage collection in Java, these are called as Young Generation and Tenured or Old Generation. Presenter: Rupal Chatterjee, Mindfire Solutions
  • 11. Garbage Collection (Contd...) How it works? (Contd...) Young Generation is further divided into three parts. - Eden space, - From Space (Survivor 1) - To Space (Survivor 2) When an object first created, 1) It gets into Young Generation inside Eden space. 2) After subsequent Minor Garbage Collection, if object survives it gets moved to From Space / Survivor 1. 3) Then to To Space / Survivor 2. 4) Then eventually object moved to Old or Tenured Generation. Presenter: Rupal Chatterjee, Mindfire Solutions
  • 12. Garbage Collection (Contd...) How it works? (Contd...) Young Generation is where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. 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. 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. (extract from Oracle site) Presenter: Rupal Chatterjee, Mindfire Solutions
  • 13. Type of Garbage Collectors The Serial GC 1. The serial collector is the default GC in Java SE 5 and 6. 2. Here both minor and major garbage collections are done using a single Thread. 3. It uses a mark-compact collection method. 4. Compacting of memory makes it faster to allocate new chunks of memory to the heap. 5. The Serial GC is the choice for most applications that do not have low pause time requirements. 6. To enable the Serial Collector use: -XX:+UseSerialGC Here is a sample command line for starting a sample JAR: java -XX:+UseSerialGC -jar GcTest.jar Presenter: Rupal Chatterjee, Mindfire Solutions
  • 14. Type of Garbage Collectors (Contd...) The Parallel GC 1. The parallel GC uses multiple threads to perform minor garbage collections. 2. By default on a host with N CPUs, the parallel GC uses N GC threads in the collection. 3. The number of GC threads can be controlled with command-line options: -XX:ParallelGCThreads=<desired number>. 4. On a host with a single CPU the default GC is used even if the parallel GC has been requested. 5. On a host with two CPUs the parallel GC generally performs as well as the default GC. Presenter: Rupal Chatterjee, Mindfire Solutions
  • 15. Type of Garbage Collectors (Contd...) The Parallel GC (Contd...) 6.. Reduction in the young generation GC pause times can be expected on hosts with more than two CPUs. 7. There are two ways to enable Parallel GC, -XX:+UseParallelGC, With this command line option you get a multi-thread young GC with a single-threaded old GC. -XX:+UseParallelOldGC, With this option, you get both a multi-threaded young GC and multi-threaded old GC. Presenter: Rupal Chatterjee, Mindfire Solutions
  • 16. Type of Garbage Collectors (Contd...) The Concurrent Mark Sweep (CMS) Collector 1. It collects the old / tenured generation. 2. Here garbage collection is done concurrently with the application thread. Hence it reduces the pause time. 3. As it works with live object, compacting is not done here. 4. CMS collectors are used for applications which require low pause time. 5. To enable the CMS Collector use: -XX:+UseConcMarkSweepGC 6. To set the number of threads use: -XX:ParallelCMSThreads=<n> Here is a sample command line for starting a sample JAR: java -XX:+UseConcMarkSweepGC -XX:ParallelCMSThreads=2 -jar GcTest.jar Presenter: Rupal Chatterjee, Mindfire Solutions
  • 17. Type of Garbage Collectors (Contd...) The G1 Garbage Collector 1. The Garbage First or G1 garbage collector is available in Java 7. 2. It is designed to be the long term replacement for the CMS collector. 3. The G1 collector is a parallel, concurrent, and compacting low-pause garbage collector. 4. To enable the G1 Collector use: -XX:+UseG1GC Here is a sample command line for starting a sample JAR: java -XX:+UseG1GC -jar GcTest.jar Presenter: Rupal Chatterjee, Mindfire Solutions
  • 18. Full GC And Concurrent GC 1. Concurrent GC in java runs concurrently with the application threads. 2. The goal is to complete the collection of Tenured Generation before it becomes full. 3. If the Concurrent GC fails to finish before the Tenured Generation fill up. Then? 4. The application will be paused and the collection is completed with all the application threads stopped. 5. Such collections with the application stopped are referred as Full GC. 6. Full GC affects performance of Java applications. Presenter: Rupal Chatterjee, Mindfire Solutions
  • 19. Summary on Garbage collection in Java 1. Java Heap is divided into three generation for sake of garbage collection. These are Young Generation, Tenured or Old Generation and PermGen. 2. New objects are created in Young Generation and subsequently moved to Old Generation. 3. Minor GC is used to move object from Eden space to Survivor 1 and Survivor 2 space and then to Tenured Generation. 4. Whenever Major GC occurs application threads stops during that period, which will reduce application’s performance. 5. JVM command line options –Xmx and -Xms is used to setup min and max size for Java Heap. Ideal ratio of this parameter is either 1:1 or 1:1.5. 6. There is no manual way of doing garbage collection in Java. Presenter: Rupal Chatterjee, Mindfire Solutions
  • 20. Question and Answer Presenter: Rupal Chatterjee, Mindfire Solutions
  • 21. Thank you Presenter: Rupal Chatterjee, Mindfire Solutions