1 /*
   2  * Copyright (c) 2019, 2020, 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  */
  23 
  24 package gc.z;
  25 
  26 /*
  27  * @test TestUncommit
  28  * @requires vm.gc.Z & !vm.graal.enabled & vm.compMode != "Xcomp"
  29  * @summary Test ZGC uncommit unused memory
  30  * @run main/othervm -XX:+UseZGC -Xlog:gc*,gc+stats=off -Xms128M -Xmx512M -XX:ZUncommitDelay=10 gc.z.TestUncommit true 2
  31  * @run main/othervm -XX:+UseZGC -Xlog:gc*,gc+stats=off -Xms512M -Xmx512M -XX:ZUncommitDelay=10 gc.z.TestUncommit false 1
  32  * @run main/othervm -XX:+UseZGC -Xlog:gc*,gc+stats=off -Xms128M -Xmx512M -XX:ZUncommitDelay=10 -XX:-ZUncommit gc.z.TestUncommit false 1
  33  */
  34 
  35 /*
  36  * This test is disabled when running with -Xcomp, since it seems to affect
  37  * the timing of the test, causing memory to appear to be uncommitted too fast.
  38  */
  39 
  40 import java.util.ArrayList;
  41 
  42 public class TestUncommit {
  43     private static final int delay = 10; // seconds
  44     private static final int allocSize = 200 * 1024 * 1024; // 200M
  45     private static final int smallObjectSize = 4 * 1024; // 4K
  46     private static final int mediumObjectSize = 2 * 1024 * 1024; // 2M
  47     private static final int largeObjectSize = allocSize;
  48 
  49     private static volatile ArrayList<byte[]> keepAlive;
  50 
  51     private static long capacity() {
  52         return Runtime.getRuntime().totalMemory();
  53     }
  54 
  55     private static void allocate(int objectSize) {
  56         keepAlive = new ArrayList<>();
  57         for (int i = 0; i < allocSize; i+= objectSize) {
  58             keepAlive.add(new byte[objectSize]);
  59         }
  60     }
  61 
  62     private static void reclaim() {
  63         keepAlive = null;
  64         System.gc();
  65     }
  66 
  67     private static void test(boolean enabled, int objectSize) throws Exception {
  68         final var beforeAlloc = capacity();
  69 
  70         // Allocate memory
  71         allocate(objectSize);
  72 
  73         final var afterAlloc = capacity();
  74 
  75         // Reclaim memory
  76         reclaim();
  77 
  78         // Wait shorter than the uncommit delay
  79         Thread.sleep(delay * 1000 / 2);
  80 
  81         final var beforeUncommit = capacity();
  82 
  83         // Wait longer than the uncommit delay
  84         Thread.sleep(delay * 1000);
  85 
  86         final var afterUncommit = capacity();
  87 
  88         System.out.println("  Uncommit Enabled: " + enabled);
  89         System.out.println("    Uncommit Delay: " + delay);
  90         System.out.println("       Object Size: " + objectSize);
  91         System.out.println("        Alloc Size: " + allocSize);
  92         System.out.println("      Before Alloc: " + beforeAlloc);
  93         System.out.println("       After Alloc: " + afterAlloc);
  94         System.out.println("   Before Uncommit: " + beforeUncommit);
  95         System.out.println("    After Uncommit: " + afterUncommit);
  96         System.out.println();
  97 
  98         // Verify
  99         if (enabled) {
 100             if (beforeUncommit == beforeAlloc) {
 101                 throw new Exception("Uncommitted too fast");
 102             }
 103 
 104             if (afterUncommit >= afterAlloc) {
 105                 throw new Exception("Uncommitted too slow");
 106             }
 107 
 108             if (afterUncommit < beforeAlloc) {
 109                 throw new Exception("Uncommitted too much");
 110             }
 111 
 112             if (afterUncommit > beforeAlloc) {
 113                 throw new Exception("Uncommitted too little");
 114             }
 115         } else {
 116             if (afterAlloc > beforeUncommit ||
 117                 afterAlloc > afterUncommit) {
 118                 throw new Exception("Should not uncommit");
 119             }
 120         }
 121     }
 122 
 123     public static void main(String[] args) throws Exception {
 124         final boolean enabled = Boolean.parseBoolean(args[0]);
 125         final int iterations = Integer.parseInt(args[1]);
 126 
 127         for (int i = 0; i < iterations; i++) {
 128             System.out.println("Iteration " + i);
 129             test(enabled, smallObjectSize);
 130             test(enabled, mediumObjectSize);
 131             test(enabled, largeObjectSize);
 132         }
 133     }
 134 }