hotspot/test/serviceability/tmtools/jstat/utils/GcProvokerImpl.java
changeset 43946 b683a1f9f2e5
parent 43684 771c619e190d
parent 43945 e7f2e49d2274
child 43947 a52ee13998f3
equal deleted inserted replaced
43684:771c619e190d 43946:b683a1f9f2e5
     1 /*
       
     2  * Copyright (c) 2015, 2016, 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.lang.management.ManagementFactory;
       
    26 import java.lang.management.MemoryPoolMXBean;
       
    27 import java.lang.management.MemoryUsage;
       
    28 import java.util.ArrayList;
       
    29 import java.util.List;
       
    30 
       
    31 /**
       
    32  *
       
    33  * Utilities to provoke GC in various ways
       
    34  */
       
    35 public class GcProvokerImpl implements GcProvoker {
       
    36 
       
    37     private static List<Object> eatenMetaspace;
       
    38     private static List<Object> eatenMemory;
       
    39 
       
    40     static List<Object> eatHeapMemory(float targetUsage) {
       
    41         long maxMemory = Runtime.getRuntime().maxMemory();
       
    42         // uses fixed small objects to avoid Humongous objects allocation in G1
       
    43         int memoryChunk = 2048;
       
    44         List<Object> list = new ArrayList<>();
       
    45         long used = 0;
       
    46         long target = (long) (maxMemory * targetUsage);
       
    47         while (used < target) {
       
    48             try {
       
    49                 list.add(new byte[memoryChunk]);
       
    50                 used += memoryChunk;
       
    51             } catch (OutOfMemoryError e) {
       
    52                 list = null;
       
    53                 throw new RuntimeException("Unexpected OOME '" + e.getMessage() + "' while eating " + targetUsage + " of heap memory.");
       
    54             }
       
    55         }
       
    56         return list;
       
    57     }
       
    58 
       
    59     @Override
       
    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             eatHeapMemory(targetPercent);
       
    69             eatHeapMemory(targetPercent);
       
    70             System.gc();
       
    71         }
       
    72     }
       
    73 
       
    74     @Override
       
    75     public void eatMetaspaceAndHeap(float targetMemoryUsagePercent) {
       
    76         // Metaspace should be filled before Java Heap to prevent unexpected OOME
       
    77         // in the Java Heap while filling Metaspace
       
    78         eatenMetaspace = eatMetaspace(targetMemoryUsagePercent);
       
    79         eatenMemory = eatHeapMemory(targetMemoryUsagePercent);
       
    80     }
       
    81 
       
    82     private static List<Object> eatMetaspace(float targetUsage) {
       
    83         List<Object> list = new ArrayList<>();
       
    84         final String metaspacePoolName = "Metaspace";
       
    85         MemoryPoolMXBean metaspacePool = null;
       
    86         for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
       
    87             if (pool.getName().contains(metaspacePoolName)) {
       
    88                 metaspacePool = pool;
       
    89                 break;
       
    90             }
       
    91         }
       
    92         if (metaspacePool == null) {
       
    93             throw new RuntimeException("MXBean for Metaspace pool wasn't found");
       
    94         }
       
    95         float currentUsage;
       
    96         GeneratedClassProducer gp = new GeneratedClassProducer();
       
    97         do {
       
    98             try {
       
    99                 list.add(gp.create(0));
       
   100             } catch (OutOfMemoryError oome) {
       
   101                 list = null;
       
   102                 throw new RuntimeException("Unexpected OOME '" + oome.getMessage() + "' while eating " + targetUsage + " of Metaspace.");
       
   103             }
       
   104             MemoryUsage memoryUsage = metaspacePool.getUsage();
       
   105             currentUsage = (((float) memoryUsage.getUsed()) / memoryUsage.getMax());
       
   106         } while (currentUsage < targetUsage);
       
   107         return list;
       
   108     }
       
   109 
       
   110     public GcProvokerImpl() {
       
   111     }
       
   112 
       
   113 }