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 ...
Java Memory Management (Contd...)
Key Notes 1. Classes (loaded by the class-loaders) go tp Permanent Generation area.
2. A...
Java Memory Management (Contd...)
Key Notes (Contd...) 6. For non-static and static methods, all the parameters and local ...
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 whic...
Garbage Collection (Contd...)
Key Notes (Contd...) 5. Programmer can not force Garbage collection in Java; it will only tr...
Garbage Collection (Contd...)
When an Object becomes Eligible for Garbage Collection
If its not reachable from any referen...
Garbage Collection (Contd...)
How it works?
Java objects are created in Heap and Heap is divided into two parts or generat...
Garbage Collection (Contd...)
How it works? (Contd...)
Young Generation is further divided into three parts.
- Eden space,...
Garbage Collection (Contd...)
How it works? (Contd...)
Young Generation is where all new objects are allocated and aged. W...
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 ...
Type of Garbage Collectors (Contd...)
The Parallel GC
1. The parallel GC uses multiple threads to perform minor garbage co...
Type of Garbage Collectors (Contd...)
The Parallel GC (Contd...)
6.. Reduction in the young generation GC pause times can ...
Type of Garbage Collectors (Contd...)
The Concurrent Mark Sweep (CMS) Collector
1. It collects the old / tenured generatio...
Type of Garbage Collectors (Contd...)
The G1 Garbage Collector
1. The Garbage First or G1 garbage collector is available i...
Full GC And Concurrent GC
1. Concurrent GC in java runs concurrently with the application threads.
2. The goal is to compl...
Summary on Garbage collection in Java
1. Java Heap is divided into three generation for sake of garbage collection. These ...
Question and
Answer

Presenter: Rupal Chatterjee, Mindfire Solutions
Thank you

Presenter: Rupal Chatterjee, Mindfire Solutions
Upcoming SlideShare
Loading in...5
×

Java Garbage Collection - How it works

2,063

Published on

This session is all about - the mechanism provided by Java Virtual Machine to reclaim heap space from objects which are eligible for Garbage collection.

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,063
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
240
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Java Garbage Collection - How it works

  1. 1. Java Garbage Collection Presenter: Rupal Chatterjee, Mindfire Solutions Date: 27/09/2013
  2. 2. Java Memory Management Presenter: Rupal Chatterjee, Mindfire Solutions
  3. 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. 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. 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. 6. Java Memory Management (Contd...) Source: http://blog.pointsoftware.ch Presenter: Rupal Chatterjee, Mindfire Solutions
  7. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 20. Question and Answer Presenter: Rupal Chatterjee, Mindfire Solutions
  21. 21. Thank you Presenter: Rupal Chatterjee, Mindfire Solutions
  1. ¿Le ha llamado la atención una diapositiva en particular?

    Recortar diapositivas es una manera útil de recopilar información importante para consultarla más tarde.

×