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 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 eatenMemory = eatHeapMemory(targetMemoryUsagePercent); 77 eatenMetaspace = eatMetaspace(targetMemoryUsagePercent); 78 } 79 80 private static List<Object> eatMetaspace(float targetUsage) { 81 List<Object> list = new ArrayList<>(); 82 final String metaspacePoolName = "Metaspace"; 83 MemoryPoolMXBean metaspacePool = null; 84 for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) { 85 if (pool.getName().contains(metaspacePoolName)) { 86 metaspacePool = pool; 87 break; 88 } 89 } 90 if (metaspacePool == null) { 91 throw new RuntimeException("MXBean for Metaspace pool wasn't found"); 92 } 93 float currentUsage; 94 GeneratedClassProducer gp = new GeneratedClassProducer(); 95 do { 96 try { 97 list.add(gp.create(0)); 98 } catch (OutOfMemoryError oome) { 99 list = null; 100 throw new RuntimeException("Unexpected OOME while eating " + targetUsage + " of Metaspace."); 101 } 102 MemoryUsage memoryUsage = metaspacePool.getUsage(); 103 currentUsage = (((float) memoryUsage.getUsed()) / memoryUsage.getMax()); 104 } while (currentUsage < targetUsage); 105 return list; 106 } 107 108 public GcProvokerImpl() { 109 } 110 111 }