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 8036025 27 * @summary Verify that heap shrinking after GC of hard defragmented by humongous object heap 28 * @library /testlibrary 29 * @run main/othervm -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=50 -XX:+UseG1GC -XX:G1HeapRegionSize=1M -Xloggc:gc_10_50.log TestHumongousShrinkHeap 30 */ 31 32 import com.oracle.java.testlibrary.gc.MemoryUsagePrinter; 33 import com.oracle.java.testlibrary.gc.TestShrinkHeap; 34 import java.lang.management.ManagementFactory; 35 import java.lang.management.MemoryUsage; 36 import java.util.ArrayList; 37 import java.util.List; 38 import sun.management.ManagementFactoryHelper; 39 import static com.oracle.java.testlibrary.Asserts.*; 40 41 public class TestHumongousShrinkHeap extends TestShrinkHeap { 42 43 private static final ArrayList<ArrayList<byte[]>> garbage = new ArrayList<>(); 44 private static final int PAGE_SIZE = 1024 * 1024; // 1M 45 private static final int PAGES_NUM = 5; 46 47 48 public final void test() { 49 System.gc(); 50 MemoryUsagePrinter.printMemoryUsage("init"); 51 52 eat(); 53 MemoryUsagePrinter.printMemoryUsage("eaten"); 54 MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 55 56 free(); 57 MemoryUsagePrinter.printMemoryUsage("free"); 58 MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 59 60 assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format( 61 "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n" 62 + "%s = %s%n%s = %s", 63 MIN_FREE_RATIO_FLAG_NAME, 64 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(), 65 MAX_FREE_RATIO_FLAG_NAME, 66 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue() 67 )); 68 } 69 70 protected void eat() { 71 int HumongousObjectSize = Math.round(.9f * PAGE_SIZE); 72 System.out.println("Will allocate objects of size=" + 73 MemoryUsagePrinter.humanReadableByteCount(HumongousObjectSize, true)); 74 75 for (int i = 0; i < PAGES_NUM; i++) { 76 ArrayList<byte[]> stuff = new ArrayList<>(); 77 eatList(stuff, 100, HumongousObjectSize); 78 MemoryUsagePrinter.printMemoryUsage("eat #" + i); 79 garbage.add(stuff); 80 } 81 } 82 83 protected void free() { 84 // do not free last one list 85 garbage.subList(0, garbage.size() - 1).clear(); 86 87 // do not free last one element from last list 88 ArrayList stuff = garbage.get(garbage.size() - 1); 89 stuff.subList(0, stuff.size() - 1).clear(); 90 System.gc(); 91 } 92 93 public static void main(String[] args) { 94 new TestHumongousShrinkHeap().test(); 95 } 96 97 private static void eatList(List garbage, int count, int size) { 98 for (int i = 0; i < count; i++) { 99 garbage.add(new byte[size]); 100 } 101 } 102 }