`

JAVA GC

 
阅读更多

UseParallelGC   
    Universe::_collectedHeap = new ParallelScavengeHeap();
UseG1GC        
    G1CollectorPolicy* g1p = new G1CollectorPolicy();
    g1p->initialize_all();
    G1CollectedHeap* g1h = new G1CollectedHeap(g1p);
    Universe::_collectedHeap = g1h;

UseSerialGC
    GenCollectorPolicy gc_policy = new MarkSweepPolicy();
    Universe::_collectedHeap = new GenCollectedHeap(gc_policy);

UseConcMarkSweepGC
      GenCollectorPolicy gc_policy
      if (UseAdaptiveSizePolicy) {
        gc_policy = new ASConcurrentMarkSweepPolicy();
      } else {
        gc_policy = new ConcurrentMarkSweepPolicy();
      }         
     Universe::_collectedHeap = new GenCollectedHeap(gc_policy);
其他GC[UseParNewGC,UseParallelOldGC]
    GenCollectorPolicy gc_policy = new MarkSweepPolicy();
    Universe::_collectedHeap = new GenCollectedHeap(gc_policy);
   
   
   
bool Arguments::check_gc_consistency() {
  check_gclog_consistency();
  bool status = true;
  // Ensure that the user has not selected conflicting sets
  // of collectors. [Note: this check is merely a user convenience;
  // collectors over-ride each other so that only a non-conflicting
  // set is selected; however what the user gets is not what they
  // may have expected from the combination they asked for. It's
  // better to reduce user confusion by not allowing them to
  // select conflicting combinations.
  uint i = 0;
  if (UseSerialGC)                       i++;
  if (UseConcMarkSweepGC || UseParNewGC) i++;
  if (UseParallelGC || UseParallelOldGC) i++;
  if (UseG1GC)                           i++;
  if (i > 1) {
    jio_fprintf(defaultStream::error_stream(),
                "Conflicting collector combinations in option list; "
                "please refer to the release notes for the combinations "
                "allowed\n");
    status = false;
  }
  return status;
}   

 

1.  SerialGC 

参数-XX:+UseSerialGC

就是Young区和old区都使用serial 垃圾回收算法

2.  ParallelGC 

参数-XX:+UseParallelGC

Young区:使用Parallel scavenge 回收算法

Old  区:可以使用单线程的或者Parallel 垃圾回收算法,由 -XX:+UseParallelOldGC 来控制

3.  CMS  

参数-XX:+UseConcMarkSweepGC

Young区:可以使用普通的或者parallel 垃圾回收算法,由参数 -XX:+UseParNewGC来控制

Old 区:只能使用Concurrent Mark Sweep 

4. G1 

参数:-XX:+UseG1GC

没有young/old区

 

Young generation collectors

 

Copy (enabled with -XX:+UseSerialGC) -
the serial copy collector, uses one thread to copy surviving objects from Eden to Survivor spaces and between Survivor spaces until it decides they've been there long enough, at which point it copies them into the old generation.
PS Scavenge (enabled with -XX:+UseParallelGC) -
the parallel scavenge collector, like the Copy collector, but uses multiple threads in parallel and has some knowledge of how the old generation is collected (essentially written to work with the serial and PS old gen collectors).
ParNew (enabled with -XX:+UseParNewGC) -
the parallel copy collector, like the Copy collector, but uses multiple threads in parallel and has an internal 'callback' that allows an old generation collector to operate on the objects it collects (really written to work with the concurrent collector).
G1 Young Generation (enabled with -XX:+UseG1GC) -
the garbage first collector, uses the 'Garbage First' algorithm which splits up the heap into lots of smaller spaces, but these are still separated into Eden and Survivor spaces in the young generation for G1.

Old generation collectors

 

MarkSweepCompact (enabled with -XX:+UseSerialGC) -
the serial mark-sweep collector, the daddy of them all, uses a serial (one thread) full mark-sweep garbage collection algorithm, with optional compaction.
PS MarkSweep (enabled with -XX:+UseParallelOldGC) -
the parallel scavenge mark-sweep collector, parallelised version (i.e. uses multiple threads) of the MarkSweepCompact.
ConcurrentMarkSweep (enabled with -XX:+UseConcMarkSweepGC) -
the concurrent collector, a garbage collection algorithm that attempts to do most of the garbage collection work in the background without stopping application threads while it works (there are still phases where it has to stop application threads, but these phases are attempted to be kept to a minimum). Note if the concurrent collector fails to keep up with the garbage, it fails over to the serial MarkSweepCompact collector for (just) the next GC.
G1 Mixed Generation (enabled with -XX:+UseG1GC) -
the garbage first collector, uses the 'Garbage First' algorithm which splits up the heap into lots of smaller spaces.

All of the garbage collection algorithms except ConcurrentMarkSweep are stop-the-world, i.e. they stop all application threads while they operate - the stop is known as 'pause' time. The ConcurrentMarkSweep tries to do most of it's work in the background and minimize the pause time, but it also has a stop-the-world phase and can fail into the MarkSweepCompact which is fully stop-the-world. (The G1 collector has a concurrent phase but is currently mostly stop-the-world).

Combinations of Garbage Collectors

That's the set of garbage collectors available, but they operate in two different heap spaces and it's the combination that is what we actually end up with for a particular JVM setting, so I'll also list the combinations that are possible. It doesn't explode into a dozen combinations because not all of these collectors work with each other. G1 is effectively an antisocial collector that doesn't like working with anyone else; the serial collectors are the "last man picked" collectors; the 'PS' collectors like to work with each other; and ParNew and Concurrent like to work together. From Java 9 that's pretty much it (with the addition of being able to turn off adaptive sizing policy) Before that it wasn't quite as simple as that, so here is the list of what I consider are the main options in terms of garbage collection algorithm options. As an aside for those who like a bit of trivia, we have actually lost one garbage collection algorithm over time, the "train" (incremental) garbage collector which was available in various JVMs as -Xincgc and -XX:+UseTrainGC - these flags are no longer useful, the former flag is just silently converted into using ParNew and Concurrent, while the latter flag will cause a startup error in this JVM, and from Java 9 we also lose the incremental algorithm in the concurrent collector.

The full list of possible GC algorithm combinations that can work are:

Command Options* Resulting Collector Combination
-XX:+UseSerialGC young Copy and old MarkSweepCompact
-XX:+UseG1GC young G1 Young and old G1 Mixed
-XX:+UseParallelGC -XX:+UseParallelOldGC -XX:+UseAdaptiveSizePolicy young PS Scavenge old PS MarkSweep with adaptive sizing
-XX:+UseParallelGC -XX:+UseParallelOldGC -XX:-UseAdaptiveSizePolicy young PS Scavenge old PS MarkSweep, no adaptive sizing
-XX:+UseParNewGC (deprecated in Java 8 and removed in Java 9 - for ParNew see the line below which is NOT deprecated) young ParNew old MarkSweepCompact
-XX:+UseConcMarkSweepGC -XX:+UseParNewGC young ParNew old ConcurrentMarkSweep**
-XX:+UseConcMarkSweepGC -XX:-UseParNewGC (deprecated in Java 8 and removed in Java 9) young Copy old ConcurrentMarkSweep**
*All the combinations listed here will fail to let the JVM start if you add another GC algorithm not listed, with the exception of -XX:+UseParNewGC which is only combinable with -XX:+UseConcMarkSweepGC
**there are many many options for use with -XX:+UseConcMarkSweepGC which change the algorithm, e.g.
  • -XX:+/-CMSIncrementalMode (deprecated in Java 8 and removed in Java 9) - uses or disables an incremental concurrent GC algorithm
  • -XX:+/-CMSConcurrentMTEnabled - uses or disables parallel (multiple threads) concurrent GC algorithm
  • -XX:+/-UseCMSCompactAtFullCollection - uses or disables a compaction when a full GC occurs

Other options equivalent to one of the above:

Command Options Used On Their Own Equivalent To Entry In Table Above
-XX:+UseParallelGC -XX:+UseParallelGC -XX:+UseParallelOldGC
-XX:+UseParallelOldGC -XX:+UseParallelGC -XX:+UseParallelOldGC
-Xincgc (deprecated in Java 8 and removed in Java 9) -XX:+UseParNewGC -XX:+UseConcMarkSweepGC
-XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC
no option on most Windows -XX:+UseG1GC from Java 9, or before that -XX:+UseSerialGC (see also this page)
no option on most Unix -XX:+UseG1GC from Java 9, or before that -XX:+UseParallelGC -XX:+UseParallelOldGC -XX:+UseAdaptiveSizePolicy (see also this page)
-XX:+AggressiveHeap -XX:+UseParallelGC -XX:+UseParallelOldGC -XX:+UseAdaptiveSizePolicy with a bunch of other options related to sizing memory and threads and how they interact with the OS
分享到:
评论

相关推荐

    Java GC的过程

    Java垃圾收集(Garbage Collection, 简称GC)是Java编程中的一项重要特性,它自动管理内存,释放不再使用的对象,避免了程序员手动管理内存可能导致的内存泄露问题。本篇将深入探讨Java中的GC过程。 一、Java内存...

    Java GC与性能调优

    Java GC与性能调优 Java GC与性能调优是 Java programming language 中非常重要的一部分,直接影响着 Java application 的性能。本文档将对 Java GC 与性能调优进行详细的介绍。 一、 Java 平台的逻辑结构 Java ...

    成为JavaGC专家PartII-如何监控Java垃圾回收

    在Java编程语言中,垃圾回收(Garbage Collection, GC)是一项至关重要的机制,它自动管理内存,释放不再使用的对象,防止内存泄漏。本篇将深入探讨如何监控Java的垃圾回收,帮助开发者提升应用性能和稳定性。 Java...

    Java基础[Java基础]--Java GC工作原理

    ### Java GC工作原理详解 #### 一、引言 在现代软件开发中,Java作为一种广泛使用的编程语言,其强大的功能和高效性深受开发者喜爱。Java的自动内存管理机制是其一大亮点,尤其体现在垃圾收集(Garbage Collection...

    Java_GC垃圾回收调优指南

    ### Java GC垃圾回收调优指南 #### 概述 在Java开发过程中,垃圾回收(Garbage Collection, GC)是管理内存资源的关键技术之一。合理的GC配置可以显著提高应用程序的性能和稳定性。本指南旨在帮助开发者深入理解...

    JavaGC专家指南.pdf

    Java虚拟机(JVM)是运行Java字节码的虚拟机进程。它负责在不同操作系统上...以上就是对JavaGC专家指南中提到的知识点的一个全面解析,涵盖了从虚拟机的基本概念到内存管理,再到垃圾回收和性能调优的各个重要方面。

    Java GC的副本.pptx

    【Java GC机制详解】 Java垃圾回收(Garbage Collection, GC)是Java虚拟机(JVM)的核心特性之一,它负责自动管理程序运行时的内存,有效地避免了内存泄露和溢出的问题。Java GC机制主要涉及以下几个关键方面: 1...

    JavaGC调优基础.pdf

    Java垃圾收集(Garbage Collection, 简称GC)是Java编程中一个重要的概念,它负责自动管理程序中的内存,回收不再使用的对象所占用的内存空间。GC调优是优化Java应用程序性能的关键环节,尤其是在大型系统中,正确...

    java GC手册

    GC java 手册 kindle格式 GC java 手册 kindle格式 GC java 手册 kindle格式

    Java GC笔记总纲1

    Java垃圾收集(GC)是Java编程中至关重要的一个部分,它自动管理程序的内存,以避免内存泄漏和系统资源耗尽。以下是对标题和描述中提及的知识点的详细阐述: ### 1. 基础知识 #### 1.1 常见的内存溢出 内存溢出是...

    JAVA GC 与 JVM调优1

    JAVA GC (Garbage Collection) 是Java编程语言中的一个重要特性,它自动管理程序的内存,确保不再使用的对象能够被及时回收,以防止内存泄漏。GC主要针对的是Java堆内存中的对象,这里的对象是由Java栈中的引用指向...

    Java GC on HP-UX Itanium

    【Java GC on HP-UX Itanium】和【JDK on HP-UX 简要介绍及性能调试概要】这两个主题涉及到的是Java在HP-UX操作系统上的运行环境、内存管理和性能优化,特别是针对Itanium架构的Java垃圾收集(GC)机制。以下是详细...

    Java GC 专题

    Java垃圾收集(Garbage Collection, 简称GC)是Java编程中的一个重要概念,它自动管理内存,使得程序员不必手动释放不再使用的对象,从而避免了内存泄漏的问题。本专题将深入探讨Java GC的基础知识,包括其工作原理...

    成为JavaGC专家(4)

    在第一篇文章成为JavaGC专家PartI—深入浅出Java垃圾回收机制中我们学习了不同GC算法的执行过程,GC如何工作,新生代及老年代的基本概念,在JDK7中你应该了解的5种GC类型以及他们的性能如何。在第二篇文章成为JavaGC...

    jvm 参数及gc详解

    Java虚拟机(JVM)是Java程序运行的基础,它的配置参数和垃圾收集(GC)机制对于优化应用程序性能至关重要。本文将深入探讨JVM参数及其与Java垃圾收集相关的知识。 一、JVM参数详解 JVM参数可以分为三类:启动参数...

    java gc调优

    Java垃圾收集(Garbage Collection, 简称GC)是Java平台中的一项重要特性,它自动管理程序的内存,使得开发者无需手动进行内存分配和释放。然而,GC调优是每个Java开发者都需要面对的挑战,因为合适的GC配置可以显著...

    成为JavaGC专家上(1)—深入浅出Java垃圾回收机制

    对于Java开发人员来说,了解垃圾回收机制(GC)有哪些好处呢?首先可以满足作为一名软件工程师的求知欲,其次,深入了解GC如何工作可以...这是成为JavaGC专家系列文章的第一篇,本篇主要针对GC机制进行介绍,在下一篇

Global site tag (gtag.js) - Google Analytics