hotspot/test/serviceability/tmtools/jstat/utils/GcProvokerImpl.java
changeset 35493 863fb33f9940
child 35945 f532c5c62540
equal deleted inserted replaced
35491:663c609dfeee 35493:863fb33f9940
       
     1 /*
       
     2  * Copyright (c) 2015, 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         float used = 0;
       
    46         while (used < targetUsage * maxMemory) {
       
    47             try {
       
    48                 list.add(new byte[memoryChunk]);
       
    49                 used += memoryChunk;
       
    50             } catch (OutOfMemoryError e) {
       
    51                 list = null;
       
    52                 throw new RuntimeException("Unexpected OOME while eating " + targetUsage + " of heap memory.");
       
    53             }
       
    54         }
       
    55         return list;
       
    56     }
       
    57 
       
    58     @Override
       
    59     public void provokeGc() {
       
    60         for (int i = 0; i < 3; i++) {
       
    61             long edenSize = Pools.getEdenCommittedSize();
       
    62             long heapSize = Pools.getHeapCommittedSize();
       
    63             float targetPercent = ((float) edenSize) / (heapSize);
       
    64             if ((targetPercent <= 0) || (targetPercent > 1.0)) {
       
    65                 throw new RuntimeException("Error in the percent calculation" + " (eden size: " + edenSize + ", heap size: " + heapSize + ", calculated eden percent: " + targetPercent + ")");
       
    66             }
       
    67             eatHeapMemory(targetPercent);
       
    68             eatHeapMemory(targetPercent);
       
    69             System.gc();
       
    70         }
       
    71     }
       
    72 
       
    73     @Override
       
    74     public void eatMetaspaceAndHeap(float targetMemoryUsagePercent) {
       
    75         eatenMemory = eatHeapMemory(targetMemoryUsagePercent);
       
    76         eatenMetaspace = eatMetaspace(targetMemoryUsagePercent);
       
    77     }
       
    78 
       
    79     private static List<Object> eatMetaspace(float targetUsage) {
       
    80         List<Object> list = new ArrayList<>();
       
    81         final String metaspacePoolName = "Metaspace";
       
    82         MemoryPoolMXBean metaspacePool = null;
       
    83         for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
       
    84             if (pool.getName().contains(metaspacePoolName)) {
       
    85                 metaspacePool = pool;
       
    86                 break;
       
    87             }
       
    88         }
       
    89         if (metaspacePool == null) {
       
    90             throw new RuntimeException("MXBean for Metaspace pool wasn't found");
       
    91         }
       
    92         float currentUsage;
       
    93         GeneratedClassProducer gp = new GeneratedClassProducer();
       
    94         do {
       
    95             try {
       
    96                 list.add(gp.create(0));
       
    97             } catch (OutOfMemoryError oome) {
       
    98                 list = null;
       
    99                 throw new RuntimeException("Unexpected OOME while eating " + targetUsage + " of Metaspace.");
       
   100             }
       
   101             MemoryUsage memoryUsage = metaspacePool.getUsage();
       
   102             currentUsage = (((float) memoryUsage.getUsed()) / memoryUsage.getMax());
       
   103         } while (currentUsage < targetUsage);
       
   104         return list;
       
   105     }
       
   106 
       
   107     public GcProvokerImpl() {
       
   108     }
       
   109 
       
   110 }