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 
  30 /**
  31  * Parent class for all tests of shrinking heap
  32  * Performs initialize, eating and releasing memory via child's inherited methods 
  33  * Fails if heap doesn't shrink
  34  * @author andrey.x.zakharov@oracle.com
  35  */
  36 public abstract class TestShrinkHeap {
  37 
  38     public static final String MIN_FREE_RATIO_FLAG_NAME = "MinHeapFreeRatio";
  39     public static final String MAX_FREE_RATIO_FLAG_NAME = "MaxHeapFreeRatio";
  40 
  41     protected TestShrinkHeap() {
  42     }
  43 
  44     public final void test() {
  45         System.gc();
  46         MemoryUsagePrinter.printMemoryUsage("init");
  47 
  48         eat();        
  49         MemoryUsagePrinter.printMemoryUsage("eaten");
  50         MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
  51         
  52         free();
  53         MemoryUsagePrinter.printMemoryUsage("free");
  54         MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
  55 
  56         if (!(muFree.getCommitted() < muFull.getCommitted())) {
  57             throw new RuntimeException(
  58                     String.format("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     
  69     protected abstract void eat();
  70     protected abstract void free();
  71 }