The heap size difference among Parallel, CMS, and G1

Lijie Xu csxulijie at gmail.com
Wed Dec 20 07:47:58 UTC 2017


Hi All,

I'm testing the performance of Parallel, CMS, and G1. I found that their
available heap sizes are different under the same -Xmx configuration.

For example, I set the -Xmx to 6.5GB (6656m) and
use Runtime.getRuntime().maxMemory() to obtain the real heap size.
The output is as follows.

Running with: [-Xmx6656m, -XX:+UseParallelGC, -XX:+UseParallelOldGC]

Runtime.getRuntime().maxMemory(): 6,058,496K, 5916.50MB, 5.78GB.

memoryStore: 3.29GB.


Running with: [-Xmx6656m, -XX:+UseConcMarkSweepGC]

Runtime.getRuntime().maxMemory(): 6,747,648K, 6589.50MB, 6.44GB.

memoryStore: 3.69GB.


Running with: [-Xmx6656m, -XX:+UseG1GC]

Runtime.getRuntime().maxMemory(): 6,815,744K, 6656.00MB, 6.50GB.

memoryStore: 3.72GB.

We can see that the available heap sizes are [Parallel: 5.78GB < CMS:
6.44GB < G1: 6.50GB].
My JDK version is

java version "1.8.0_121"

Java(TM) SE Runtime Environment (build 1.8.0_121-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Any suggestions will be appreciated.


The test script is as follows.
#!/bin/bash
XMX=6656m
java -Xmx$XMX -XX:+UseParallelGC -XX:+UseParallelOldGC HeapSizeDifferences
java -Xmx$XMX -XX:+UseConcMarkSweepGC HeapSizeDifferences
java -Xmx$XMX -XX:+UseG1GC HeapSizeDifferences

The testing code is as follows.

import java.lang.management.ManagementFactory;

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;



public class HeapSizeDifferences {



  static Collection<Object> objects = new ArrayList<Object>();

  static long lastMaxMemory = 0;



  public static void main(String[] args) {

    try {

      List<String> inputArguments =
ManagementFactory.getRuntimeMXBean().getInputArguments();

      System.out.println("Running with: " + inputArguments);

      while (true) {

        printMaxMemory();

        consumeSpace();

      }

    } catch (OutOfMemoryError e) {

      freeSpace();

      printMaxMemory();

    }

  }



  static void printMaxMemory() {

    long currentMaxMemory = Runtime.getRuntime().maxMemory();

    if (currentMaxMemory != lastMaxMemory) {

      lastMaxMemory = currentMaxMemory;

      System.out.format("Runtime.getRuntime().maxMemory(): %,dK, %.2fMB,
%.2fGB.%n",

                    currentMaxMemory / 1024, (float) currentMaxMemory /
1024 / 1024,

                    (float) currentMaxMemory / 1024 / 1024 / 1024);

      double memoryStore = (currentMaxMemory - 300 * 1024 * 1024) * 0.6;

      System.out.format("memoryStore: %.2fGB.%n", memoryStore / 1024 / 1024
/ 1024);

    }

  }



  static void consumeSpace() {

    objects.add(new int[1_000_000]);

  }



  static void freeSpace() {

    objects.clear();

  }

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/hotspot-gc-use/attachments/20171220/d631211d/attachment.html>


More information about the hotspot-gc-use mailing list