Basic GC Tuning
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.
- By default, then, the young generation starts out
- -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的更多相关文章
- GC和GC Tuning
GC和GC Tuning GC的基础知识 什么是垃圾 C语言申请内存:malloc free C++: new delete c/C++ 手动回收内存 Java: new ? 自动内存回收,编程上简单 ...
- 提交并发量的方法:Java GC tuning :Garbage collector
三色算法,高效率垃圾回收,jvm调优 Garbage collector:垃圾回收器 What garbage? 没有任何引用指向它的对象 JVM GC回收算法: 引用计数法(ReferenceCou ...
- Java 8 VM GC Tuning Guide Charter2
第二章 Ergonomics Ergonomics is the process by which the Java Virtual Machine (JVM) and garbage collect ...
- 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 ...
- Understanding CMS GC Logs--转载
原文地址:https://blogs.oracle.com/poonam/entry/understanding_cms_gc_logs Understanding CMS GC Logs By Po ...
- Tuning Spark
https://spark.apache.org/docs/1.2.1/tuning.html Data Serialization 数据序列化,对于任意分布式系统都是性能的关键点 Spark默认使用 ...
- 【转载】为什么不建议<=3G的情况下使用CMS GC
之前曾经有讲过在heap size<=3G的情况下完全不要考虑CMS GC,在heap size>3G的情况下也优先选择ParallelOldGC,而不是CMS GC,只有在暂停时间无法接 ...
- jvm系列(十):如何优化Java GC「译」
本文由CrowHawk翻译,是Java GC调优的经典佳作. 本文翻译自Sangmin Lee发表在Cubrid上的"Become a Java GC Expert"系列文章的第三 ...
- Java GC性能优化实战
GC优化是必要的吗? 或者更准确地说,GC优化对Java基础服务来说是必要的吗?答案是否定的,事实上GC优化对Java基础服务来说在有些场合是可以省去的,但前提是这些正在运行的Java系统,必须包含以 ...
随机推荐
- 编译内核时出现drivers/mfd/mxc-hdmi-core.c:36:24: fatal error: mach/clock.h: No such file or directory
在学习恩智浦IMX6D开发板时,编译内核出现 drivers/mfd/mxc-hdmi-core.c::: fatal error: mach/clock.h: No such file or dir ...
- 大湾区联动:广州深圳助力东莞.NET俱乐部首次线下活动
新年伊始,经过一个寒冬考验后的.NET社区热情不减,长沙.南京.合肥.东莞先后建立以微信为主要平台的线上.NET社区.并相继开始筹划和组织各地区的首次线下活动.东莞作为粤港澳大湾区的腹地,制造业基地, ...
- (最完美)小米平板3的USB调试模式在哪里开启的流程
经常我们使用安卓手机链上电脑的时候,或者使用的有些应用软件比如我们公司营销小组经常使用的应用软件引号精灵,之前的老版本就需要开启usb调试模式下使用,现经常新版本不需要了,如果手机没有开启usb调试模 ...
- 学习安卓开发[3] - 使用RecyclerView显示列表
在上一篇学习安卓开发[2] - 在Activity中托管Fragment中了解了使用Fragment的好处和方法,本次记录的是在进行列表展示时RecyclerView的使用. RecyclerView ...
- PHP如何实现在数据库随机获取几条记录
本文实例讲述了PHP实现在数据库百万条数据中随机获取20条记录的方法.PHP实例分享给大家供大家参考,具体如下: 为什么要写这个? 在去某个公司面试时,让写个算法出来,当时就蒙了,我开发过程中用到算法 ...
- ASP.NET MVC从空项目开始定制项目
在上一篇net core的文章中已经讲过如何从零开始搭建WebSocket. 今天聊聊ASP.NET的文件结构,如何用自己的目录结构组织项目里的文件. 如果用Visual Studio(VS)向导或d ...
- UE3客户端加入DS过程
拉起DS进程 客户端将比赛地图及相关参数发送给ZoneSvr请求开赛,收到消息后,ZoneSvr会分配一个ip和端口号,并与客户端发过来的地图及其他参数,来构建一个命令行来拉起一个DS进程, DS启动 ...
- SQL Server捕获发生The query processor ran out of internal resources and could not produce a query plan...错误的SQL语句
最近收到一SQL Server数据库服务器的告警邮件,告警内容具体如下所示: DATE/TIME: 10/23/2018 4:30:26 PM DESCRIPTION: The query proc ...
- MFC映射
所有CDC输出函数最终都会输出到物理平面(屏幕窗口.打印纸等).这些物理平面的单位量化往往多种多样,比如像素.打印点.英寸.毫米等等.这样可能会造成很多混乱,所以CDC输出对所有物理平面进行统一抽象化 ...
- sqlserver中批量导出所有作业或链接脚本
问题描述: 经常在数据库迁移到另外一台服务器的时候,需要把作业也迁移过去,但是作业有时候好多个,要是一个个编写监本出来很麻烦 今天知道个简单方法批量可以导出sql脚本,顺便做个笔记 解决方法: 1.在 ...