Does Java G1 garbage collector generate Java inconsistencies?

I recently tried to activate a garbage collector and evaluate it. In the beginning, I wrote this code, trying to create java.lang.OutOfMemoryError:

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class G1Test {
  public static void floodMemory() {
    int i = 0;
    try {
      // allocate an array where we will just store a lot of java objects
      List<Date> l = new ArrayList<Date>();
      for (; i < 1051366050; i++) {
        l.add(new Date());
      }
    } catch (Throwable t) {
      System.err.println("Throwable in floodMemory!");
      System.out.println("i=" + i);
      t.printStackTrace();
    }
  }

  public static void main(String[] args) {
    try {
      System.out.println("Started memory flooding.");
      floodMemory();
      System.out.println("Sleeping.");
      Thread.sleep(Long.MAX_VALUE);
    } catch (Throwable t) {
      System.err.println("Throwable in main!");
      t.printStackTrace();
    }
  }
}

... and I ran the code using two scripts:

Case 1. With these flags:, -Xmx4096M -XX:+UseG1GCI get this output:

    Started memory flooding.
    Throwable in main!
    java.lang.OutOfMemoryError: Java heap space
    at com.siemens.scr.usi.experimental.G1Test.floodMemory (G1Test.java:14)
    at com.siemens.scr.usi.experimental.G1Test.main (G1Test.java:26)

... which means that the infamous is OutOfMemoryErrorthrown away somewhere, but captured in the main method.

Case 2. With this flag:, -Xmx4096MI get this output:

Started memory flooding.
Throwable in floodMemory!
i=105136605
java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2245)
    at java.util.Arrays.copyOf(Arrays.java:2219)
    at java.util.ArrayList.grow(ArrayList.java:242)
    at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:216)
    at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:208)
    at java.util.ArrayList.add(ArrayList.java:440)
    at com.siemens.scr.usi.experimental.G1Test.floodMemory(G1Test.java:14)
    at com.siemens.scr.usi.experimental.G1Test.main(G1Test.java:26)
Sleeping.

... , , , .

:

  • - - .
  • Oracle JDK 1.7.0, 60, 64 Windows 7 Enterprise, Dell Precision M4700.

, - - - ( ).

+4
1

, , , :

(-Xmx4096M -XX: + UseG1GC): OutOfMemoryError floodMemory, , OutOfMemory, , OutOfMemoryError catch floodMemory. , System.err, . .

System.err, .

, . Java 1.7, , , , , .

0

Source: https://habr.com/ru/post/1548564/


All Articles