1 /*
   2  * Copyright (c) 2014, 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 /**
  25  * @test TestHumongousShrinkHeap
  26  * @bug 8037925
  27  * @summary Verify that heap shrinking after GC of hard defragmented by humongous object heap
  28  * @library /testlibrary
  29  * @author Andrey Zakharov /andrey.x.zakharov@oracle.com/
  30  * @author Dmitry Fazunenko /dmitry.fazunenko@oracle.com/
  31  * @run main/othervm -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=50 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -Xloggc:gc_10_50.log TestHumongousShrinkHeap
  32  */
  33 import com.oracle.java.testlibrary.MemoryUsagePrinter;
  34 import com.oracle.java.testlibrary.TestShrinkHeap;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 
  38 public class TestHumongousShrinkHeap extends TestShrinkHeap {
  39 
  40     private static final ArrayList< ArrayList< byte[]>> garbage = new ArrayList<>();
  41     private static final int PageSize = 1024 * 1024; // 1M 
  42     private static final int PagesNum = 5;
  43     private final int HumongousObjectSize;
  44 
  45     public TestHumongousShrinkHeap() {
  46         super();
  47         HumongousObjectSize = Math.round(.9f * PageSize);
  48         System.out.println("Will allocate objects of size=" + MemoryUsagePrinter.humanReadableByteCount(HumongousObjectSize, true));
  49     }
  50 
  51     public void eat() {
  52         for (int i = 0; i < PagesNum; i++) {
  53             ArrayList<byte[]> stuff = new ArrayList<>();
  54             eatList(stuff, 100, HumongousObjectSize);
  55             MemoryUsagePrinter.printMemoryUsage("eat #" + i);
  56             garbage.add(stuff);
  57         }
  58     }
  59 
  60     public void free() {
  61         //do not free last one list
  62         garbage.subList(0, garbage.size() - 1).clear();
  63 
  64         // do not free last one element from last list
  65         ArrayList stuff = garbage.get(garbage.size() - 1);
  66         stuff.subList(0, stuff.size() - 1).clear();
  67         System.gc();
  68     }
  69 
  70     public static void main(String[] args) {
  71         new TestHumongousShrinkHeap().test();
  72     }
  73 
  74     public static void eatList(List garbage, int count, int size) {
  75         for (int i = 0; i < count; i++) {
  76             garbage.add(new byte[size]);
  77         }
  78     }
  79 }