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 package com.oracle.java.testlibrary.gc; 25 26 import java.lang.management.ManagementFactory; 27 import java.lang.management.MemoryUsage; 28 import sun.management.ManagementFactoryHelper; 29 import static com.oracle.java.testlibrary.Asserts.*; 30 31 /** 32 * Parent class for all tests of shrinking heap 33 * Performs initialize, eating and releasing memory via child's inherited methods 34 * Fails if heap doesn't shrink 35 * @author andrey.x.zakharov@oracle.com 36 */ 37 public abstract class TestShrinkHeap { 38 39 public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio"; 40 public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio"; 41 42 protected TestShrinkHeap() { 43 } 44 45 public final void test() { 46 System.gc(); 47 MemoryUsagePrinter.printMemoryUsage("init"); 48 49 eat(); 50 MemoryUsagePrinter.printMemoryUsage("eaten"); 51 MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 52 53 free(); 54 MemoryUsagePrinter.printMemoryUsage("free"); 55 MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); 56 57 assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format( 58 "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n" 59 + "%s = %s%n%s = %s", 60 MIN_FREE_RATIO_FLAG_NAME, 61 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(), 62 MAX_FREE_RATIO_FLAG_NAME, 63 ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue() 64 )); 65 66 } 67 68 protected abstract void eat(); 69 protected abstract void free(); 70 }