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