简体   繁体   English

G1GC:如何使用所有空闲内存?

[英]G1GC: How to use all free memory?

I try to use G1GC with my program.我尝试在我的程序中使用 G1GC。 Program is used on various machines with various memory size: VPS with 1Gb memory (minimum), desktop with 8Gb memory, DS with 32Gb memory (maximum).程序用于具有各种内存大小的各种机器:具有 1Gb 内存(最小)的 VPS、具有 8Gb 内存的台式机、具有 32Gb 内存(最大)的 DS。 I noticed that G1GC not reserve more memory even if there is a lot of free memory (for example, G1GC not reserve more than 3Gb on my machine with 8Gb total / 4Gb free)我注意到即使有很多可用内存,G1GC 也不会保留更多内存(例如,G1GC 不会在我的机器上保留超过 3Gb 的内存,总共有 8Gb/4Gb 可用)

PS I want to have universal solution. PS我想要通用的解决方案。 I cannot create separate version or separate run script for each type of machine.我无法为每种类型的机器创建单独的版本或单独的运行脚本。

I think you have chosen the wrong garbage collection algorithm.我认为您选择了错误的垃圾收集算法。 The Java 8 documentation offers this guidance: Java 8 文档提供了以下指导:

Selecting a Collector选择收集器

Unless your application has rather strict pause time requirements, first run your application and allow the VM to select a collector.除非您的应用程序对暂停时间有相当严格的要求,否则首先运行您的应用程序并允许 VM 选择一个收集器。 If necessary, adjust the heap size to improve performance.如有必要,调整堆大小以提高性能。 If the performance still does not meet your goals, then use the following guidelines as a starting point for selecting a collector.如果性能仍不能满足您的目标,请使用以下指南作为选择收集器的起点。

  • If the application has a small data set (up to approximately 100 MB), then select the serial collector with the option -XX:+UseSerialGC .如果应用程序的数据集很小(最多大约 100 MB),则选择带有选项-XX:+UseSerialGC的串行收集器。

  • If the application will be run on a single processor and there are no pause time requirements, then let the VM select the collector, or select the serial collector with the option -XX:+UseSerialGC .如果应用程序将在单个处理器上运行并且没有暂停时间要求,那么让 VM 选择收集器,或者使用选项-XX:+UseSerialGC选择串行收集器。

  • If (a) peak application performance is the first priority and (b) there are no pause time requirements or pauses of 1 second or longer are acceptable, then let the VM select the collector, or select the parallel collector with -XX:+UseParallelGC .如果 (a) 峰值应用程序性能是第一优先级并且 (b) 没有暂停时间要求或 1 秒或更长的暂停是可以接受的,那么让 VM 选择收集器,或者使用-XX:+UseParallelGC选择并行收集器.

  • If response time is more important than overall throughput and garbage collection pauses must be kept shorter than approximately 1 second, then select the concurrent collector with -XX:+UseConcMarkSweepGC or -XX:+UseG1GC .如果响应时间比总吞吐量更重要并且垃圾收集暂停必须保持短于大约 1 秒,则选择带有-XX:+UseConcMarkSweepGC-XX:+UseG1GC的并发收集器。

Source: Selecting a Collector来源: 选择收藏家

Based on your comments, it seems that your goal is to get peak performance;根据您的评论,您的目标似乎是获得最佳性能; ie minimize the overall time spent on GC and related overheads.即最大限度地减少在 GC 和相关开销上花费的总时间。

That means that your best options are:这意味着您最好的选择是:

  • Set some performance goals and let the JVM to decide which collector is best.设置一些性能目标,让 JVM 决定哪个收集器最好。 See the Behavior-based Tuning material for details of the performance goals mechanisms.有关性能目标机制的详细信息,请参阅基于行为的调优材料。
  • Select the Serial GC if you have only one core.如果您只有一个内核,请选择 Serial GC。
  • Select the Parallel GC if you have more than one core.如果您有多个内核,请选择 Parallel GC。

If you want a one-size-fits all script that works irrespective of your hardware, the performance goal approach is best, though that means you won't be able to use platform-specific settings to (potentially) improve on the JVM's decisions.如果您想要一个适用于所有硬件的通用脚本,性能目标方法是最好的,但这意味着您将无法使用特定于平台的设置来(可能)改进 JVM 的决策。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM