Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why multiple garbage collectors in Java?

Every Java process I start on my machine seems to have two garbage collectors by default. I'm checking this via JConsole.

Example - for my currently running Eclipse.

PS MarkSweep

Collection Count - 221
Collection Time - 102118
Memory Pool Names - java.lang.String[4]

PS Scavenge

Collection Count - 241
Collection Time - 2428
Memory Pool Names - java.lang.String[2]

I am assuming they have overlapping pools. How do two garbage collectors work together when using the same pools (Eden, survivor, old gen)? Is there no overlap in movement of objects between pools (like movement from one survivor to another when the second algorithm is called)? Even if it is not, why do we need more than one collector per pool?

I have read this article on GC. They refer to using different collectors for different major and minor GC, but there seems to be no reference to using multiple collectors on the same pool.

like image 991
Aditya Avatar asked Apr 16 '13 08:04

Aditya


People also ask

How many garbage collectors are there in Java?

Types of Garbage Collectors in the Java Virtual Machine Garbage collection makes Java memory efficient because it removes the unreferenced objects from heap memory and makes free space for new objects. The Java Virtual Machine has eight types of garbage collectors.

How often does GC occur in Java?

That is, every time you allocate a small block of memory (big blocks are typically placed directly into "older" generations), the system checks whether there's enough free space in the gen-0 heap, and if there isn't, it runs the GC to free up space for the allocation to succeed.

What is the purpose of Java garbage collector?

The biggest benefit of Java garbage collection is that it automatically handles the deletion of unused objects or objects that are out of reach to free up vital memory resources. Programmers working in languages without garbage collection (like C and C++) must implement manual memory management in their code.

How do you know which garbage collector to use?

You can use -XX flag for JRE to choose the garbage collector of your choice. Additionally, you can use JConsole to monitor garbage collection.


2 Answers

I am assuming they have overlapping pools.

This assumption is wrong. PS Scavenge will be used on the young (eden, survivor) generation and PS MarkSweep will be used on the old generation. The only "overlap" is that PS Scavenge will move objects into the old generation once they've been around a while and let PS MarkSweep deal with them then.

The benefit of having different garbage collectors for different pools is that an algorithm that works well for objects in the eden pool isn't necessarily going to work well for old generation objects.

This article covers the various options for different garbage collectors working together.

As far as "major" collections which occur when there is no space to move objects into the old generation, this (admittedly old) whitepaper from Sun says the following:

...the young generation collection algorithm is not run. Instead, the old generation collection algorithm is used on the entire heap.

like image 179
Dave Webb Avatar answered Nov 04 '22 06:11

Dave Webb


Form the article you've provided:

Typically some fraction of the surviving objects from the young generation are moved to the tenured generation during each minor collection. Eventually, the tenured generation will fill up and must be collected, resulting in a major collection, in which the entire heap is collected. Major collections usually last much longer than minor collections because a significantly larger number of objects are involved.

like image 31
Mikhail Avatar answered Nov 04 '22 05:11

Mikhail