test/gc/7168848/HumongousAlloc.java

Thu, 19 Sep 2013 09:36:51 -0700

author
cl
date
Thu, 19 Sep 2013 09:36:51 -0700
changeset 5664
34aa07e92d22
parent 3808
9a344d88dc22
permissions
-rw-r--r--

Added tag jdk8-b108 for changeset 85072013aad4

     1 /*
     2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test Humongous.java
    26  * @bug 7168848
    27  * @summary G1: humongous object allocations should initiate marking cycles when necessary
    28  * @run main/othervm -Xms100m -Xmx100m -XX:+PrintGC -XX:G1HeapRegionSize=1m -XX:+UseG1GC  HumongousAlloc
    29  *
    30  */
    31 import java.lang.management.GarbageCollectorMXBean;
    32 import java.lang.management.ManagementFactory;
    33 import java.util.List;
    35 public class HumongousAlloc {
    37     public static byte[] dummy;
    38     private static int sleepFreq = 40;
    39     private static int sleepTime = 1000;
    40     private static double size = 0.75;
    41     private static int iterations = 50;
    42     private static int MB = 1024 * 1024;
    44     public static void allocate(int size, int sleepTime, int sleepFreq) throws InterruptedException {
    45         System.out.println("Will allocate objects of size: " + size
    46                 + " bytes and sleep for " + sleepTime
    47                 + " ms after every " + sleepFreq + "th allocation.");
    48         int count = 0;
    49         while (count < iterations) {
    50             for (int i = 0; i < sleepFreq; i++) {
    51                 dummy = new byte[size - 16];
    52             }
    53             Thread.sleep(sleepTime);
    54             count++;
    55         }
    56     }
    58     public static void main(String[] args) throws InterruptedException {
    59         allocate((int) (size * MB), sleepTime, sleepFreq);
    60         List<GarbageCollectorMXBean> collectors = ManagementFactory.getGarbageCollectorMXBeans();
    61         for (GarbageCollectorMXBean collector : collectors) {
    62             if (collector.getName().contains("G1 Old")) {
    63                long count = collector.getCollectionCount();
    64                 if (count > 0) {
    65                     throw new RuntimeException("Failed: FullGCs should not have happened. The number of FullGC run is " + count);
    66                 }
    67                 else {
    68                     System.out.println("Passed.");
    69                 }
    70             }
    71         }
    72     }
    73 }

mercurial