1 /*
   2  * Copyright (c) 2015, 2017, 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 utils;
  24 
  25 import java.util.ArrayList;
  26 import java.util.List;
  27 
  28 /**
  29  * This is an class used to provoke GC and perform other GC-related
  30  * procedures
  31  *
  32  */
  33 public class GcProvoker{
  34 
  35     // Uses fixed small objects to avoid Humongous objects allocation in G1
  36     public static final int MEMORY_CHUNK = 2048;
  37 
  38     private final Runtime runtime;
  39 
  40     private List<Object> allocateHeap(float targetUsage) {
  41         long maxMemory = runtime.maxMemory();
  42         List<Object> list = new ArrayList<>();
  43         long used = 0;
  44         long target = (long) (maxMemory * targetUsage);
  45         while (used < target) {
  46             try {
  47                 list.add(new byte[MEMORY_CHUNK]);
  48                 used += MEMORY_CHUNK;
  49             } catch (OutOfMemoryError e) {
  50                 list = null;
  51                 throw new RuntimeException("Unexpected OOME '" + e.getMessage() + "' while eating " + targetUsage + " of heap memory.");
  52             }
  53         }
  54         return list;
  55     }
  56 
  57     /**
  58      * This method provokes a GC
  59      */
  60     public void provokeGc() {
  61         for (int i = 0; i < 3; i++) {
  62             long edenSize = Pools.getEdenCommittedSize();
  63             long heapSize = Pools.getHeapCommittedSize();
  64             float targetPercent = ((float) edenSize) / (heapSize);
  65             if ((targetPercent < 0) || (targetPercent > 1.0)) {
  66                 throw new RuntimeException("Error in the percent calculation" + " (eden size: " + edenSize + ", heap size: " + heapSize + ", calculated eden percent: " + targetPercent + ")");
  67             }
  68             allocateHeap(targetPercent);
  69             allocateHeap(targetPercent);
  70             System.gc();
  71         }
  72     }
  73 
  74     public GcProvoker() {
  75         runtime = Runtime.getRuntime();
  76     }
  77 }