Java 7 (JDK 7) garbage collection and documentation on G1
Asked Answered
M

8

82

Java 7 has been out for a while now, but I cannot find any good resources on the configuration of the garbage collectors, specifically the new G1 collector.

My questions:

  1. Is G1 the default collector in Java 7 and if not how do I activate G1?
  2. What optional settings does g1 have in Java7?
  3. Were there any changes made to other collectors like cms or the parallel collector in Java 7?
  4. Where can I find good documentation on garbage collection in Java 7?
Maieutic answered 13/11, 2011 at 11:50 Comment(2)
Getting Started with the G1 Garbage Collector also gives a good overview with best practices.Attention
oracle.com/technetwork/articles/java/g1gc-1984535.htmlInaugural
R
47

The G1 garbage collector is not the default in my installation of Java, version 1.7.0_01. You can see for yourself by using with some extra command line options:

> java -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -version
-XX:InitialHeapSize=132304640 -XX:MaxHeapSize=2116874240 -XX:ParallelGCThreads=4 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) 64-Bit Server VM (build 21.1-b02, mixed mode)
Heap
 PSYoungGen      total 37696K, used 1293K [0x00000007d5eb0000, 0x00000007d88c0000, 0x0000000800000000)
  eden space 32320K, 4% used [0x00000007d5eb0000,0x00000007d5ff3408,0x00000007d7e40000)
  from space 5376K, 0% used [0x00000007d8380000,0x00000007d8380000,0x00000007d88c0000)
  to   space 5376K, 0% used [0x00000007d7e40000,0x00000007d7e40000,0x00000007d8380000)
 PSOldGen        total 86144K, used 0K [0x0000000781c00000, 0x0000000787020000, 0x00000007d5eb0000)
  object space 86144K, 0% used [0x0000000781c00000,0x0000000781c00000,0x0000000787020000)
 PSPermGen       total 21248K, used 2032K [0x000000077ca00000, 0x000000077dec0000, 0x0000000781c00000)
  object space 21248K, 9% used [0x000000077ca00000,0x000000077cbfc288,0x000000077dec0000)

You don't need to enable experimental options to turn on the G1 collector any more, though:

> java -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseG1GC -version
-XX:InitialHeapSize=132304640 -XX:MaxHeapSize=2116874240 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedOops -XX:+UseG1GC -XX:-UseLargePagesIndividualAllocation
java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) 64-Bit Server VM (build 21.1-b02, mixed mode)
Heap
 garbage-first heap   total 130048K, used 0K [0x000000077ca00000, 0x0000000784900000, 0x00000007fae00000)
  region size 1024K, 1 young (1024K), 0 survivors (0K)
 compacting perm gen  total 20480K, used 2032K [0x00000007fae00000, 0x00000007fc200000, 0x0000000800000000)
   the space 20480K,   9% used [0x00000007fae00000, 0x00000007faffc288, 0x00000007faffc400, 0x00000007fc200000)
No shared spaces configured.

I don't know where you can find any good documentation.

Riptide answered 17/11, 2011 at 1:53 Comment(2)
This is still valid for 1.7.0_09 on OSXBenz
Not true for Oracle JDK 7u17 on linux/amd64 fetched directly from Oracle website. It says -XX:+UseParallelGC.Exponible
M
31

Oracle finally made G1 official in Java 7 U4: http://www.oracle.com/technetwork/java/javase/7u4-relnotes-1575007.html

Description: http://docs.oracle.com/javase/7/docs/technotes/guides/vm/G1.html

Command line options: http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html#G1Options

Still, I do not think it is the default collector in Java 7. For servers the default is the Parallel Collector as in Java 6.

Maieutic answered 10/5, 2012 at 14:19 Comment(1)
and server is defined by 2 cores and 2 GB ram or more. Details can be found via hg.openjdk.java.net/jdk7u/jdk7u/hotspot/file/0d82bf449a61/src -- look at the files ./share/tools/launcher/java.c and ./share/vm/runtime/os.cppExponible
P
22

Yes, G1 is the new standard garbage collector in Java 1.7 JVM.

Here you can find plenty of information on how to use and configre the new garbage collector:

Using G1 G1 is still considered experimental and can be enabled with the following two parameters:

-XX:+UnlockExperimentalVMOptions -XX:+UseG1GC

To set a GC pause time goal, use the following parameter:

-XX:MaxGCPauseMillis =50 (for a pause time target of 50ms)

With G1, a time interval can be specified during which a GC pause should last no longer than the time given above:

-XX:GCPauseIntervalMillis =200 (for a pause interval target of 200ms)

Note that the above two options represent goals, not promises or guarantees. They might work well in some situations but not in others, and the GC might not always be able to obey them.

Alternatively, the size of the young generation can be specified explicitly to impact evacuation pause times:

-XX:+G1YoungGenSize=512m (for a 512 megabyte young generation)

G1 also uses the equivalent of survivor spaces, which are, naturally, a set of (potentially non-contiguous) regions. Their size can be specified with the usual parameters (e.g., -XX:SurvivorRatio=6).

Finally, to run G1 at its full potential, try setting these two parameters which are currently disabled by default because they may uncover a rare race condition:

-XX:+G1ParallelRSetUpdatingEnabled -XX:+G1ParallelRSetScanningEnabled

One more thing to note is that G1 is very verbose compared to other HotSpot GCs when -XX:+PrintGCDetails is set. This is because it prints per-GC-thread timings and other information very helpful in profiling and trouble-shooting. If you want a more concise GC log, please switch to using -verbosegc (though it is recommended that the more detailed GC log be obtained).

I have also found this article very helpful in understanding the inners of G1.

Even more info here.

Puritanism answered 13/11, 2011 at 11:56 Comment(1)
I have seen these resources. But the first article is about G1 in JDK 6 when it was still an experimental option. The other articles are about beta releases of the JDK 7 and at least 1 year old. I am looking for more up to date information or official documentation from Oracle or the JDK team.Maieutic
C
13

1. Is G1 the default collector in Java 7 (...)

The rule on this Java 5 page is still applicable in Java 7 (and AFAIK, Java 8):

On server-class machines running the server VM, the garbage collector (GC) has changed from the previous serial collector (-XX:+UseSerialGC) to a parallel collector (-XX:+UseParallelGC).

But also consider:

  • 64-bit JVMs do not come with a -client VM, so are always "server class"
  • Since Java 7, using -XX:+UseParallelGC (whether set or implied) additionally implies -XX:+UseParallelOldGC (i.e. unless explicitly disabled)

For example, if on Windows x64 you run...

  • Java 7 64-bit, you get Parallel GC (for both young and old generations) by default.
  • Java 8 32-bit, you get Serial GC (for both generations) by default

1. (...) how do I activate G1?

As of Java 7, simply -XX:+UseG1GC. Perhaps also of interest is when you would want to:

Applications running today with either the CMS or the ParallelOld garbage collector would benefit switching to G1 if the application has one or more of the following traits.

  • More than 50% of the Java heap is occupied with live data.
  • The rate of object allocation rate or promotion varies significantly.
  • Undesired long garbage collection or compaction pauses (longer than 0.5 to 1 second)

2. What optional settings does g1 have in Java7?

I've not used G1 myself, but I gather that it adheres to the same basic "throughput / ergonomic" flags used to tune the other parallel collectors. In my experience with the Parallel GC, -XX:GCTimeRatio has been the pivotal one in providing the expected speed-memory tradeoff. YMMV.

G1-specific options are listed here

3. Were there changes to (...) cms or the parallel collector in Java 7?

Don't know, but...

G1 is planned as the long term replacement for the Concurrent Mark-Sweep Collector (CMS)

4. Where can I find good documentation on garbage collection in Java 7?

It can be a pain to find, can't it? Probably the best "hub" page I've found is this one:

http://www.oracle.com/technetwork/java/javase/tech/index-jsp-140228.html

Some deep reading required, but worth the time if you need to do some tuning. Particularly insightful is: Garbage Collector Ergonomics

Chenab answered 8/12, 2013 at 22:20 Comment(0)
D
11

1. Is G1 the default collector in Java 7 and if not how do I activate G1?

G1 is not default collector in Java 7. -XX:+UseG1GC will enable G1GC

2. What optional settings does g1 have in Java7?

There are many. Have a look at this oracle documentation page for complete information.

The G1 GC is an adaptive garbage collector with defaults that enable it to work efficiently without modification.

Due to this reason, customize critical parameters

-XX:MaxGCPauseMillis
-XX:G1HeapRegionSize
-XX:ParallelGCThreads
-XX:ConcGCThreads

and leave all other parameters to default value.

You have re-configured many G1GC parameters, which are not required if you follow above documentation page. Please cross check with above recommendations especially on ParallelGCThreads and ConcGCThreads, which are to be based on your CPU cores. Remove re-configuration of un-necessary parameters.

Young Generation Size: Avoid explicitly setting young generation size with the -Xmn option -XX:NewRatio. Fixing the size of the young generation overrides the target pause-time goal.

3. Were there any changes made to other collectors like cms or the parallel collector in Java 7?

There are some changes with Java 7. Have a look at this oracle release notes page.

4. Where can I find good documentation on garbage collection in Java 7?

Refer to oracle tutorial page

Dime answered 13/12, 2015 at 18:19 Comment(0)
C
3

No G1 is not default garbage collector in jdk 1.7.0_02. The default garbage collector depends upon the class of machine. If the machine is of Server class then the default garbage collector is Throughput Collector. If the machine is of Client class then the default garbage collector is Serial Collector.

Cathode answered 27/4, 2012 at 7:50 Comment(2)
I contest that this is entirely correct. Java 5 reference, still valid. On a Windows system (32/64): Run Java 32-bit (5..8) >> Serial GC by default. Run Java 64-bit (5..6) >> ParallelGC (Young gen only) by default. Run Java 64-bit (7..8) >> ParallelOldGC (parallel Young and Old) by default. Reference for Java 7 change, finally 'parallel' collector == 'throughput' collector"Chenab
(A harsh response for this person's first, and accurate, SO contribution. I note it's also their last.)Chenab
P
2

The documentation available at http://www.oracle.com/technetwork/java/javase/tech/g1-intro-jsp-135488.html (the link provided by Wojtek) seems to be the only official link with info but the info seems outdated as some of the flags mentioned there were only available in the test builds, they no longer exist in the production releases. Some one from Oracle should provide some updated documentation on the G1 GC.

Phenolic answered 17/1, 2012 at 17:10 Comment(0)
N
0

By default you don't really want to use G1 collector, as it is not really better than the others. It is only good for special purposes.

In low latency application is is sligthly better than CMS, as it has a little bit shorter, and more predictable pause times. In exchange the throughput is much worse than CMS in exchange.

So it is only good if the latency is important, but the throughput is not important at all. If both are important, then stay with CMS.

Neal answered 8/1, 2015 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.