Sizing the Heap

  • -XmsN
  • -XmxN

Summary

  • The JVM will attempt to find a reasonable minimum and maximum 
    heap size based on the machine it is running on.
  • Unless the application needs a larger heap than the default, consider 
    tuning the performance goals of a GC algorithm (given in 
    the next chapter) rather than fine-tuning the heap size.

Sizing the Generations

  • -XX:NewRatio=N

    • Set the ratio of the young generation to the old generation.
    • default value of 2.
    • Initial Young Gen Size = Initial Heap Size / (1 + NewRatio) 
      • By default, then, the young generation starts out 
        at 33% of the initial heap size.
  • -XX:NewSize=N 
    • Set the initial size of the young generation.
    • If that option NewSize flag is set, it will take precedence over the value calculated fromthe NewRatio.
  • -XX:MaxNewSize=N 
    • Set the maximum size of the young generation.
    • By default, that maximum is also set using the 
      NewRatio value, though it is based on the maximum (rather than initial) heap size.
  • -XmnN 
    • Shorthand for setting both NewSize and MaxNewSize to the same value.
    • When a heap size is fixed (by setting -Xms equal to -Xmx),it is usually preferable to use -Xmn to specify a fixed size for the young generation as well.

Summary

  • Within the overall heap size, the sizes of the generations are 
    controlled by how much space is allocated to the young generation.
  • The young generation will grow in tandem with the overall heap 
    size, but it can also fluctuate as a percentage of the total heap 
    (based on the initial and maximum size of the young generation).

Sizing Permgen and Metaspace

  • When the JVM loads classes, it must keep track of certain metadata about those classes. 
    From the perspective of an end user, this is all just bookkeeping information. This data 
    is held in a separate heap space. In Java 7, this is called the permgen (or permanent 
    generation), and in Java 8, this is called the metaspace.
  • Permgen and metaspace are not exactly the same thing. In Java 7, permgen contains 
    some miscellaneous objects that are unrelated to class data; these are moved into the 
    regular heap in Java 8. As end users, all we need to know is that permgen/metaspace holds a bunch of class-related data, and that there are certain circumstances where the size of that region needs to be tuned.
  • Note that permgen/metaspace does not hold the actual instance of the class (the Class 
    objects), nor reflection objects (e.g., Method objects); those are held in the regular heap. 
    Information in permgen/metaspace is really only used by the compiler and JVM runtime, 
    and the data it holds is referred to as class metadata.
  • the metaspace rarely needs to be sized—because (unlike permgen) metaspace will by default use as much space as it needs.
  • These memory regions behave just like a separate instance of the regular heap. They are 
    sized dynamically based on an initial size and will increase as needed to a maximum size.
  • permgen, 
    • -XX:PermSize=N and -XX:MaxPermSize=N.
  • Metaspace 
    • -XX:MetaspaceSize=N and -XX:MaxMetaspaceSize=N.
  • Default maximum metaspace size Unlimited 
    • Because the default size of metaspace is unlimited, there is the possibility (particularly in a 32-bit JVM) that a Java 8 application can run out of memory by filling up metaspace.
  • Heap dumps can be used to diagnose what classloaders exist, which in 
    turn can help determine if a classloader leak is filling up permgen (or metaspace). 
    Otherwise, jmap can be used with the argument -permstat (in Java 7) or -clstats (in 
    Java 8) to print out information about the classloaders.

Summary

  • The permanent generation or metaspace holds class metadata (not class data itself). It behaves like a separate heap.
  • For typical applications that do not load classes after startup, the initial size of this region can be based on its usage after all classes have been loaded. That will slightly speed up startup.
  • Application servers doing development (or any environment where classes are frequently redefined) will see an occasional full GC caused when permgen/metaspace fills up and old class metadata is discarded.

Controlling Parallelism

  • All GC algorithms except the serial collector use multiple threads. The number of these 
    threads is controlled by the -XX:ParallelGCThreads=N flag. The value of this flag affects 
    the number of threads used for the following operations:

    • Collection of the young generation when using -XX:+UseParallelGC
    • Collection of the old generation when using -XX:+UseParallelOldGC
    • Collection of the young generation when using -XX:+UseParNewGC
    • Collection of the young generation when using -XX:+UseG1GC
    • Stop-the-world phases of CMS (though not full GCs)
    • Stop-the-world phases of G1 (though not full GCs)
  • Because these GC operations stop the application threads from executing, the JVM 
    attempts to use as many CPU resources as it can in order to minimize the pause time. 
    By default, that means the JVM will run one thread for each CPU on a machine, up to 
    eight. Once that threshold has been reached, the JVM only adds a new thread for every 
    five-eighths of a CPU. So the total number of threads (where N is the number of CPUs) 
    on a machine with more than eight CPUs is: 
    • ParallelGCThreads = 8 + ((N - 8) * 5 / 8)
  • Note that this flag does not set the number of background threads used by CMS or G1

Summary

  • The basic number of threads used by all GC algorithms is based 
    on the number of CPUs on a machine.
  • When multiple JVMs are run on a single machine, that number 
    will be too high and must be reduced.

Adaptive Sizing

  • The sizes of the heap, the generations, and the survivor spaces can vary during execution 
    as the JVM attempts to find the optimal performance according to its policies and 
    tunings.
  • This is a best-effort solution, and it relies on past performance: the assumption is that 
    future GC cycles will look similar to the GC cycles in the recent past. That turns out to 
    be a reasonable assumption for many workloads, and even if the allocation rate suddenly 
    changes, the JVM will readapt its sizes based on the new information.
  • At a global level, adaptive sizing is disabled by turning off the -XX:-UseAdaptiveSize 
    Policy flag (which is true by default). With the exception of the survivor spaces (which 
    are examined in detail in the next chapter), adaptive sizing is also effectively turned off 
    if the minimum and maximum heap sizes are set to the same value, and the initial and 
    maximum sizes of the new generation are set to the same value.

Summary

  • Adaptive sizing controls how the JVM alters the ratio of young 
    generation to old generation within the heap.
  • Adaptive sizing should generally be kept enabled, since adjusting 
    those generation sizes is how GC algorithms attempt to meet 
    their pause time goals.
  • For finely tuned heaps, adaptive sizing can be disabled for a small 
    performance boost.

Basic GC Tuning的更多相关文章

  1. GC和GC Tuning

    GC和GC Tuning GC的基础知识 什么是垃圾 C语言申请内存:malloc free C++: new delete c/C++ 手动回收内存 Java: new ? 自动内存回收,编程上简单 ...

  2. 提交并发量的方法:Java GC tuning :Garbage collector

    三色算法,高效率垃圾回收,jvm调优 Garbage collector:垃圾回收器 What garbage? 没有任何引用指向它的对象 JVM GC回收算法: 引用计数法(ReferenceCou ...

  3. Java 8 VM GC Tuning Guide Charter2

    第二章 Ergonomics Ergonomics is the process by which the Java Virtual Machine (JVM) and garbage collect ...

  4. Java 8 VM GC Tuning Guide Charter3-4

    第三章 Generations One strength of the Java SE platform is that it shields the developer from the compl ...

  5. Understanding CMS GC Logs--转载

    原文地址:https://blogs.oracle.com/poonam/entry/understanding_cms_gc_logs Understanding CMS GC Logs By Po ...

  6. Tuning Spark

    https://spark.apache.org/docs/1.2.1/tuning.html Data Serialization 数据序列化,对于任意分布式系统都是性能的关键点 Spark默认使用 ...

  7. 【转载】为什么不建议<=3G的情况下使用CMS GC

    之前曾经有讲过在heap size<=3G的情况下完全不要考虑CMS GC,在heap size>3G的情况下也优先选择ParallelOldGC,而不是CMS GC,只有在暂停时间无法接 ...

  8. jvm系列(十):如何优化Java GC「译」

    本文由CrowHawk翻译,是Java GC调优的经典佳作. 本文翻译自Sangmin Lee发表在Cubrid上的"Become a Java GC Expert"系列文章的第三 ...

  9. Java GC性能优化实战

    GC优化是必要的吗? 或者更准确地说,GC优化对Java基础服务来说是必要的吗?答案是否定的,事实上GC优化对Java基础服务来说在有些场合是可以省去的,但前提是这些正在运行的Java系统,必须包含以 ...

随机推荐

  1. java重写与重载的区别

    override(重写) :即把改方法重新写一次,内部逻辑可变,外壳不变,核心重写 1. 方法名.参数.返回值相同. 2. 子类方法不能缩小父类方法的访问权限. 3. 子类方法不能抛出比父类方法更多的 ...

  2. java日期转化,三种基本的日期格式

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...

  3. 多线程(5)async&await

    .net 4.0的Task已经让我们可以非常简单地使用多线程,并且可以有返回值,也可以支持线程的取消等操作,可谓已经很强大了.但.net 4.5为我们带来了async&await,使得实现多线 ...

  4. 史上最全python面试题详解(四)(附带详细答案(关注、持续更新))

    python高级进阶-网络编程和并发(?道题详解) 1.简述 OSI 七层协议. OSI是Open System Interconnection的缩写,意为开放式系统互联. OSI七层协议模型主要是: ...

  5. Js与jQuery的相互转换

    $()与jQuery() jQuery中$函数,根据传入参数的不同,进行不同的调用,实现不同的功能.返回的是jQuery对象 jQuery这个js库,除了$之外,还提供了另外一个函数:jQuery j ...

  6. 动态BGP和静态BGP的含义与区别

    1.在华为云上选购虚拟机时,会让用户选择动态BGP还是静态BGP, 全动态BGP可根据设定的寻路协议第一时间自动优化网络结构,以保持客户使用的网络持续稳定.高效. 静态BGP中的网络结构发生变化,运营 ...

  7. Android预置Apk方法

    这一套8.0过时了 需要修改pms代码 否则apk会被pms删除掉 因为工作需要,经常要开发和合入系统App,所以在此开篇作为收集和记录Android合入系统应用的方法,以备日后查阅. 一.预置apk ...

  8. Chrome浏览器,处理input自动填充时带黄色背景色

    /*Chrome浏览器打开网页,input自动赋值时,会带上屎黄色的背景色,下面是通过延长增加自动填充背景色的方式, 让用户感受不到样式的变化*/ input:-webkit-autofill, in ...

  9. 手机Soc芯片简介

    手机SoC(System On a Chip,在一个芯片里面集成CPU.GPU.SP.ISP.RAM内存.Wi-Fi控制器.基带芯片以及音频芯片等)芯片(基于arm架构指令集) 高通骁龙(Snapdr ...

  10. DAS、SAN和NAS三种存储方式

    DAS存储 DAS存储在我们生活中是非常常见的,尤其是在中小企业应用中,DAS是最主要的应用模式,存储系统被直连到应用的服务器中,在中小企业中,许多的数据应用是必须安装在直连的DAS存储器上. DAS ...