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

标签 java garbage-collection g1gc

我尝试在我的程序中使用 G1GC。程序适用于具有不同内存大小的各种机器:具有 1Gb 内存(最小)的 VPS、具有 8Gb 内存的桌面、具有 32Gb 内存(最大)的 DS。我注意到即使有很多可用内存,G1GC 也不会保留更多内存(例如,G1GC 在我的机器上不会保留超过 3Gb,总共 8Gb/4Gb 可用内存)

附注我想要一个通用的解决方案。我无法为每种类型的机器创建单独的版本或单独的运行脚本。

最佳答案

我认为您选择了错误的垃圾收集算法。 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. 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.

  • 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.

  • 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.

  • 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.

来源:Selecting a Collector

根据您的评论,您的目标似乎是获得最佳性能;即最大限度地减少 GC 所花费的总时间和相关开销。

这意味着您最好的选择是:

  • 设置一些性能目标并让 JVM 决定哪个收集器最好。请参阅Behavior-based Tuning有关绩效目标机制详细信息的 Material 。
  • 如果您只有一个核心,请选择串行 GC。
  • 如果您有多个核心,请选择并行 GC。

如果您想要一个无论您的硬件如何都适用的通用脚本,性能目标方法是最好的,尽管这意味着您将无法使用特定于平台的设置来(可能)改进JVM 的决定。

关于java - G1GC:如何使用所有空闲内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35824572/

相关文章:

java - jsp中自定义错误信息

ubuntu - 我的 openjdk-7-jdk 已过时

go - 在 Go 中,什么时候变量会变得不可访问?

tomcat - Xms与Xmx和XX的区别 :MaxPermSize

java - 为什么在达到 InitiatingHeapOccupancyPercent 时 G1 不开始标记周期?

java - 为什么 G1 垃圾收集器卸载时间随着时间的推移而增加?

java - G1 GC 是否有最大区域大小或最大区域数量?

java - Spring 4 Websocket 支持和客户端订阅所有主题

java - java中如何通过周数和日数(Week)来确定日期

java - 如何确保 Java 中的 String 对象被垃圾回收