8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap

Thu, 11 Sep 2014 14:21:13 +0200

author
jwilhelm
date
Thu, 11 Sep 2014 14:21:13 +0200
changeset 7174
df66e3a3c4c2
parent 7173
a98dd542cd25
child 7175
9b8bd21b6823

8041946: CMM Testing: 8u40 an allocated humongous object at the end of the heap should not prevents shrinking the heap
Summary: New test added
Reviewed-by: jwilhelm, tschatzl
Contributed-by: andrey.x.zakharov@oracle.com

test/gc/g1/TestShrinkDefragmentedHeap.java file | annotate | diff | comparison | revisions
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/gc/g1/TestShrinkDefragmentedHeap.java	Thu Sep 11 14:21:13 2014 +0200
     1.3 @@ -0,0 +1,188 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * @test TestShrinkDefragmentedHeap
    1.29 + * @bug 8038423
    1.30 + * @summary Verify that heap shrinks after GC in the presence of fragmentation due to humongous objects
    1.31 + *     1. allocate small objects mixed with humongous ones
    1.32 + *        "ssssHssssHssssHssssHssssHssssHssssH"
    1.33 + *     2. release all allocated object except the last humongous one
    1.34 + *        "..................................H"
    1.35 + *     3. invoke gc and check that memory returned to the system (amount of committed memory got down)
    1.36 + *
    1.37 + * @library /testlibrary
    1.38 + */
    1.39 +import java.lang.management.ManagementFactory;
    1.40 +import java.lang.management.MemoryUsage;
    1.41 +import java.util.ArrayList;
    1.42 +import java.util.List;
    1.43 +import sun.management.ManagementFactoryHelper;
    1.44 +import static com.oracle.java.testlibrary.Asserts.*;
    1.45 +import com.oracle.java.testlibrary.ProcessTools;
    1.46 +import com.oracle.java.testlibrary.OutputAnalyzer;
    1.47 +
    1.48 +public class TestShrinkDefragmentedHeap {
    1.49 +    // Since we store all the small objects, they become old and old regions are also allocated at the bottom of the heap
    1.50 +    // together with humongous regions. So if there are a lot of old regions in the lower part of the heap,
    1.51 +    // the humongous regions will be allocated in the upper part of the heap anyway.
    1.52 +    // To avoid this the Eden needs to be big enough to fit all the small objects.
    1.53 +    private static final int INITIAL_HEAP_SIZE  = 200 * 1024 * 1024;
    1.54 +    private static final int MINIMAL_YOUNG_SIZE = 190 * 1024 * 1024;
    1.55 +    private static final int REGION_SIZE        = 1 * 1024 * 1024;
    1.56 +
    1.57 +    public static void main(String[] args) throws Exception, Throwable {
    1.58 +        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    1.59 +                "-XX:InitialHeapSize=" + INITIAL_HEAP_SIZE,
    1.60 +                "-Xmn" + MINIMAL_YOUNG_SIZE,
    1.61 +                "-XX:MinHeapFreeRatio=10",
    1.62 +                "-XX:MaxHeapFreeRatio=11",
    1.63 +                "-XX:+UseG1GC",
    1.64 +                "-XX:G1HeapRegionSize=" + REGION_SIZE,
    1.65 +                "-verbose:gc",
    1.66 +                GCTest.class.getName()
    1.67 +        );
    1.68 +
    1.69 +        OutputAnalyzer output = ProcessTools.executeProcess(pb);
    1.70 +        output.shouldHaveExitValue(0);
    1.71 +    }
    1.72 +
    1.73 +    static class GCTest {
    1.74 +
    1.75 +        private static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
    1.76 +        private static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
    1.77 +        private static final String NEW_SIZE_FLAG_NAME = "NewSize";
    1.78 +
    1.79 +        private static final ArrayList<ArrayList<byte[]>> garbage = new ArrayList<>();
    1.80 +
    1.81 +        private static final int SMALL_OBJS_SIZE  = 10 * 1024; // 10kB
    1.82 +        private static final int SMALL_OBJS_COUNT = MINIMAL_YOUNG_SIZE / (SMALL_OBJS_SIZE-1);
    1.83 +        private static final int ALLOCATE_COUNT = 3;
    1.84 +        // try to put all humongous object into gap between min young size and initial heap size
    1.85 +        // to avoid implicit GCs
    1.86 +        private static final int HUMONG_OBJS_SIZE = (int) Math.max(
    1.87 +                (INITIAL_HEAP_SIZE - MINIMAL_YOUNG_SIZE) / ALLOCATE_COUNT / 4,
    1.88 +                REGION_SIZE * 1.1
    1.89 +        );
    1.90 +
    1.91 +        private static final long initialHeapSize = getHeapMemoryUsage().getUsed();
    1.92 +
    1.93 +        public static void main(String[] args) throws InterruptedException {
    1.94 +            new GCTest().test();
    1.95 +        }
    1.96 +
    1.97 +        private void test() throws InterruptedException {
    1.98 +            MemoryUsagePrinter.printMemoryUsage("init");
    1.99 +
   1.100 +            allocate();
   1.101 +            System.gc();
   1.102 +            MemoryUsage muFull = getHeapMemoryUsage();
   1.103 +            MemoryUsagePrinter.printMemoryUsage("allocated");
   1.104 +
   1.105 +            free();
   1.106 +            //Thread.sleep(1000); // sleep before measures due lags in JMX
   1.107 +            MemoryUsage muFree = getHeapMemoryUsage();
   1.108 +            MemoryUsagePrinter.printMemoryUsage("free");
   1.109 +
   1.110 +            assertLessThan(muFree.getCommitted(), muFull.getCommitted(), prepareMessageCommittedIsNotLess() );
   1.111 +        }
   1.112 +
   1.113 +        private void allocate() {
   1.114 +            System.out.format("Will allocate objects of small size = %s and humongous size = %s",
   1.115 +                    MemoryUsagePrinter.humanReadableByteCount(SMALL_OBJS_SIZE, false),
   1.116 +                    MemoryUsagePrinter.humanReadableByteCount(HUMONG_OBJS_SIZE, false)
   1.117 +            );
   1.118 +
   1.119 +            for (int i = 0; i < ALLOCATE_COUNT; i++) {
   1.120 +                ArrayList<byte[]> stuff = new ArrayList<>();
   1.121 +                allocateList(stuff, SMALL_OBJS_COUNT / ALLOCATE_COUNT, SMALL_OBJS_SIZE);
   1.122 +                garbage.add(stuff);
   1.123 +
   1.124 +                ArrayList<byte[]> humongousStuff = new ArrayList<>();
   1.125 +                allocateList(humongousStuff, 4, HUMONG_OBJS_SIZE);
   1.126 +                garbage.add(humongousStuff);
   1.127 +            }
   1.128 +        }
   1.129 +
   1.130 +        private void free() {
   1.131 +            // do not free last one list
   1.132 +            garbage.subList(0, garbage.size() - 1).clear();
   1.133 +
   1.134 +            // do not free last one element from last list
   1.135 +            ArrayList stuff = garbage.get(garbage.size() - 1);
   1.136 +            if (stuff.size() > 1) {
   1.137 +                stuff.subList(0, stuff.size() - 1).clear();
   1.138 +            }
   1.139 +            System.gc();
   1.140 +        }
   1.141 +
   1.142 +        private String prepareMessageCommittedIsNotLess() {
   1.143 +            return String.format(
   1.144 +                    "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n"
   1.145 +                    + "%s = %s%n%s = %s",
   1.146 +                    MIN_FREE_RATIO_FLAG_NAME,
   1.147 +                    ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(),
   1.148 +                    MAX_FREE_RATIO_FLAG_NAME,
   1.149 +                    ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue()
   1.150 +            );
   1.151 +        }
   1.152 +
   1.153 +        private static void allocateList(List garbage, int count, int size) {
   1.154 +            for (int i = 0; i < count; i++) {
   1.155 +                garbage.add(new byte[size]);
   1.156 +            }
   1.157 +        }
   1.158 +    }
   1.159 +
   1.160 +    static MemoryUsage getHeapMemoryUsage() {
   1.161 +        return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
   1.162 +    }
   1.163 +
   1.164 +    /**
   1.165 +     * Prints memory usage to standard output
   1.166 +     */
   1.167 +    static class MemoryUsagePrinter {
   1.168 +
   1.169 +        public static String humanReadableByteCount(long bytes, boolean si) {
   1.170 +            int unit = si ? 1000 : 1024;
   1.171 +            if (bytes < unit) {
   1.172 +                return bytes + " B";
   1.173 +            }
   1.174 +            int exp = (int) (Math.log(bytes) / Math.log(unit));
   1.175 +            String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
   1.176 +            return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
   1.177 +        }
   1.178 +
   1.179 +        public static void printMemoryUsage(String label) {
   1.180 +            MemoryUsage memusage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
   1.181 +            float freeratio = 1f - (float) memusage.getUsed() / memusage.getCommitted();
   1.182 +            System.out.format("[%-24s] init: %-7s, used: %-7s, comm: %-7s, freeRatio ~= %.1f%%%n",
   1.183 +                    label,
   1.184 +                    humanReadableByteCount(memusage.getInit(), false),
   1.185 +                    humanReadableByteCount(memusage.getUsed(), false),
   1.186 +                    humanReadableByteCount(memusage.getCommitted(), false),
   1.187 +                    freeratio * 100
   1.188 +            );
   1.189 +        }
   1.190 +    }
   1.191 +}

mercurial